annotate Implab/PromiseExtensions.cs @ 72:d67b95eddaf4 v2

promises refactoring
author cin
date Thu, 04 Sep 2014 18:47:12 +0400
parents
children 4439140706d0
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;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
2
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
3 namespace Implab {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
4 public static class PromiseExtensions {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
5 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
6 var context = SynchronizationContext.Current;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
7 if (context == null)
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
8 return that;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
9
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
10 var p = new SyncContextPromise<T>(context, that, true);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
11
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
12 that.Then(
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
13 x => p.Resolve(x),
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
14 e => {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
15 p.Reject(e);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
16 return default(T);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
17 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
18 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
19 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
20 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
21
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
22 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
23 Safe.ArgumentNotNull(context, "context");
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
24
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
25 var p = new SyncContextPromise<T>(context, that, true);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
26
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
27 that.Then(
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
28 x => p.Resolve(x),
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
29 e => {
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
30 p.Reject(e);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
31 return default(T);
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
32 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
33 );
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
34 return p;
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
35 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
36 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
37 }
d67b95eddaf4 promises refactoring
cin
parents:
diff changeset
38