annotate Implab/PromiseExtensions.cs @ 102:b4c4d65b7def v2

code cleaunp
author cin
date Fri, 07 Nov 2014 09:36:12 +0300
parents 279e226dffdd
children 5f10d54b45df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
1 using System.Threading;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
2 using System;
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
3 #if NET_4_5
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
4 using System.Threading.Tasks;
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
5 #endif
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
6
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
7 namespace Implab {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
8 public static class PromiseExtensions {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
9 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
10 Safe.ArgumentNotNull(that, "that");
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
11 var context = SynchronizationContext.Current;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
12 if (context == null)
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
13 return that;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
14
101
279e226dffdd code cleanup
cin
parents: 76
diff changeset
15 var p = new SyncContextPromise<T>(context, that);
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
16
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
17 that.Last(
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
18 p.Resolve,
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
19 p.Reject,
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
20 p.Cancel
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
21 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
22 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
23 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
24
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
25 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
26 Safe.ArgumentNotNull(that, "that");
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
27 Safe.ArgumentNotNull(context, "context");
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
28
101
279e226dffdd code cleanup
cin
parents: 76
diff changeset
29 var p = new SyncContextPromise<T>(context, that);
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
30
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
31 that.Last(
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
32 p.Resolve,
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
33 p.Reject,
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
34 p.Cancel
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
35 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
36 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
37 }
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
38
101
279e226dffdd code cleanup
cin
parents: 76
diff changeset
39 /// <summary>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
40 /// Ensures the dispatched.
279e226dffdd code cleanup
cin
parents: 76
diff changeset
41 /// </summary>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
42 /// <returns>The dispatched.</returns>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
43 /// <param name="that">That.</param>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
44 /// <param name="head">Head.</param>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
45 /// <param name="cleanup">Cleanup.</param>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
46 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
47 /// <typeparam name="T">The 2nd type parameter.</typeparam>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
48 public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{
279e226dffdd code cleanup
cin
parents: 76
diff changeset
49 Safe.ArgumentNotNull(that, "that");
279e226dffdd code cleanup
cin
parents: 76
diff changeset
50 Safe.ArgumentNotNull(head, "head");
279e226dffdd code cleanup
cin
parents: 76
diff changeset
51
279e226dffdd code cleanup
cin
parents: 76
diff changeset
52 that.Last(null,null,() => head.Last(cleanup));
279e226dffdd code cleanup
cin
parents: 76
diff changeset
53
279e226dffdd code cleanup
cin
parents: 76
diff changeset
54 return that;
279e226dffdd code cleanup
cin
parents: 76
diff changeset
55 }
279e226dffdd code cleanup
cin
parents: 76
diff changeset
56
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
57 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
58 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
59 Safe.ArgumentNotNull(callback, "callback");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
60 return ar => {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
61 try {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
62 that.Resolve(callback(ar));
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
63 } catch (Exception err) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
64 that.Reject(err);
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
65 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
66 };
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
67 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
68
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
69 #if NET_4_5
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
70
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
71 public static Task<T> GetTask<T>(this IPromise<T> that) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
72 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
73 var tcs = new TaskCompletionSource<T>();
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
74
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
75 that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
76
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
77 return tcs.Task;
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
78 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
79
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
80 #endif
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
81 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
82 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
83