annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
1 using System;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
2 using System.Diagnostics;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 namespace Implab {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5 class PromiseHandler {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
6 public static Action<T> Create<T>(Action<T> handler, Deferred next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
7 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
9 return (v) => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11 handler(v);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12 next.Resolve();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
17 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
18
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
19 public static Action<T> Create<T>(Func<T, IPromise> handler, Deferred next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
21
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
22 return (v) => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 next.Resolve(handler(v));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
25 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
26 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
27 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
31 public static Action<T> Create<T, T2>(Func<T, T2> handler, Deferred<T2> next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
34 return (v) => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
35 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
36 next.Resolve(handler(v));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
39 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
41 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
42
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
43 public static Action<T> Create<T, T2>(Func<T, IPromise<T2>> handler, Deferred<T2> next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
44 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
45 return (v) => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
46 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
47 next.Resolve(handler(v));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
48 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
49 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
50 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
51 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
52 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
53
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
54 public static Action Create(Action handler, Deferred next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
55 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
56
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
57 return () => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
58 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
59 handler();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
60 next.Resolve();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
61 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
62 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
63 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
64 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
65 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
66
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
67 public static Action Create(Func<IPromise> handler, Deferred next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
68 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
69
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
70 return () => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
71 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
72 next.Resolve(handler());
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
73 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
74 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
75 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
76 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
77 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
78
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
79 public static Action Create<T2>(Func<T2> handler, Deferred<T2> next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
80 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
81
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
82 return () => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
83 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
84 next.Resolve(handler());
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
85 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
86 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
87 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
88 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
89 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
90
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
91 public static Action Create<T2>(Func<IPromise<T2>> handler, Deferred<T2> next) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
92 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
93 return () => {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
94 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
95 next.Resolve(handler());
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
96 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
97 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
98 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
99 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
100 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
101 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
102 }