comparison Implab/PromiseHandler.cs @ 248:5cb4826c2c2a v3

Added awaiters to promises Added static methods to Promise Resolve, Reject, All. Updated promise helpers
author cin
date Tue, 30 Jan 2018 01:37:17 +0300
parents
children d82909310094
comparison
equal deleted inserted replaced
247:fb70574741a1 248:5cb4826c2c2a
1 using System;
2 using System.Diagnostics;
3
4 namespace Implab {
5 class PromiseHandler {
6 public static Action<T> Create<T>(Action<T> handler, Deferred next) {
7 Debug.Assert(handler != null);
8
9 return (v) => {
10 try {
11 handler(v);
12 next.Resolve();
13 } catch (Exception err) {
14 next.Reject(err);
15 }
16 };
17 }
18
19 public static Action<T> Create<T>(Func<T, IPromise> handler, Deferred next) {
20 Debug.Assert(handler != null);
21
22 return (v) => {
23 try {
24 next.Resolve(handler(v));
25 } catch (Exception err) {
26 next.Reject(err);
27 }
28 };
29 }
30
31 public static Action<T> Create<T, T2>(Func<T, T2> handler, Deferred<T2> next) {
32 Debug.Assert(handler != null);
33
34 return (v) => {
35 try {
36 next.Resolve(handler(v));
37 } catch (Exception err) {
38 next.Reject(err);
39 }
40 };
41 }
42
43 public static Action<T> Create<T, T2>(Func<T, IPromise<T2>> handler, Deferred<T2> next) {
44 Debug.Assert(handler != null);
45 return (v) => {
46 try {
47 next.Resolve(handler(v));
48 } catch (Exception err) {
49 next.Reject(err);
50 }
51 };
52 }
53
54 public static Action Create(Action handler, Deferred next) {
55 Debug.Assert(handler != null);
56
57 return () => {
58 try {
59 handler();
60 next.Resolve();
61 } catch (Exception err) {
62 next.Reject(err);
63 }
64 };
65 }
66
67 public static Action Create(Func<IPromise> handler, Deferred next) {
68 Debug.Assert(handler != null);
69
70 return () => {
71 try {
72 next.Resolve(handler());
73 } catch (Exception err) {
74 next.Reject(err);
75 }
76 };
77 }
78
79 public static Action Create<T2>(Func<T2> handler, Deferred<T2> next) {
80 Debug.Assert(handler != null);
81
82 return () => {
83 try {
84 next.Resolve(handler());
85 } catch (Exception err) {
86 next.Reject(err);
87 }
88 };
89 }
90
91 public static Action Create<T2>(Func<IPromise<T2>> handler, Deferred<T2> next) {
92 Debug.Assert(handler != null);
93 return () => {
94 try {
95 next.Resolve(handler());
96 } catch (Exception err) {
97 next.Reject(err);
98 }
99 };
100 }
101 }
102 }