annotate Implab/PromiseExtensions.cs @ 109:1b7ebcc52e5a v2

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