comparison Implab/PromiseExtensions.cs @ 101:279e226dffdd v2

code cleanup added EnsureDispatched extension
author cin
date Thu, 06 Nov 2014 20:03:19 +0300
parents c761fc982e1d
children 5f10d54b45df
comparison
equal deleted inserted replaced
100:673947ce458a 101:279e226dffdd
10 Safe.ArgumentNotNull(that, "that"); 10 Safe.ArgumentNotNull(that, "that");
11 var context = SynchronizationContext.Current; 11 var context = SynchronizationContext.Current;
12 if (context == null) 12 if (context == null)
13 return that; 13 return that;
14 14
15 var p = new SyncContextPromise<T>(context, that, true); 15 var p = new SyncContextPromise<T>(context, that);
16 16
17 that.Last( 17 that.Last(
18 p.Resolve, 18 p.Resolve,
19 p.Reject, 19 p.Reject,
20 p.Cancel 20 p.Cancel
24 24
25 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) {
26 Safe.ArgumentNotNull(that, "that"); 26 Safe.ArgumentNotNull(that, "that");
27 Safe.ArgumentNotNull(context, "context"); 27 Safe.ArgumentNotNull(context, "context");
28 28
29 var p = new SyncContextPromise<T>(context, that, true); 29 var p = new SyncContextPromise<T>(context, that);
30 30
31 that.Last( 31 that.Last(
32 p.Resolve, 32 p.Resolve,
33 p.Reject, 33 p.Reject,
34 p.Cancel 34 p.Cancel
35 ); 35 );
36 return p; 36 return p;
37 }
38
39 /// <summary>
40 /// Ensures the dispatched.
41 /// </summary>
42 /// <returns>The dispatched.</returns>
43 /// <param name="that">That.</param>
44 /// <param name="head">Head.</param>
45 /// <param name="cleanup">Cleanup.</param>
46 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
47 /// <typeparam name="T">The 2nd type parameter.</typeparam>
48 public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{
49 Safe.ArgumentNotNull(that, "that");
50 Safe.ArgumentNotNull(head, "head");
51
52 that.Last(null,null,() => head.Last(cleanup));
53
54 return that;
37 } 55 }
38 56
39 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { 57 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
40 Safe.ArgumentNotNull(that, "that"); 58 Safe.ArgumentNotNull(that, "that");
41 Safe.ArgumentNotNull(callback, "callback"); 59 Safe.ArgumentNotNull(callback, "callback");