comparison Implab/PromiseExtensions.cs @ 249:d82909310094 v3

Implab.Test moved to xunit Complete set of PromiseHelpers (Then, Catch, Finally) Removed obsolete types ICancellable, ICancellationToken
author cin
date Wed, 31 Jan 2018 11:28:38 +0300
parents 5cb4826c2c2a
children 7d52dc684bbd
comparison
equal deleted inserted replaced
248:5cb4826c2c2a 249:d82909310094
6 6
7 namespace Implab { 7 namespace Implab {
8 public static class PromiseExtensions { 8 public static class PromiseExtensions {
9 9
10 public static IPromise Then(this IPromise that, Action fulfilled, Action<Exception> rejected) { 10 public static IPromise Then(this IPromise that, Action fulfilled, Action<Exception> rejected) {
11 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); 11 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
12 that.Then(reaction);
13 return reaction.Promise;
14 }
15
16 public static IPromise Then(this IPromise that, Action fulfilled) {
17 var reaction = PromiseActionReaction.Create(fulfilled, null, Promise.DefaultDispatcher);
12 that.Then(reaction); 18 that.Then(reaction);
13 return reaction.Promise; 19 return reaction.Promise;
14 } 20 }
15 21
16 public static IPromise Then(this IPromise that, Action fulfilled, Func<Exception, IPromise> rejected) { 22 public static IPromise Then(this IPromise that, Action fulfilled, Func<Exception, IPromise> rejected) {
17 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); 23 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
18 that.Then(reaction); 24 that.Then(reaction);
19 return reaction.Promise; 25 return reaction.Promise;
20 } 26 }
21 27
22 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Action<Exception> rejected) { 28 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Action<Exception> rejected) {
23 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); 29 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
30 that.Then(reaction);
31 return reaction.Promise;
32 }
33
34 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled) {
35 var reaction = PromiseActionReaction.Create(fulfilled, null, Promise.DefaultDispatcher);
24 that.Then(reaction); 36 that.Then(reaction);
25 return reaction.Promise; 37 return reaction.Promise;
26 } 38 }
27 39
28 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Func<Exception, IPromise> rejected) { 40 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Func<Exception, IPromise> rejected) {
29 var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); 41 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher);
30 that.Then(reaction); 42 that.Then(reaction);
31 return reaction.Promise; 43 return reaction.Promise;
32 } 44 }
33 45
34 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Action<Exception> rejected) { 46 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Action<Exception> rejected) {
35 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); 47 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
48 that.Then(reaction);
49 return reaction.Promise;
50 }
51
52 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled) {
53 var reaction = PromiseActionReaction<T>.Create(fulfilled, null, Promise.DefaultDispatcher);
36 that.Then(reaction); 54 that.Then(reaction);
37 return reaction.Promise; 55 return reaction.Promise;
38 } 56 }
39 57
40 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Func<Exception, IPromise> rejected) { 58 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Func<Exception, IPromise> rejected) {
41 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); 59 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
42 that.Then(reaction); 60 that.Then(reaction);
43 return reaction.Promise; 61 return reaction.Promise;
44 } 62 }
45 63
46 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Action<Exception> rejected) { 64 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Action<Exception> rejected) {
47 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); 65 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
66 that.Then(reaction);
67 return reaction.Promise;
68 }
69
70 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled) {
71 var reaction = PromiseActionReaction<T>.Create(fulfilled, null, Promise.DefaultDispatcher);
48 that.Then(reaction); 72 that.Then(reaction);
49 return reaction.Promise; 73 return reaction.Promise;
50 } 74 }
51 75
52 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected) { 76 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected) {
53 var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); 77 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
54 that.Then(reaction); 78 that.Then(reaction);
55 return reaction.Promise; 79 return reaction.Promise;
56 } 80 }
57 81
58 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, Tout> rejected) { 82 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, Tout> rejected) {
59 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 83 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
84 that.Then(reaction);
85 return reaction.Promise;
86 }
87
88 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled) {
89 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
60 that.Then(reaction); 90 that.Then(reaction);
61 return reaction.Promise; 91 return reaction.Promise;
62 } 92 }
63 93
64 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) { 94 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
65 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 95 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
66 that.Then(reaction); 96 that.Then(reaction);
67 return reaction.Promise; 97 return reaction.Promise;
68 } 98 }
69 99
70 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) { 100 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) {
71 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 101 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
102 that.Then(reaction);
103 return reaction.Promise;
104 }
105
106 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled) {
107 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
72 that.Then(reaction); 108 that.Then(reaction);
73 return reaction.Promise; 109 return reaction.Promise;
74 } 110 }
75 111
76 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) { 112 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
77 var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 113 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
78 that.Then(reaction); 114 that.Then(reaction);
79 return reaction.Promise; 115 return reaction.Promise;
80 } 116 }
81 117
82 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, Tout> rejected) { 118 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, Tout> rejected) {
83 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 119 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
120 that.Then(reaction);
121 return reaction.Promise;
122 }
123
124 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled) {
125 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
84 that.Then(reaction); 126 that.Then(reaction);
85 return reaction.Promise; 127 return reaction.Promise;
86 } 128 }
87 129
88 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) { 130 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
89 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 131 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
90 that.Then(reaction); 132 that.Then(reaction);
91 return reaction.Promise; 133 return reaction.Promise;
92 } 134 }
93 135
94 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) { 136 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) {
95 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 137 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
138 that.Then(reaction);
139 return reaction.Promise;
140 }
141
142 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled) {
143 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher);
96 that.Then(reaction); 144 that.Then(reaction);
97 return reaction.Promise; 145 return reaction.Promise;
98 } 146 }
99 147
100 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) { 148 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) {
101 var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); 149 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher);
102 that.Then(reaction); 150 that.Then(reaction);
103 return reaction.Promise; 151 return reaction.Promise;
104 } 152 }
105 153
106 public static IPromise Catch(this IPromise that, Action<Exception> rejected) { 154 public static IPromise Catch(this IPromise that, Action<Exception> rejected) {
124 } 172 }
125 173
126 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, IPromise<Tout>> rejected) { 174 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, IPromise<Tout>> rejected) {
127 return Then(that, (Func<Tin, Tout>)null, rejected); 175 return Then(that, (Func<Tin, Tout>)null, rejected);
128 } 176 }
177
178 public static IPromise Finally(this IPromise that, Action final) {
179 return Then(that, final, e => {
180 final();
181 throw e.Rethrow();
182 });
183 }
184
185 public static IPromise Finally(this IPromise that, Func<IPromise> final) {
186 return Then(that, final, e => {
187 final();
188 throw e.Rethrow();
189 });
190 }
191
192 public static IPromise<T> Finally<T>(this IPromise<T> that, Action final) {
193 return Then<T, T>(that, x => {
194 final();
195 return x;
196 }, new Func<Exception, T>(e => {
197 final();
198 throw e.Rethrow();
199 }));
200 }
201
202 public static IPromise<T> Finally<T>(this IPromise<T> that, Func<IPromise> final) {
203 return Then<T, T>(that, x => {
204 return final()
205 .Then(() => x);
206 }, new Func<Exception, IPromise<T>>(e => {
207 return final()
208 .Then(new Func<T>(() => {
209 throw e.Rethrow();
210 }));
211 }));
212 }
213
214
129 } 215 }
130 } 216 }
131 217