annotate Implab/PromiseExtensions.cs @ 94:a43745f81f10 v2

minor fixes
author cin
date Thu, 23 Oct 2014 17:50:09 +0400
parents c761fc982e1d
children 279e226dffdd
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
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
15 var p = new SyncContextPromise<T>(context, that, true);
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
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
29 var p = new SyncContextPromise<T>(context, that, true);
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
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
39 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
40 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
41 Safe.ArgumentNotNull(callback, "callback");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
42 return ar => {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
43 try {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
44 that.Resolve(callback(ar));
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
45 } catch (Exception err) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
46 that.Reject(err);
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
47 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
48 };
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
49 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
50
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
51 #if NET_4_5
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
52
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
53 public static Task<T> GetTask<T>(this IPromise<T> that) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
54 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
55 var tcs = new TaskCompletionSource<T>();
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
56
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
57 that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
58
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
59 return tcs.Task;
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
60 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
61
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
62 #endif
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
63 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
64 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
65