1 ///
2 module dpromise.utils;
3 
4 import dpromise.promise, dpromise.async;
5 import deimos.libuv.uv;
6 import dpromise.internal.libuv;
7 
8 
9 void runEventloop() @safe nothrow {
10   () @trusted {
11     uv_run(localLoop, uv_run_mode.UV_RUN_DEFAULT);
12   }();
13 }
14 
15 
16 T runEventloop(T)(scope Promise!T promise) @safe
17 in {
18   assert(promise !is null);
19 } body {
20   static if(!is(T == void)) {
21     T value;
22     Exception exception;
23 
24     promise.then(
25       (v) {
26         value = v;
27         stopEventloop();
28       },
29       (e) {
30         exception = e;
31         stopEventloop();
32       }
33     );
34   } else {
35     Exception exception;
36 
37     promise.then(
38       () => stopEventloop(),
39       (e) {
40         exception = e;
41         stopEventloop();
42       }
43     );
44   }
45 
46   runEventloop();
47   if(exception !is null) throw exception;
48   static if(!is(T == void)) {
49     return value;
50   }
51 }
52 
53 
54 T runEventloop(T)(T delegate() dg)
55 in {
56   assert(dg !is null);
57 } body {
58   return runEventloop(async(dg));
59 }
60 
61 
62 void stopEventloop() @safe nothrow {
63   () @trusted {
64     uv_stop(localLoop);
65   }();
66 }