comparison Implab/PromiseExtensions.cs @ 185:822aab37b107 ref20160224

runnable component, work in progress
author cin
date Mon, 18 Apr 2016 16:41:17 +0300
parents ec91a6dfa5b3
children 8200ab154c8a
comparison
equal deleted inserted replaced
184:d6a8cba73acc 185:822aab37b107
1 using System.Threading; 1 using System.Threading;
2 using System; 2 using System;
3 using Implab.Diagnostics; 3 using Implab.Diagnostics;
4 using System.Collections.Generic; 4 using System.Collections.Generic;
5 5
6
7 #if NET_4_5
8 using System.Threading.Tasks;
9 #endif
10
11 namespace Implab { 6 namespace Implab {
12 public static class PromiseExtensions { 7 public static class PromiseExtensions {
13 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { 8 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
14 Safe.ArgumentNotNull(that, "that"); 9 Safe.ArgumentNotNull(that, "that");
15 var context = SynchronizationContext.Current; 10 var context = SynchronizationContext.Current;
16 if (context == null) 11 if (context == null)
17 return that; 12 return that;
18 13
19 var p = new SyncContextPromise<T>(context); 14 var p = new SyncContextPromise<T>(context);
20 p.On(that.Cancel, PromiseEventType.Cancelled); 15 p.CancellationRequested(that.Cancel);
21 16
22 that.On( 17 that.On(
23 p.Resolve, 18 p.Resolve,
24 p.Reject, 19 p.Reject,
25 p.Cancel 20 p.CancelOperation
26 ); 21 );
27 return p; 22 return p;
28 } 23 }
29 24
30 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { 25 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
31 Safe.ArgumentNotNull(that, "that"); 26 Safe.ArgumentNotNull(that, "that");
32 Safe.ArgumentNotNull(context, "context"); 27 Safe.ArgumentNotNull(context, "context");
33 28
34 var p = new SyncContextPromise<T>(context); 29 var p = new SyncContextPromise<T>(context);
35 p.On(that.Cancel, PromiseEventType.Cancelled); 30 p.CancellationRequested(that.Cancel);
36
37 31
38 that.On( 32 that.On(
39 p.Resolve, 33 p.Resolve,
40 p.Reject, 34 p.Reject,
41 p.Cancel 35 p.CancelOperation
42 ); 36 );
43 return p; 37 return p;
44 } 38 }
45 39
46 /// <summary> 40 /// <summary>
75 TraceContext.Instance.Leave(); 69 TraceContext.Instance.Leave();
76 } 70 }
77 }; 71 };
78 } 72 }
79 73
80 static void CancelCallback(object cookie) { 74 static void CancelByTimeoutCallback(object cookie) {
81 ((ICancellable)cookie).Cancel(); 75 ((ICancellable)cookie).Cancel(new TimeoutException());
82 } 76 }
83 77
84 /// <summary> 78 /// <summary>
85 /// Cancells promise after the specified timeout is elapsed. 79 /// Cancells promise after the specified timeout is elapsed.
86 /// </summary> 80 /// </summary>
87 /// <param name="that">The promise to cancel on timeout.</param> 81 /// <param name="that">The promise to cancel on timeout.</param>
88 /// <param name="milliseconds">The timeout in milliseconds.</param> 82 /// <param name="milliseconds">The timeout in milliseconds.</param>
89 /// <typeparam name="TPromise">The 1st type parameter.</typeparam> 83 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
90 public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise { 84 public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise {
91 Safe.ArgumentNotNull(that, "that"); 85 Safe.ArgumentNotNull(that, "that");
92 var timer = new Timer(CancelCallback, that, milliseconds, -1); 86 var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1);
93 that.On(timer.Dispose, PromiseEventType.All); 87 that.On(timer.Dispose, PromiseEventType.All);
94 return that; 88 return that;
95 } 89 }
96 90
97 public static IPromise Bundle(this ICollection<IPromise> that) { 91 public static IPromise Bundle(this ICollection<IPromise> that) {
178 public static IPromise Then(this IPromise that, Action success, Action<Exception> error, Action<Exception> cancel) { 172 public static IPromise Then(this IPromise that, Action success, Action<Exception> error, Action<Exception> cancel) {
179 Safe.ArgumentNotNull(that, "that"); 173 Safe.ArgumentNotNull(that, "that");
180 174
181 var d = new ActionTask(success, error, cancel, false); 175 var d = new ActionTask(success, error, cancel, false);
182 that.On(d.Resolve, d.Reject, d.CancelOperation); 176 that.On(d.Resolve, d.Reject, d.CancelOperation);
183 if (success != null) 177 d.CancellationRequested(that.Cancel);
184 d.CancellationRequested(that.Cancel);
185 return d; 178 return d;
186 } 179 }
187 180
188 public static IPromise Then(this IPromise that, Action success, Action<Exception> error) { 181 public static IPromise Then(this IPromise that, Action success, Action<Exception> error) {
189 return Then(that, success, error, null); 182 return Then(that, success, error, null);
196 public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error, Func<Exception, T> cancel) { 189 public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error, Func<Exception, T> cancel) {
197 Safe.ArgumentNotNull(that, "that"); 190 Safe.ArgumentNotNull(that, "that");
198 191
199 var d = new FuncTask<T>(success, error, cancel, false); 192 var d = new FuncTask<T>(success, error, cancel, false);
200 that.On(d.Resolve, d.Reject, d.CancelOperation); 193 that.On(d.Resolve, d.Reject, d.CancelOperation);
201 if (success != null) 194 d.CancellationRequested(that.Cancel);
202 d.CancellationRequested(that.Cancel);
203 return d; 195 return d;
204 } 196 }
205 197
206 public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error) { 198 public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error) {
207 return Then(that, success, error, null); 199 return Then(that, success, error, null);
213 205
214 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) { 206 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) {
215 Safe.ArgumentNotNull(that, "that"); 207 Safe.ArgumentNotNull(that, "that");
216 var d = new FuncTask<T,T2>(success, error, cancel, false); 208 var d = new FuncTask<T,T2>(success, error, cancel, false);
217 that.On(d.Resolve, d.Reject, d.CancelOperation); 209 that.On(d.Resolve, d.Reject, d.CancelOperation);
218 if (success != null) 210 d.CancellationRequested(that.Cancel);
219 d.CancellationRequested(that.Cancel);
220 return d; 211 return d;
221 } 212 }
222 213
223 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) { 214 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) {
224 return Then(that, success, error, null); 215 return Then(that, success, error, null);
232 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error, Func<Exception,IPromise> cancel) { 223 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error, Func<Exception,IPromise> cancel) {
233 Safe.ArgumentNotNull(that, "that"); 224 Safe.ArgumentNotNull(that, "that");
234 225
235 var d = new ActionChainTask(success, error, cancel, false); 226 var d = new ActionChainTask(success, error, cancel, false);
236 that.On(d.Resolve, d.Reject, d.CancelOperation); 227 that.On(d.Resolve, d.Reject, d.CancelOperation);
237 if (success != null) 228 d.CancellationRequested(that.Cancel);
238 d.CancellationRequested(that.Cancel);
239 return d; 229 return d;
240 } 230 }
241 231
242 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error) { 232 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error) {
243 return Chain(that, success, error, null); 233 return Chain(that, success, error, null);