core: Don't wait for item when there is no need to
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 13 Dec 2010 16:59:41 +0000 (18:59 +0200)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 13 Dec 2010 17:18:55 +0000 (19:18 +0200)
The 'updated' signal from the container could be emitted 'before' we start
to wait for it so better first check if item is already there and then
wait if its not.

src/rygel/rygel-item-creator.vala

index 081aa09..bc6d927 100644 (file)
@@ -291,29 +291,30 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
     private async void wait_for_item (WritableContainer container) {
         debug ("Waiting for new item to appear under container '%s'..",
                container.id);
-        var not_added = true;
 
-        while (not_added) {
-            var id = container.container_updated.connect ((container) => {
-                this.wait_for_item.callback ();
-            });
-
-            yield;
-
-            container.disconnect (id);
+        MediaItem item = null;
 
+        while (item == null) {
             try {
-                var item = yield container.find_object (this.item.id,
-                                                        this.cancellable);
-                if (item != null) {
-                    not_added = false;
-                }
+                item = yield container.find_object (this.item.id,
+                                                    this.cancellable)
+                       as MediaItem;
             } catch (Error error) {
                 warning ("Error from container '%s' on trying to find newly " +
                          "added child item '%s' in it",
                          container.id,
                          this.item.id);
             }
+
+            if (item == null) {
+                var id = container.container_updated.connect ((container) => {
+                    this.wait_for_item.callback ();
+                });
+
+                yield;
+
+                container.disconnect (id);
+            }
         }
         debug ("Finished waiting for new item to appear under container '%s'",
                container.id);