comparison Implab/PromiseExtensions.cs @ 208:7d07503621fe v2

RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool) IRunnable is now disposable Code cleanups, suppressed some CodeAnalysis warnings
author cin
date Sun, 13 Nov 2016 18:28:17 +0300
parents 558f34b2fb50
children a867536c68fc
comparison
equal deleted inserted replaced
207:558f34b2fb50 208:7d07503621fe
118 Safe.ArgumentNotNull(that, "that"); 118 Safe.ArgumentNotNull(that, "that");
119 return PromiseAll(that.ToList()); 119 return PromiseAll(that.ToList());
120 } 120 }
121 121
122 public static IPromise<T[]> PromiseAll<T>(this IEnumerable<IPromise<T>> that) { 122 public static IPromise<T[]> PromiseAll<T>(this IEnumerable<IPromise<T>> that) {
123 Safe.ArgumentNotNull(that, "that"); 123 return PromiseAll(that, null);
124 return PromiseAll(that.ToList()); 124 }
125
126 public static IPromise<T[]> PromiseAll<T>(this IEnumerable<IPromise<T>> that, Action<T> cleanup) {
127 Safe.ArgumentNotNull(that, "that");
128 return PromiseAll(that.ToList(), cleanup);
125 } 129 }
126 130
127 public static IPromise PromiseAll(this ICollection<IPromise> that) { 131 public static IPromise PromiseAll(this ICollection<IPromise> that) {
128 Safe.ArgumentNotNull(that, "that"); 132 Safe.ArgumentNotNull(that, "that");
129 133
163 167
164 return medium; 168 return medium;
165 } 169 }
166 170
167 public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that) { 171 public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that) {
168 Safe.ArgumentNotNull(that, "that"); 172 return PromiseAll(that, null);
169 173 }
174
175 /// <summary>
176 /// Creates a new promise which will be satisfied when all promises are satisfied.
177 /// </summary>
178 /// <typeparam name="T"></typeparam>
179 /// <param name="that"></param>
180 /// <param name="cleanup">A callback used to cleanup already resolved promises in case of an error</param>
181 /// <returns></returns>
182 public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that, Action<T> cleanup) {
183 Safe.ArgumentNotNull(that, "that");
184
170 int count = that.Count; 185 int count = that.Count;
186
187 if (count == 0)
188 return Promise<T[]>.FromResult(new T[0]);
189
171 int errors = 0; 190 int errors = 0;
172 var medium = new Promise<T[]>(); 191 var medium = new Promise<T[]>();
173 var results = new T[that.Count]; 192 var results = new T[that.Count];
174 193
175 medium.On(() => { 194 medium.On(() => {
176 foreach (var p2 in that) 195 foreach (var p2 in that) {
177 p2.Cancel(); 196 p2.Cancel();
197 if (cleanup != null)
198 p2.On(cleanup);
199 }
178 }, PromiseEventType.ErrorOrCancel); 200 }, PromiseEventType.ErrorOrCancel);
179 201
180 int i = 0; 202 int i = 0;
181 foreach (var p in that) { 203 foreach (var p in that) {
182 var idx = i; 204 var idx = i;
412 Safe.ArgumentNotNull(that, "that"); 434 Safe.ArgumentNotNull(that, "that");
413 435
414 return new PromiseAwaiter<T>(that); 436 return new PromiseAwaiter<T>(that);
415 } 437 }
416 438
439 public static PromiseAwaiter GetAwaiter(this IPromise that) {
440 Safe.ArgumentNotNull(that, "that");
441
442 return new PromiseAwaiter(that);
443 }
444
417 #endif 445 #endif
418 } 446 }
419 } 447 }
420 448