annotate Implab/PromiseExtensions.cs @ 117:8beee0d11de6 v2

pool refactoring
author cin
date Tue, 23 Dec 2014 19:48:47 +0300
parents 1a8426e6e895
children 2573b562e328
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 }
110
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
75
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
76 static void CancelCallback(object cookie) {
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
77 ((ICancellable)cookie).Cancel();
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
78 }
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
79
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
80 /// <summary>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
81 /// Cancells promise after the specified timeout is elapsed.
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
82 /// </summary>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
83 /// <param name="that">The promise to cancel on timeout.</param>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
84 /// <param name="milliseconds">The timeout in milliseconds.</param>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
85 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
86 public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise {
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
87 Safe.ArgumentNotNull(that, "that");
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
88 var timer = new Timer(CancelCallback, that, milliseconds, -1);
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
89 that.On(timer.Dispose, PromiseEventType.All);
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
90 return that;
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
91 }
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
92
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
93 #if NET_4_5
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
94
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
95 public static Task<T> GetTask<T>(this IPromise<T> that) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
96 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
97 var tcs = new TaskCompletionSource<T>();
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
98
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 101
diff changeset
99 that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
100
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
101 return tcs.Task;
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
102 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
103
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
104 #endif
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
105 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
106 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
107