annotate Implab/PromiseExtensions.cs @ 204:cbb0bd8fc0d1 v2

Fixed broken Implab.Diagnostics.Interactive
author cin
date Mon, 24 Oct 2016 11:24:45 +0300
parents 822aab37b107
children 8200ab154c8a
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;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
4 using System.Collections.Generic;
109
1b7ebcc52e5a minor fixes
cin
parents: 104
diff changeset
5
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
6 namespace Implab {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
7 public static class PromiseExtensions {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
8 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
9 Safe.ArgumentNotNull(that, "that");
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
10 var context = SynchronizationContext.Current;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
11 if (context == null)
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
12 return that;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
13
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
14 var p = new SyncContextPromise<T>(context);
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
15 p.CancellationRequested(that.Cancel);
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
16
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 101
diff changeset
17 that.On(
76
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,
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
20 p.CancelOperation
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
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
29 var p = new SyncContextPromise<T>(context);
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
30 p.CancellationRequested(that.Cancel);
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
31
104
5f10d54b45df renamed Promise.Last -> Promise.On
cin
parents: 101
diff changeset
32 that.On(
76
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
33 p.Resolve,
c761fc982e1d Refactoring of the IPromise<T> interface
cin
parents: 75
diff changeset
34 p.Reject,
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
35 p.CancelOperation
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
36 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
37 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
38 }
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
39
101
279e226dffdd code cleanup
cin
parents: 76
diff changeset
40 /// <summary>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
41 /// Ensures the dispatched.
279e226dffdd code cleanup
cin
parents: 76
diff changeset
42 /// </summary>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
43 /// <returns>The dispatched.</returns>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
44 /// <param name="that">That.</param>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
45 /// <param name="head">Head.</param>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
46 /// <param name="cleanup">Cleanup.</param>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
47 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
48 /// <typeparam name="T">The 2nd type parameter.</typeparam>
279e226dffdd code cleanup
cin
parents: 76
diff changeset
49 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
50 Safe.ArgumentNotNull(that, "that");
279e226dffdd code cleanup
cin
parents: 76
diff changeset
51 Safe.ArgumentNotNull(head, "head");
279e226dffdd code cleanup
cin
parents: 76
diff changeset
52
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
53 that.On(() => head.On(cleanup), PromiseEventType.Cancelled);
101
279e226dffdd code cleanup
cin
parents: 76
diff changeset
54
279e226dffdd code cleanup
cin
parents: 76
diff changeset
55 return that;
279e226dffdd code cleanup
cin
parents: 76
diff changeset
56 }
279e226dffdd code cleanup
cin
parents: 76
diff changeset
57
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
58 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
59 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
60 Safe.ArgumentNotNull(callback, "callback");
109
1b7ebcc52e5a minor fixes
cin
parents: 104
diff changeset
61 var op = TraceContext.Instance.CurrentOperation;
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
62 return ar => {
109
1b7ebcc52e5a minor fixes
cin
parents: 104
diff changeset
63 TraceContext.Instance.EnterLogicalOperation(op,false);
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
64 try {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
65 that.Resolve(callback(ar));
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
66 } catch (Exception err) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
67 that.Reject(err);
109
1b7ebcc52e5a minor fixes
cin
parents: 104
diff changeset
68 } finally {
1b7ebcc52e5a minor fixes
cin
parents: 104
diff changeset
69 TraceContext.Instance.Leave();
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
70 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
71 };
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
72 }
110
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
73
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
74 static void CancelByTimeoutCallback(object cookie) {
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
75 ((ICancellable)cookie).Cancel(new TimeoutException());
110
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
76 }
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
77
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
78 /// <summary>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
79 /// Cancells promise after the specified timeout is elapsed.
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
80 /// </summary>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
81 /// <param name="that">The promise to cancel on timeout.</param>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
82 /// <param name="milliseconds">The timeout in milliseconds.</param>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
83 /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
84 public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise {
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
85 Safe.ArgumentNotNull(that, "that");
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
86 var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1);
110
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
87 that.On(timer.Dispose, PromiseEventType.All);
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
88 return that;
1a8426e6e895 added promise timeout helper
cin
parents: 109
diff changeset
89 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
90
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
91 public static IPromise Bundle(this ICollection<IPromise> that) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
92 Safe.ArgumentNotNull(that, "that");
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
93
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
94 int count = that.Count;
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
95 int errors = 0;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
96 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
97
136
e9e7940c7d98 shared locks + tests
cin
parents: 124
diff changeset
98 if (count == 0) {
e9e7940c7d98 shared locks + tests
cin
parents: 124
diff changeset
99 medium.Resolve();
e9e7940c7d98 shared locks + tests
cin
parents: 124
diff changeset
100 return medium;
e9e7940c7d98 shared locks + tests
cin
parents: 124
diff changeset
101 }
e9e7940c7d98 shared locks + tests
cin
parents: 124
diff changeset
102
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
103 medium.On(() => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
104 foreach(var p2 in that)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
105 p2.Cancel();
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
106 }, PromiseEventType.ErrorOrCancel);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
107
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
108 foreach (var p in that)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
109 p.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
110 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
111 if (Interlocked.Decrement(ref count) == 0)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
112 medium.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
113 },
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
114 error => {
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
115 if (Interlocked.Increment(ref errors) == 1)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
116 medium.Reject(
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
117 new Exception("The dependency promise is failed", error)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
118 );
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
119 },
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
120 reason => {
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
121 if (Interlocked.Increment(ref errors) == 1)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
122 medium.Cancel(
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
123 new Exception("The dependency promise is cancelled")
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
124 );
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
125 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
126 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
127
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
128 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 110
diff changeset
129 }
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
130
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
131 public static IPromise<T[]> Bundle<T>(this ICollection<IPromise<T>> that) {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
132 Safe.ArgumentNotNull(that, "that");
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
133
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
134 int count = that.Count;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
135 int errors = 0;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
136 var medium = new Promise<T[]>();
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
137 var results = new T[that.Count];
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
138
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
139 medium.On(() => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
140 foreach(var p2 in that)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
141 p2.Cancel();
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
142 }, PromiseEventType.ErrorOrCancel);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
143
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
144 int i = 0;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
145 foreach (var p in that) {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
146 var idx = i;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
147 p.On(
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
148 x => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
149 results[idx] = x;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
150 if (Interlocked.Decrement(ref count) == 0)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
151 medium.Resolve(results);
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
152 },
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
153 error => {
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
154 if (Interlocked.Increment(ref errors) == 1)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
155 medium.Reject(
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
156 new Exception("The dependency promise is failed", error)
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
157 );
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
158 },
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
159 reason => {
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
160 if (Interlocked.Increment(ref errors) == 1)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
161 medium.Cancel(
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 136
diff changeset
162 new Exception("The dependency promise is cancelled", reason)
124
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
163 );
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
164 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
165 );
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
166 i++;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
167 }
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
168
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
169 return medium;
a336cb13c6a9 major update, added Drain mathod to AsyncQueue class
cin
parents: 119
diff changeset
170 }
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
171
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
172 public static IPromise Then(this IPromise that, Action success, Action<Exception> error, Action<Exception> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
173 Safe.ArgumentNotNull(that, "that");
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
174
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
175 var d = new ActionTask(success, error, cancel, false);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
176 that.On(d.Resolve, d.Reject, d.CancelOperation);
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
177 d.CancellationRequested(that.Cancel);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
178 return d;
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
179 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
180
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
181 public static IPromise Then(this IPromise that, Action success, Action<Exception> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
182 return Then(that, success, error, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
183 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
184
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
185 public static IPromise Then(this IPromise that, Action success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
186 return Then(that, success, null, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
187 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
188
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
189 public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error, Func<Exception, T> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
190 Safe.ArgumentNotNull(that, "that");
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
191
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
192 var d = new FuncTask<T>(success, error, cancel, false);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
193 that.On(d.Resolve, d.Reject, d.CancelOperation);
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
194 d.CancellationRequested(that.Cancel);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
195 return d;
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
196 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
197
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
198 public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
199 return Then(that, success, error, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
200 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
201
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
202 public static IPromise<T> Then<T>(this IPromise that, Func<T> success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
203 return Then(that, success, null, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
204 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
205
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
206 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
207 Safe.ArgumentNotNull(that, "that");
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
208 var d = new FuncTask<T,T2>(success, error, cancel, false);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
209 that.On(d.Resolve, d.Reject, d.CancelOperation);
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
210 d.CancellationRequested(that.Cancel);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
211 return d;
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
212 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
213
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
214 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
215 return Then(that, success, error, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
216 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
217
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
218 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
219 return Then(that, success, null, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
220 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
221
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
222 #region chain traits
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
223 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error, Func<Exception,IPromise> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
224 Safe.ArgumentNotNull(that, "that");
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
225
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
226 var d = new ActionChainTask(success, error, cancel, false);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
227 that.On(d.Resolve, d.Reject, d.CancelOperation);
185
822aab37b107 runnable component, work in progress
cin
parents: 151
diff changeset
228 d.CancellationRequested(that.Cancel);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
229 return d;
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
230 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
231
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
232 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
233 return Chain(that, success, error, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
234 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
235
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
236 public static IPromise Chain(this IPromise that, Func<IPromise> success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
237 return Chain(that, success, null, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
238 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
239
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
240 public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success, Func<Exception, IPromise<T>> error, Func<Exception, IPromise<T>> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
241 Safe.ArgumentNotNull(that, "that");
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
242
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
243 var d = new FuncChainTask<T>(success, error, cancel, false);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
244 that.On(d.Resolve, d.Reject, d.CancelOperation);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
245 if (success != null)
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
246 d.CancellationRequested(that.Cancel);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
247 return d;
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
248 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
249
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
250 public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success, Func<Exception, IPromise<T>> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
251 return Chain(that, success, error, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
252 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
253
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
254 public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
255 return Chain(that, success, null, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
256 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
257
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
258 public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success, Func<Exception, IPromise<T2>> error, Func<Exception, IPromise<T2>> cancel) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
259 Safe.ArgumentNotNull(that, "that");
149
eb793fbbe4ea fixed promises cancellation
cin
parents: 145
diff changeset
260 var d = new FuncChainTask<T,T2>(success, error, cancel, false);
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
261 that.On(d.Resolve, d.Reject, d.CancelOperation);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
262 if (success != null)
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
263 d.CancellationRequested(that.Cancel);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
264 return d;
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
265 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
266
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
267 public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success, Func<Exception, IPromise<T2>> error) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
268 return Chain(that, success, error, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
269 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
270
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
271 public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success) {
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
272 return Chain(that, success, null, null);
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
273 }
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
274
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
275 #endregion
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 138
diff changeset
276
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
277
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
278 #if NET_4_5
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
279
151
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
280 public static PromiseAwaiter<T> GetAwaiter<T>(this IPromise<T> that) {
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
281 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
282
151
ec91a6dfa5b3 Added support for 'await' operator to promises
cin
parents: 149
diff changeset
283 return new PromiseAwaiter<T>(that);
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
284 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
285
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
286 #endif
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
287 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
288 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
289