comparison Implab/PromiseExtensions.cs @ 209:a867536c68fc v2

Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise
author cin
date Wed, 16 Nov 2016 03:06:08 +0300
parents 7d07503621fe
children 5cb4826c2c2a
comparison
equal deleted inserted replaced
208:7d07503621fe 209:a867536c68fc
179 /// <param name="that"></param> 179 /// <param name="that"></param>
180 /// <param name="cleanup">A callback used to cleanup already resolved promises in case of an error</param> 180 /// <param name="cleanup">A callback used to cleanup already resolved promises in case of an error</param>
181 /// <returns></returns> 181 /// <returns></returns>
182 public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that, Action<T> cleanup) { 182 public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that, Action<T> cleanup) {
183 Safe.ArgumentNotNull(that, "that"); 183 Safe.ArgumentNotNull(that, "that");
184 184
185 int count = that.Count; 185 int count = that.Count;
186 186
187 if (count == 0) 187 if (count == 0)
188 return Promise<T[]>.FromResult(new T[0]); 188 return Promise<T[]>.FromResult(new T[0]);
189 189
261 return Then(that, success, null, null); 261 return Then(that, success, null, null);
262 } 262 }
263 263
264 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) { 264 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) {
265 Safe.ArgumentNotNull(that, "that"); 265 Safe.ArgumentNotNull(that, "that");
266 Safe.ArgumentNotNull(success, "success");
267
266 var d = new FuncTask<T, T2>(success, error, cancel, false); 268 var d = new FuncTask<T, T2>(success, error, cancel, false);
267 that.On(d.Resolve, d.Reject, d.CancelOperation); 269 that.On(d.Resolve, d.Reject, d.CancelOperation);
268 d.CancellationRequested(that.Cancel); 270 d.CancellationRequested(that.Cancel);
269 return d; 271 return d;
270 } 272 }
274 var d = new FuncTask<T, T>( 276 var d = new FuncTask<T, T>(
275 x => { 277 x => {
276 success(x); 278 success(x);
277 return x; 279 return x;
278 }, 280 },
279 error, 281 error,
280 cancel, 282 cancel,
281 false 283 false
282 ); 284 );
283 that.On(d.Resolve, d.Reject, d.CancelOperation); 285 that.On(d.Resolve, d.Reject, d.CancelOperation);
284 d.CancellationRequested(that.Cancel); 286 d.CancellationRequested(that.Cancel);
285 return d; 287 return d;
425 return Chain(that, success, null, null); 427 return Chain(that, success, null, null);
426 } 428 }
427 429
428 #endregion 430 #endregion
429 431
432 public static IPromise<T2> Guard<T, T2>(this IPromise<T> that, Func<IPromise<T>, IPromise<T2>> continuation, Action<T> cleanup) {
433 Safe.ArgumentNotNull(that, "that");
434 Safe.ArgumentNotNull(continuation, "continuation");
435 return continuation(that).Error((err) => {
436 that.On(cleanup);
437 }, true);
438 }
430 439
431 #if NET_4_5 440 #if NET_4_5
432 441
433 public static PromiseAwaiter<T> GetAwaiter<T>(this IPromise<T> that) { 442 public static PromiseAwaiter<T> GetAwaiter<T>(this IPromise<T> that) {
434 Safe.ArgumentNotNull(that, "that"); 443 Safe.ArgumentNotNull(that, "that");
438 447
439 public static PromiseAwaiter GetAwaiter(this IPromise that) { 448 public static PromiseAwaiter GetAwaiter(this IPromise that) {
440 Safe.ArgumentNotNull(that, "that"); 449 Safe.ArgumentNotNull(that, "that");
441 450
442 return new PromiseAwaiter(that); 451 return new PromiseAwaiter(that);
452 }
453
454 public static IPromise BoundCancellationToken(this IPromise that, CancellationToken ct) {
455 Safe.ArgumentNotNull(that, "that");
456 ct.Register(that.Cancel);
457 return that.Then(null, null, (err) => {
458 ct.ThrowIfCancellationRequested();
459 throw new PromiseTransientException(err);
460 });
461 }
462
463 public static IPromise<T> BoundCancellationToken<T>(this IPromise<T> that, CancellationToken ct) {
464 Safe.ArgumentNotNull(that, "that");
465 ct.Register(that.Cancel);
466 return that.Then(null, null, (err) => {
467 ct.ThrowIfCancellationRequested();
468 throw new PromiseTransientException(err);
469 });
443 } 470 }
444 471
445 #endif 472 #endif
446 } 473 }
447 } 474 }