Mercurial > pub > ImplabNet
annotate Implab/PromiseExtensions.cs @ 260:547a2fc0d93e v3 v3.0.6
minor fixes
author | cin |
---|---|
date | Fri, 13 Apr 2018 19:14:59 +0300 |
parents | 7d52dc684bbd |
children |
rev | line source |
---|---|
72 | 1 using System.Threading; |
75 | 2 using System; |
109 | 3 using Implab.Diagnostics; |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
4 using System.Collections.Generic; |
248 | 5 using System.Linq; |
6 | |
72 | 7 namespace Implab { |
8 public static class PromiseExtensions { | |
9 | |
248 | 10 public static IPromise Then(this IPromise that, Action fulfilled, Action<Exception> rejected) { |
249 | 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); | |
248 | 18 that.Then(reaction); |
19 return reaction.Promise; | |
101 | 20 } |
21 | |
248 | 22 public static IPromise Then(this IPromise that, Action fulfilled, Func<Exception, IPromise> rejected) { |
249 | 23 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 24 that.Then(reaction); |
25 return reaction.Promise; | |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
26 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
27 |
248 | 28 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Action<Exception> rejected) { |
249 | 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); | |
248 | 36 that.Then(reaction); |
37 return reaction.Promise; | |
75 | 38 } |
110 | 39 |
248 | 40 public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Func<Exception, IPromise> rejected) { |
249 | 41 var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 42 that.Then(reaction); |
43 return reaction.Promise; | |
110 | 44 } |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
45 |
248 | 46 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Action<Exception> rejected) { |
249 | 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); | |
248 | 54 that.Then(reaction); |
55 return reaction.Promise; | |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
185
diff
changeset
|
56 } |
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
185
diff
changeset
|
57 |
248 | 58 public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Func<Exception, IPromise> rejected) { |
249 | 59 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 60 that.Then(reaction); |
61 return reaction.Promise; | |
208
7d07503621fe
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents:
207
diff
changeset
|
62 } |
7d07503621fe
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents:
207
diff
changeset
|
63 |
248 | 64 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Action<Exception> rejected) { |
249 | 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); | |
248 | 72 that.Then(reaction); |
73 return reaction.Promise; | |
124 | 74 } |
145 | 75 |
248 | 76 public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected) { |
249 | 77 var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 78 that.Then(reaction); |
79 return reaction.Promise; | |
80 } | |
145 | 81 |
248 | 82 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, Tout> rejected) { |
249 | 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); | |
248 | 90 that.Then(reaction); |
91 return reaction.Promise; | |
145 | 92 } |
93 | |
248 | 94 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) { |
249 | 95 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 96 that.Then(reaction); |
97 return reaction.Promise; | |
145 | 98 } |
99 | |
248 | 100 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) { |
249 | 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); | |
248 | 108 that.Then(reaction); |
109 return reaction.Promise; | |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
110 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
111 |
248 | 112 public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) { |
249 | 113 var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 114 that.Then(reaction); |
115 return reaction.Promise; | |
145 | 116 } |
117 | |
248 | 118 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, Tout> rejected) { |
249 | 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); | |
248 | 126 that.Then(reaction); |
127 return reaction.Promise; | |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
128 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
129 |
248 | 130 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) { |
249 | 131 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 132 that.Then(reaction); |
133 return reaction.Promise; | |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
134 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
135 |
248 | 136 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) { |
249 | 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); | |
248 | 144 that.Then(reaction); |
145 return reaction.Promise; | |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
146 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
147 |
248 | 148 public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) { |
249 | 149 var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); |
248 | 150 that.Then(reaction); |
151 return reaction.Promise; | |
145 | 152 } |
153 | |
248 | 154 public static IPromise Catch(this IPromise that, Action<Exception> rejected) { |
155 return Then(that, null, rejected); | |
145 | 156 } |
157 | |
248 | 158 public static IPromise Catch(this IPromise that, Func<Exception, IPromise> rejected) { |
159 return Then(that, null, rejected); | |
145 | 160 } |
161 | |
248 | 162 public static IPromise<Tout> Catch<Tout>(this IPromise that, Func<Exception, Tout> rejected) { |
163 return Then(that, (Func<Tout>)null, rejected); | |
145 | 164 } |
165 | |
248 | 166 public static IPromise<Tout> Catch<Tout>(this IPromise that, Func<Exception, IPromise<Tout>> rejected) { |
167 return Then(that, (Func<Tout>)null, rejected); | |
168 } | |
75 | 169 |
248 | 170 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, Tout> rejected) { |
171 return Then(that, (Func<Tin, Tout>)null, rejected); | |
172 } | |
208
7d07503621fe
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents:
207
diff
changeset
|
173 |
248 | 174 public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, IPromise<Tout>> rejected) { |
175 return Then(that, (Func<Tin, Tout>)null, rejected); | |
176 } | |
249 | 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 | |
259 | 214 public static PromiseAwaiter GetAwaiter(this IPromise that) { |
215 Safe.ArgumentNotNull(that, nameof(that)); | |
216 return new PromiseAwaiter(that); | |
217 } | |
218 | |
219 public static PromiseAwaiter<T> GetAwaiter<T>(this IPromise<T> that) { | |
220 Safe.ArgumentNotNull(that, nameof(that)); | |
221 return new PromiseAwaiter<T>(that); | |
222 } | |
249 | 223 |
72 | 224 } |
225 } | |
226 |