cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=637073813b197b36bacb566af34144576fde3a57

commit 637073813b197b36bacb566af34144576fde3a57
Author: Cedric BAIL <ced...@osg.samsung.com>
Date:   Fri Dec 16 14:05:10 2016 -0800

    ecore: add helper to automatically connect a future result to a promise.
    
    This helper is to be used likely with next and an external request.
---
 src/lib/ecore/efl_promise.c  | 38 +++++++++++++++++++++++++++++++++++++-
 src/lib/ecore/efl_promise.eo | 11 +++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/lib/ecore/efl_promise.c b/src/lib/ecore/efl_promise.c
index 9ced564..a9e5177 100644
--- a/src/lib/ecore/efl_promise.c
+++ b/src/lib/ecore/efl_promise.c
@@ -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;
diff --git a/src/lib/ecore/efl_promise.eo b/src/lib/ecore/efl_promise.eo
index acc6e74..84cf0cf 100644
--- a/src/lib/ecore/efl_promise.eo
+++ b/src/lib/ecore/efl_promise.eo
@@ -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.]]

-- 


Reply via email to