diff 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
line wrap: on
line diff
--- a/Implab/PromiseExtensions.cs	Wed Nov 09 12:03:22 2016 +0300
+++ b/Implab/PromiseExtensions.cs	Sun Nov 13 18:28:17 2016 +0300
@@ -120,8 +120,12 @@
         }
 
         public static IPromise<T[]> PromiseAll<T>(this IEnumerable<IPromise<T>> that) {
+            return PromiseAll(that, null);
+        }
+
+        public static IPromise<T[]> PromiseAll<T>(this IEnumerable<IPromise<T>> that, Action<T> cleanup) {
             Safe.ArgumentNotNull(that, "that");
-            return PromiseAll(that.ToList());
+            return PromiseAll(that.ToList(), cleanup);
         }
 
         public static IPromise PromiseAll(this ICollection<IPromise> that) {
@@ -164,17 +168,35 @@
             return medium;
         }
 
-        public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that) {
+        public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that) {
+            return PromiseAll(that, null);
+        }
+
+        /// <summary>
+        /// Creates a new promise which will be satisfied when all promises are satisfied.
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="that"></param>
+        /// <param name="cleanup">A callback used to cleanup already resolved promises in case of an error</param>
+        /// <returns></returns>
+        public static IPromise<T[]> PromiseAll<T>(this ICollection<IPromise<T>> that, Action<T> cleanup) {
             Safe.ArgumentNotNull(that, "that");
+            
+            int count = that.Count;
 
-            int count = that.Count;
+            if (count == 0)
+                return Promise<T[]>.FromResult(new T[0]);
+
             int errors = 0;
             var medium = new Promise<T[]>();
             var results = new T[that.Count];
 
             medium.On(() => {
-                foreach (var p2 in that)
-                    p2.Cancel();
+                foreach (var p2 in that) {
+                    p2.Cancel();
+                    if (cleanup != null)
+                        p2.On(cleanup);
+                }
             }, PromiseEventType.ErrorOrCancel);
 
             int i = 0;
@@ -414,6 +436,12 @@
             return new PromiseAwaiter<T>(that);
         }
 
+        public static PromiseAwaiter GetAwaiter(this IPromise that) {
+            Safe.ArgumentNotNull(that, "that");
+
+            return new PromiseAwaiter(that);
+        }
+
 #endif
     }
 }