[gnome-games/async-cover: 1/3] grilo-media: Add resolve_media_async



commit a89aba1d5a87a556b11a1bd7197a22ad37fd58ef
Author: theawless <theawless gmail com>
Date:   Sat Oct 6 17:45:26 2018 +0530

    grilo-media: Add resolve_media_async
    
    This will allow Grilo objects to be fetched asynchronously.

 src/grilo/grilo-media.vala | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
---
diff --git a/src/grilo/grilo-media.vala b/src/grilo/grilo-media.vala
index 738cb710..eb40ff96 100644
--- a/src/grilo/grilo-media.vala
+++ b/src/grilo/grilo-media.vala
@@ -8,16 +8,25 @@ public class Games.GriloMedia : Object {
 
        public signal void resolved ();
 
+       private class ResolveCallback {
+               public SourceFunc func;
+
+               public ResolveCallback (owned SourceFunc func) {
+                       this.func = (owned) func;
+               }
+       }
+
        private Title title;
        private string mime_type;
        private bool resolving;
-
+       private GenericSet<ResolveCallback> resolve_cbs;
        private Grl.Media? media;
 
        public GriloMedia (Title title, string mime_type) {
                this.title = title;
                this.mime_type = mime_type;
                resolving = false;
+               resolve_cbs = new GenericSet<ResolveCallback> (direct_hash, direct_equal);
        }
 
        private static Grl.Registry get_registry () throws Error {
@@ -42,6 +51,16 @@ public class Games.GriloMedia : Object {
                return registry;
        }
 
+       public async void resolve_media_async () {
+               if (media != null)
+                       return;
+
+               resolve_cbs.add (new ResolveCallback (resolve_media_async.callback));
+               try_resolve_media ();
+
+               yield;
+       }
+
        public void try_resolve_media () {
                try {
                        resolve_media ();
@@ -87,5 +106,10 @@ public class Games.GriloMedia : Object {
        private void on_media_resolved (Grl.Source source, uint operation_id, owned Grl.Media media, 
GLib.Error? error) {
                this.media = media;
                resolved ();
+
+               resolve_cbs.foreach ((resolve_cb) => {
+                       Idle.add ((owned) resolve_cb.func);
+               });
+               resolve_cbs.remove_all ();
        }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]