summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2017-08-26 19:04:43 -0300
committerGuilherme Iscaro <iscaro@profusion.mobi>2017-09-04 10:24:00 -0300
commitd258774c2ad108fbb0d59e4f3b513349510669e1 (patch)
tree8fb7919ffda1da512dec5dd12680a5d5efd515a8
parent3ba9f25cf7ad74986bf77b679041ce2ad6758536 (diff)
downloadefl-d258774c2ad108fbb0d59e4f3b513349510669e1.tar.gz
Eina_Future: add eina_future_resolved()
This is a helper that creates a promise, then a future and immediately resolves the promise, this will let the future be dispatched as usual, from a clean main loop context.
-rw-r--r--src/lib/eina/eina_promise.c26
-rw-r--r--src/lib/eina/eina_promise.h37
2 files changed, 61 insertions, 2 deletions
diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c
index 596149fb10..e1b891865a 100644
--- a/src/lib/eina/eina_promise.c
+++ b/src/lib/eina/eina_promise.c
@@ -599,7 +599,7 @@ _future_proxy(void *data, const Eina_Value v,
}
static void
-_proxy_cancel(void *data EINA_UNUSED, const Eina_Promise *dead_ptr EINA_UNUSED)
+_dummy_cancel(void *data EINA_UNUSED, const Eina_Promise *dead_ptr EINA_UNUSED)
{
}
@@ -619,7 +619,7 @@ eina_future_as_value(Eina_Future *f)
Eina_Future *r_future;
EINA_FUTURE_CHECK_RETURN_VAL(f, v);
- p = eina_promise_new(_scheduler_get(f), _proxy_cancel, NULL);
+ p = eina_promise_new(_scheduler_get(f), _dummy_cancel, NULL);
EINA_SAFETY_ON_NULL_GOTO(p, err_promise);
r_future = eina_future_then(f, _future_proxy, p);
//If eina_future_then() fails f will be cancelled
@@ -793,6 +793,28 @@ _eina_future_then(Eina_Future *prev, const Eina_Future_Desc desc)
}
EAPI Eina_Future *
+eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value)
+{
+ Eina_Promise *p;
+ Eina_Future *f;
+
+ EINA_SAFETY_ON_NULL_GOTO(scheduler, error);
+
+ p = eina_promise_new(scheduler, _dummy_cancel, NULL);
+ EINA_SAFETY_ON_NULL_GOTO(p, error);
+
+ f = eina_future_new(p);
+ EINA_SAFETY_ON_NULL_GOTO(f, error);
+
+ eina_promise_resolve(p, value);
+ return f;
+
+ error:
+ eina_value_flush(&value);
+ return NULL;
+}
+
+EAPI Eina_Future *
eina_future_then_from_desc(Eina_Future *prev, const Eina_Future_Desc desc)
{
EINA_FUTURE_CHECK_GOTO(prev, err_future);
diff --git a/src/lib/eina/eina_promise.h b/src/lib/eina/eina_promise.h
index 43aa90ad16..db6b6432bd 100644
--- a/src/lib/eina/eina_promise.h
+++ b/src/lib/eina/eina_promise.h
@@ -521,6 +521,10 @@ struct _Eina_Future_Desc {
* }
* @endcode
*
+ * If you already have a value and want to create a future that will
+ * resolve to it directly use the eina_future_resolved(), it has the
+ * same effect as creating a promise and immediately resolving it.
+ *
* @param cancel_cb A callback used to inform that the promise was canceled. Use
* this callback to @c free @p data. @p cancel_cb must not be @c NULL !
* @param data Data to @p cancel_cb.
@@ -741,6 +745,39 @@ EAPI Eina_Value eina_future_as_value(Eina_Future *f)EINA_ARG_NONNULL(1) EINA_WAR
* @see eina_future_cancel()
*/
EAPI Eina_Future *eina_future_new(Eina_Promise *p) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
+
+/**
+ * Creates a new future that is already resolved to a value.
+ *
+ * This function creates a new future with an already known value,
+ * that will be resolved and dispatched by the given @a scheduler as
+ * usual.
+ *
+ * This is a helper that behaves the same as eina_promise_new()
+ * followed by eina_future_new() and then eina_promise_resolve().
+ *
+ * Futures can also be canceled using eina_future_cancel(), which will
+ * cause the whole chain to be cancelled alongside with any pending
+ * promise.
+ *
+ * @param scheduler The scheduler to use.
+ * @param value The value to be delivered. Note that the value
+ * contents must survive this function scope, that is, do @b not use
+ * stack allocated blobs, arrays, structures or types that keeps
+ * references to memory you give. Values will be automatically cleaned
+ * up using @c eina_value_flush() once they are unused (no more future
+ * or futures returned a new value).
+ *
+ * @return The future or @c NULL on error.
+ *
+ * @see eina_promise_new()
+ * @see eina_future_new()
+ * @see eina_promise_reject()
+ * @see eina_promise_resolve()
+ * @see eina_future_cancel()
+ */
+EAPI Eina_Future *eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value) EINA_ARG_NONNULL(1);
+
/**
* Register an Eina_Future_Desc to be used when the future is resolve/rejected.
*