Mercurial > pub > ImplabNet
comparison Implab/PromiseExtensions.cs @ 124:a336cb13c6a9 v2
major update, added Drain mathod to AsyncQueue class
| author | cin |
|---|---|
| date | Thu, 15 Jan 2015 02:43:14 +0300 |
| parents | 2573b562e328 |
| children | e9e7940c7d98 |
comparison
equal
deleted
inserted
replaced
| 123:f4d6ea6969cc | 124:a336cb13c6a9 |
|---|---|
| 92 var timer = new Timer(CancelCallback, that, milliseconds, -1); | 92 var timer = new Timer(CancelCallback, that, milliseconds, -1); |
| 93 that.On(timer.Dispose, PromiseEventType.All); | 93 that.On(timer.Dispose, PromiseEventType.All); |
| 94 return that; | 94 return that; |
| 95 } | 95 } |
| 96 | 96 |
| 97 public static IPromise Combine(this ICollection<IPromise> that) { | 97 public static IPromise Bundle(this ICollection<IPromise> that) { |
| 98 Safe.ArgumentNotNull(that, "that"); | 98 Safe.ArgumentNotNull(that, "that"); |
| 99 | 99 |
| 100 int count = that.Count; | 100 int count = that.Count; |
| 101 int errors = 0; | |
| 101 var medium = new Promise(); | 102 var medium = new Promise(); |
| 103 | |
| 104 medium.On(() => { | |
| 105 foreach(var p2 in that) | |
| 106 p2.Cancel(); | |
| 107 }, PromiseEventType.ErrorOrCancel); | |
| 102 | 108 |
| 103 foreach (var p in that) | 109 foreach (var p in that) |
| 104 p.On( | 110 p.On( |
| 105 () => { | 111 () => { |
| 106 if (Interlocked.Decrement(ref count) == 0) | 112 if (Interlocked.Decrement(ref count) == 0) |
| 107 medium.Resolve(); | 113 medium.Resolve(); |
| 108 }, | 114 }, |
| 109 error => { | 115 error => { |
| 110 throw new Exception("The dependency promise is failed", error); | 116 if (Interlocked.Increment(ref errors) == 1) |
| 117 medium.Reject( | |
| 118 new Exception("The dependency promise is failed", error) | |
| 119 ); | |
| 111 }, | 120 }, |
| 112 () => { | 121 () => { |
| 113 throw new OperationCanceledException("The dependency promise is cancelled"); | 122 if (Interlocked.Increment(ref errors) == 1) |
| 123 medium.Reject( | |
| 124 new Exception("The dependency promise is cancelled") | |
| 125 ); | |
| 114 } | 126 } |
| 115 ); | 127 ); |
| 128 | |
| 129 return medium; | |
| 130 } | |
| 131 | |
| 132 public static IPromise<T[]> Bundle<T>(this ICollection<IPromise<T>> that) { | |
| 133 Safe.ArgumentNotNull(that, "that"); | |
| 134 | |
| 135 int count = that.Count; | |
| 136 int errors = 0; | |
| 137 var medium = new Promise<T[]>(); | |
| 138 var results = new T[that.Count]; | |
| 139 | |
| 140 medium.On(() => { | |
| 141 foreach(var p2 in that) | |
| 142 p2.Cancel(); | |
| 143 }, PromiseEventType.ErrorOrCancel); | |
| 144 | |
| 145 int i = 0; | |
| 146 foreach (var p in that) { | |
| 147 var idx = i; | |
| 148 p.On( | |
| 149 x => { | |
| 150 results[idx] = x; | |
| 151 if (Interlocked.Decrement(ref count) == 0) | |
| 152 medium.Resolve(results); | |
| 153 }, | |
| 154 error => { | |
| 155 if (Interlocked.Increment(ref errors) == 1) | |
| 156 medium.Reject( | |
| 157 new Exception("The dependency promise is failed", error) | |
| 158 ); | |
| 159 }, | |
| 160 () => { | |
| 161 if (Interlocked.Increment(ref errors) == 1) | |
| 162 medium.Reject( | |
| 163 new Exception("The dependency promise is cancelled") | |
| 164 ); | |
| 165 } | |
| 166 ); | |
| 167 i++; | |
| 168 } | |
| 116 | 169 |
| 117 return medium; | 170 return medium; |
| 118 } | 171 } |
| 119 | 172 |
| 120 #if NET_4_5 | 173 #if NET_4_5 |
