summaryrefslogtreecommitdiff
path: root/src/examples/ecore/ecore_promise2_example.c
blob: a88b1ff13ee91a54653eb0be260c484ead5b1d12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define EFL_BETA_API_SUPPORT 1
#include <Efl_Core.h>
#include <stdlib.h>
#include <errno.h>

typedef struct _Ctx {
   Eina_Promise *p;
   Eina_Bool should_fail;
   Eina_Future *timer;
   Eina_Value *value;
} Ctx;

typedef struct _Inner_Promise_Ctx {
   Eina_Future *future;
   Eina_Promise *promise;
} Inner_Promise_Ctx;

#define DEFAULT_MSG "the simple example is working!"

#define VALUE_TYPE_CHECK(_v, _type)                                     \
  if (_v.type != _type)                                                 \
    {                                                                   \
       fprintf(stderr, "Value type is not '%s' - received '%s'\n",      \
               _type->name, _v.type->name);                             \
       return _v;                                                       \
    }

static Eina_Value
_timeout(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Ctx *ctx = data;
   if (v.type == EINA_VALUE_TYPE_ERROR) goto end;
   if (ctx->should_fail) eina_promise_reject(ctx->p, ENETDOWN);
   else
     {
        Eina_Value tmp;
        eina_value_copy(ctx->value, &tmp);
        eina_promise_resolve(ctx->p, tmp);
     }
 end:
   if (ctx->value) eina_value_free(ctx->value);
   ctx->timer = NULL;
   free(ctx);
   return v;
}

static Eina_Value
_promise_cancel(Eo *o EINA_UNUSED, void *data, Eina_Error error)
{
   Ctx *ctx = data;

   if (error == ECANCELED && ctx->timer) eina_future_cancel(ctx->timer);
   return eina_value_error_init(error);
}

static Ctx *
_promise_ctx_new(Efl_Loop *loop, Eina_Value *v)
{
   Ctx *ctx;
   ctx = calloc(1, sizeof(Ctx));
   EINA_SAFETY_ON_NULL_GOTO(ctx, err_ctx);
   ctx->p = efl_loop_promise_new(loop);
   EINA_SAFETY_ON_NULL_GOTO(ctx->p, err_timer);
   ctx->value = v;
   return ctx;
 err_timer:
   free(ctx);
 err_ctx:
   eina_value_free(v);
   return NULL;
}

static Eina_Future *
_future_get(Ctx *ctx, Efl_Loop *loop)
{
   Eina_Future *f;

   f = eina_future_new(ctx->p);
   EINA_SAFETY_ON_NULL_GOTO(f, err_future);
   ctx->timer = eina_future_then(efl_loop_timeout(loop, 0.1), _timeout, ctx);
   EINA_SAFETY_ON_NULL_GOTO(ctx->timer, err_timer);
   return efl_future_then(loop, f, .error = _promise_cancel, .data = ctx);

 err_timer:
   eina_future_cancel(f);
 err_future:
   return NULL;
}

static Eina_Future *
_fail_future_get(Efl_Loop *loop)
{
   Ctx *ctx = _promise_ctx_new(loop, NULL);
   EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
   ctx->should_fail = EINA_TRUE;
   return _future_get(ctx, loop);
}

static Eina_Future *
_str_future_get(Efl_Loop *loop)
{
   Eina_Value *v = eina_value_util_string_new(DEFAULT_MSG);
   EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL);
   Ctx *ctx = _promise_ctx_new(loop, v);
   EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
   return _future_get(ctx, loop);
}

static Eina_Future *
_int_future_get(Efl_Loop *loop, int i)
{
   Eina_Value *v = eina_value_util_int_new(i);
   EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL);
   Ctx *ctx = _promise_ctx_new(loop, v);
   EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
   return _future_get(ctx, loop);
}

static Eina_Value
_simple_ok(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
   return v;
}

static Eina_Value
_alternate_error_cb(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Bool *should_fail = data;
   Eina_Value new_v = EINA_VALUE_EMPTY;

   if (*should_fail)
     {
        *should_fail = EINA_FALSE;
        eina_value_setup(&new_v, EINA_VALUE_TYPE_ERROR);
        eina_value_set(&new_v, ENETDOWN);
        printf("Received succes from the previous future - Generating error for the next future...\n");
     }
   else
     {
        *should_fail = EINA_TRUE;
        Eina_Error err;
        VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR);
        eina_value_get(&v, &err);
        printf("Received error from the previous future - value: %s. Send success\n",
               eina_error_msg_get(err));
     }
   return new_v;
}

static void
_alternate_error(Efl_Loop *loop)
{
   static Eina_Bool should_fail = EINA_TRUE;

   eina_future_chain(_str_future_get(loop),
                     {.cb = _alternate_error_cb, .data = &should_fail},
                     {.cb = _alternate_error_cb, .data = &should_fail},
                     {.cb = _alternate_error_cb, .data = &should_fail},
                     {.cb = _alternate_error_cb, .data = &should_fail});

}

static Eina_Value
_simple_err(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR);
   return v;
}

static void
_simple(Efl_Loop *loop)
{
   eina_future_chain(_str_future_get(loop),
                     eina_future_cb_console("Expecting the following message: "DEFAULT_MSG ". Got: ", NULL),
                     { .cb = _simple_ok, .data = NULL });
   eina_future_chain(_fail_future_get(loop),
                     eina_future_cb_console("Expectig network down error. Got: ", NULL),
                     { .cb = _simple_err, .data = NULL });
}

static Eina_Value
_chain_no_errors_cb(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   int count;
   Eina_Value new_v;

   eina_value_setup(&new_v, EINA_VALUE_TYPE_INT);
   if (!v.type)
     count = 1;
   else
     {
        VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_INT);
        eina_value_get(&v, &count);
     }
   eina_value_set(&new_v, count * 2);
   return new_v;
}

static void
_chain_no_errors(Efl_Loop *loop)
{
   eina_future_chain(_int_future_get(loop, 0),
                     eina_future_cb_console("Expecting no value. Got: ", NULL),
                     {.cb = _chain_no_errors_cb, .data = NULL},
                     eina_future_cb_console("Expecting number 2. Got: ", NULL),
                     {.cb = _chain_no_errors_cb, .data = NULL},
                     eina_future_cb_console("Expecting number 4. Got: ", NULL),
                     {.cb = _chain_no_errors_cb, .data = NULL},
                     eina_future_cb_console("Expecting number 8. Got: ", NULL),
                     {.cb = _chain_no_errors_cb, .data = NULL},
                     eina_future_cb_console("Expecting number 16. Got: ", NULL));
}

static Eina_Value
_chain_with_error_cb(void *data EINA_UNUSED, const Eina_Value v EINA_UNUSED, const Eina_Future *dead_future EINA_UNUSED)
{
   Eina_Value err;
   eina_value_setup(&err, EINA_VALUE_TYPE_ERROR);
   eina_value_set(&err, E2BIG);
   return err;
}

static void
_chain_with_error(Efl_Loop *loop)
{
   eina_future_chain(_int_future_get(loop, 0),
                     { _chain_with_error_cb, NULL },
                     eina_future_cb_console("Expecting argument list too long. Got: ", NULL),
                     { .cb = _simple_err, .data = NULL });
}

static Eina_Value
_delayed_resolve(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Inner_Promise_Ctx *ctx = data;
   Eina_Value new_v;
   eina_value_setup(&new_v, EINA_VALUE_TYPE_STRING);
   eina_value_set(&new_v, "Hello from inner future");
   eina_promise_resolve(ctx->promise, new_v);
   free(ctx);
   return v;
}

static Eina_Value
_delayed_reject(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   Inner_Promise_Ctx *ctx = data;
   eina_promise_reject(ctx->promise, ENETDOWN);
   free(ctx);
   return v;
}

static void
_inner_promise_cancel(void *data, const Eina_Promise *dead EINA_UNUSED)
{
   Inner_Promise_Ctx *ctx = data;
   eina_future_cancel(ctx->future);
   free(ctx);
}

static Eina_Value
_chain_inner_cb(void *data, const Eina_Value v, const Eina_Future *dead_future)
{
   Inner_Promise_Ctx *ctx;
   Eina_Value r;
   int s = 0;

   eina_value_int_get(&v, &s);

   ctx = calloc(1, sizeof(Inner_Promise_Ctx));
   EINA_SAFETY_ON_NULL_GOTO(ctx, err);
   ctx->promise = eina_promise_continue_new(dead_future, _inner_promise_cancel, ctx);
   EINA_SAFETY_ON_NULL_GOTO(ctx->promise, err);

   printf("Creating a new promise inside the future cb\n");
   ctx->future = eina_future_then(_int_future_get(data, 0),
                                  !s ? _delayed_resolve : _delayed_reject,
                                  ctx);
   return eina_promise_as_value(ctx->promise);

 err:
   eina_value_setup(&r, EINA_VALUE_TYPE_ERROR);
   eina_value_set(&r, ENOMEM);
   free(ctx);
   return r;
}

static Eina_Value
_chain_inner_last_cb(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_STRING);
   return v;
}

static void
_chain_inner_no_errors(Efl_Loop *loop)
{
   eina_future_chain(_int_future_get(loop, 0),
                     { .cb = _chain_inner_cb, .data = loop },
                     eina_future_cb_console("Expecting message: 'Hello from inner future'. Got: ", NULL),
                     { .cb = _chain_inner_last_cb, .data = NULL });
}

static Eina_Value
_err_inner_chain(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR);
   efl_loop_quit(data, EINA_VALUE_EMPTY);
   return v;
}

static void
_chain_inner_errors(Efl_Loop *loop)
{

   eina_future_chain(_int_future_get(loop, 1),
                     { .cb = _chain_inner_cb, .data = loop },
                     eina_future_cb_console("Expection network down error. Got: ", NULL),
                     { .cb = _err_inner_chain, .data = loop });
}

static Eina_Value
_canceled_cb(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
{
   VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR);
   return v;
}

static void
_future_cancel(Efl_Loop *loop)
{
   Eina_Future *f;

   f = eina_future_chain(_int_future_get(loop, 0),
                         eina_future_cb_console("Expecting cancelled operation error.  Got: ", NULL),
                         { .cb = _canceled_cb, .data = NULL },
                         eina_future_cb_console("Expecting cancelled operation error.  Got: ", NULL),
                         { .cb = _canceled_cb, .data = NULL },
                         eina_future_cb_console("Expecting cancelled operation error.  Got: ", NULL),
                         { .cb = _canceled_cb, .data = NULL },
                         eina_future_cb_console("Expecting cancelled operation error.  Got: ", NULL),
                         { .cb = _canceled_cb, .data = NULL },
                         eina_future_cb_console("Expecting cancelled operation error.  Got: ", NULL),
                         { .cb = _canceled_cb, .data = NULL },
                         eina_future_cb_console("Expecting cancelled operation error.  Got: ", NULL));
   eina_future_cancel(f);
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED,
         const Efl_Event *ev)
{
   _simple(ev->object);
   _alternate_error(ev->object);
   _chain_no_errors(ev->object);
   _chain_with_error(ev->object);
   _chain_inner_no_errors(ev->object);
   _chain_inner_errors(ev->object);
   _future_cancel(ev->object);
}

EFL_MAIN();