Mercurial > pub > ImplabNet
comparison Implab/PromiseExtensions.cs @ 72:d67b95eddaf4 v2
promises refactoring
| author | cin |
|---|---|
| date | Thu, 04 Sep 2014 18:47:12 +0400 |
| parents | |
| children | 4439140706d0 |
comparison
equal
deleted
inserted
replaced
| 71:1714fd8678ef | 72:d67b95eddaf4 |
|---|---|
| 1 using System.Threading; | |
| 2 | |
| 3 namespace Implab { | |
| 4 public static class PromiseExtensions { | |
| 5 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { | |
| 6 var context = SynchronizationContext.Current; | |
| 7 if (context == null) | |
| 8 return that; | |
| 9 | |
| 10 var p = new SyncContextPromise<T>(context, that, true); | |
| 11 | |
| 12 that.Then( | |
| 13 x => p.Resolve(x), | |
| 14 e => { | |
| 15 p.Reject(e); | |
| 16 return default(T); | |
| 17 } | |
| 18 ); | |
| 19 return p; | |
| 20 } | |
| 21 | |
| 22 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | |
| 23 Safe.ArgumentNotNull(context, "context"); | |
| 24 | |
| 25 var p = new SyncContextPromise<T>(context, that, true); | |
| 26 | |
| 27 that.Then( | |
| 28 x => p.Resolve(x), | |
| 29 e => { | |
| 30 p.Reject(e); | |
| 31 return default(T); | |
| 32 } | |
| 33 ); | |
| 34 return p; | |
| 35 } | |
| 36 } | |
| 37 } | |
| 38 |
