annotate Implab/PromiseExtensions.cs @ 75:4439140706d0 v2

major refactoring, added tasks support
author cin
date Wed, 10 Sep 2014 11:17:37 +0400
parents d67b95eddaf4
children c761fc982e1d
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
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
17 that.Then(
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
18 x => p.Resolve(x),
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
19 e => {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
20 p.Reject(e);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
21 return default(T);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
22 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
23 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
24 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
25 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
26
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
27 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
28 Safe.ArgumentNotNull(that, "that");
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
29 Safe.ArgumentNotNull(context, "context");
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
30
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
31 var p = new SyncContextPromise<T>(context, that, true);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
32
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
33 that.Then(
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
34 x => p.Resolve(x),
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
35 e => {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
36 p.Reject(e);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
37 return default(T);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
38 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
39 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
40 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
41 }
75
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
42
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
43 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
44 Safe.ArgumentNotNull(that, "that");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
45 Safe.ArgumentNotNull(callback, "callback");
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
46 return ar => {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
47 try {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
48 that.Resolve(callback(ar));
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
49 } catch (Exception err) {
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
50 that.Reject(err);
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
51 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
52 };
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
53 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
54
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
55 #if NET_4_5
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
56
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
57 public static Task<T> GetTask<T>(this IPromise<T> that) {
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 var tcs = new TaskCompletionSource<T>();
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
60
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
61 that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
62
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
63 return tcs.Task;
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
64 }
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
65
4439140706d0 major refactoring, added tasks support
cin
parents: 72
diff changeset
66 #endif
72
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
67 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
68 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
69