ecore: add helper to automatically connect a future result to a promise.
authorCedric BAIL <cedric@osg.samsung.com>
Fri, 16 Dec 2016 22:05:10 +0000 (14:05 -0800)
committerCedric BAIL <cedric@osg.samsung.com>
Fri, 16 Dec 2016 22:10:58 +0000 (14:10 -0800)
This helper is to be used likely with next and an external request.

src/lib/ecore/efl_promise.c
src/lib/ecore/efl_promise.eo

index 9ced5643ea82ea5820236c5160fa5afdccad824f..a9e5177bc96cac5467107ee292fe64e944a46cca 100644 (file)
@@ -297,7 +297,7 @@ _efl_loop_future_internal_then(Efl_Future *f,
 
    efl_unref(f);
 
-   return EINA_FALSE;
+   return EINA_TRUE;
 }
 
 static void
@@ -710,6 +710,42 @@ _efl_promise_efl_object_destructor(Eo *obj, Efl_Promise_Data *pd)
    efl_destructor(efl_super(obj, EFL_PROMISE_CLASS));
 }
 
+static void
+_efl_promise_connect_then(void *data, const Efl_Event *ev)
+{
+   Efl_Future_Event_Success *success = ev->info;
+   Efl_Promise_Data *pd = data;
+   // This is a trick due to the fact we are using internal function call to register this functions
+   Efl_Promise_Msg *d = success->value;
+
+   EINA_REFCOUNT_REF(d);
+   pd->message = d;
+
+   _efl_promise_propagate(pd->promise, pd);
+
+   efl_unref(pd->promise);
+}
+
+static void
+_efl_promise_connect_fail(void *data, const Efl_Event *ev)
+{
+   Efl_Future_Event_Failure *fail = ev->info;
+   Efl_Promise_Data *pd = data;
+
+   efl_promise_failed_set(pd->promise, fail->error);
+
+   efl_unref(pd->promise);
+}
+
+static Eina_Bool
+_efl_promise_connect(Eo *obj, Efl_Promise_Data *pd, Efl_Future *f)
+{
+   // We have to keep a reference on the promise to avoid it dying before the future
+   efl_ref(obj);
+
+   return _efl_loop_future_internal_then(f, _efl_promise_connect_then, _efl_promise_connect_fail, NULL, pd);
+}
+
 typedef struct _Efl_Promise_Composite Efl_Promise_All;
 typedef struct _Efl_Future_All Efl_Future_All;
 typedef struct _Efl_Accessor_All Efl_Accessor_All;
index acc6e749ee124678600623150ff062094b813a1c..84cf0cf46c3472361c4ed5368ace4a4f33c6b6fc 100644 (file)
@@ -53,6 +53,17 @@ class Efl.Promise (Efl.Loop_User)
            err: Eina.Error; [[The reason for failure of this promise.]]
          }
       }
+      connect {
+         [[Connect a future output to this promise.
+
+           This helper will automatically trigger #value.set or #failed.set when the future
+           succeed or fail respectively with the value provided by the future.
+        ]]
+        params {
+           @in f: future<void_ptr, void_ptr>; [[The future to connect this promise to.]]
+        }
+        return: bool; [[Return false if unable to setup the connection.]]
+      }
    }
    events {
       future,set: future<void_ptr>; [[This event is triggered whenever a future is fully set to receive all events and that the user of it do not hold any more reference on it.]]