Mercurial > pub > ImplabNet
comparison Implab/PromiseExtensions.cs @ 119:2573b562e328 v2
Promises rewritten, added improved version of AsyncQueue
| author | cin |
|---|---|
| date | Sun, 11 Jan 2015 19:13:02 +0300 |
| parents | 1a8426e6e895 |
| children | a336cb13c6a9 |
comparison
equal
deleted
inserted
replaced
| 118:e046a94eecb1 | 119:2573b562e328 |
|---|---|
| 1 using System.Threading; | 1 using System.Threading; |
| 2 using System; | 2 using System; |
| 3 using Implab.Diagnostics; | 3 using Implab.Diagnostics; |
| 4 using System.Collections.Generic; | |
| 4 | 5 |
| 5 | 6 |
| 6 #if NET_4_5 | 7 #if NET_4_5 |
| 7 using System.Threading.Tasks; | 8 using System.Threading.Tasks; |
| 8 #endif | 9 #endif |
| 13 Safe.ArgumentNotNull(that, "that"); | 14 Safe.ArgumentNotNull(that, "that"); |
| 14 var context = SynchronizationContext.Current; | 15 var context = SynchronizationContext.Current; |
| 15 if (context == null) | 16 if (context == null) |
| 16 return that; | 17 return that; |
| 17 | 18 |
| 18 var p = new SyncContextPromise<T>(context, that); | 19 var p = new SyncContextPromise<T>(context); |
| 20 p.On(that.Cancel, PromiseEventType.Cancelled); | |
| 19 | 21 |
| 20 that.On( | 22 that.On( |
| 21 p.Resolve, | 23 p.Resolve, |
| 22 p.Reject, | 24 p.Reject, |
| 23 p.Cancel | 25 p.Cancel |
| 27 | 29 |
| 28 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | 30 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { |
| 29 Safe.ArgumentNotNull(that, "that"); | 31 Safe.ArgumentNotNull(that, "that"); |
| 30 Safe.ArgumentNotNull(context, "context"); | 32 Safe.ArgumentNotNull(context, "context"); |
| 31 | 33 |
| 32 var p = new SyncContextPromise<T>(context, that); | 34 var p = new SyncContextPromise<T>(context); |
| 35 p.On(that.Cancel, PromiseEventType.Cancelled); | |
| 36 | |
| 33 | 37 |
| 34 that.On( | 38 that.On( |
| 35 p.Resolve, | 39 p.Resolve, |
| 36 p.Reject, | 40 p.Reject, |
| 37 p.Cancel | 41 p.Cancel |
| 87 Safe.ArgumentNotNull(that, "that"); | 91 Safe.ArgumentNotNull(that, "that"); |
| 88 var timer = new Timer(CancelCallback, that, milliseconds, -1); | 92 var timer = new Timer(CancelCallback, that, milliseconds, -1); |
| 89 that.On(timer.Dispose, PromiseEventType.All); | 93 that.On(timer.Dispose, PromiseEventType.All); |
| 90 return that; | 94 return that; |
| 91 } | 95 } |
| 96 | |
| 97 public static IPromise Combine(this ICollection<IPromise> that) { | |
| 98 Safe.ArgumentNotNull(that, "that"); | |
| 99 | |
| 100 int count = that.Count; | |
| 101 var medium = new Promise(); | |
| 102 | |
| 103 foreach (var p in that) | |
| 104 p.On( | |
| 105 () => { | |
| 106 if (Interlocked.Decrement(ref count) == 0) | |
| 107 medium.Resolve(); | |
| 108 }, | |
| 109 error => { | |
| 110 throw new Exception("The dependency promise is failed", error); | |
| 111 }, | |
| 112 () => { | |
| 113 throw new OperationCanceledException("The dependency promise is cancelled"); | |
| 114 } | |
| 115 ); | |
| 116 | |
| 117 return medium; | |
| 118 } | |
| 92 | 119 |
| 93 #if NET_4_5 | 120 #if NET_4_5 |
| 94 | 121 |
| 95 public static Task<T> GetTask<T>(this IPromise<T> that) { | 122 public static Task<T> GetTask<T>(this IPromise<T> that) { |
| 96 Safe.ArgumentNotNull(that, "that"); | 123 Safe.ArgumentNotNull(that, "that"); |
