diff 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
line wrap: on
line diff
--- a/Implab/PromiseExtensions.cs	Sun Dec 28 16:09:03 2014 +0300
+++ b/Implab/PromiseExtensions.cs	Sun Jan 11 19:13:02 2015 +0300
@@ -1,6 +1,7 @@
 using System.Threading;
 using System;
 using Implab.Diagnostics;
+using System.Collections.Generic;
 
 
 #if NET_4_5
@@ -15,7 +16,8 @@
             if (context == null)
                 return that;
 
-            var p = new SyncContextPromise<T>(context, that);
+            var p = new SyncContextPromise<T>(context);
+            p.On(that.Cancel, PromiseEventType.Cancelled);
 
             that.On(
                 p.Resolve,
@@ -29,7 +31,9 @@
             Safe.ArgumentNotNull(that, "that");
             Safe.ArgumentNotNull(context, "context");
 
-            var p = new SyncContextPromise<T>(context, that);
+            var p = new SyncContextPromise<T>(context);
+            p.On(that.Cancel, PromiseEventType.Cancelled);
+
 
             that.On(
                 p.Resolve,
@@ -89,6 +93,29 @@
             that.On(timer.Dispose, PromiseEventType.All);
             return that;
         }
+
+        public static IPromise Combine(this ICollection<IPromise> that) {
+            Safe.ArgumentNotNull(that, "that");
+
+            int count = that.Count;
+            var medium = new Promise();
+
+            foreach (var p in that)
+                p.On(
+                    () => {
+                        if (Interlocked.Decrement(ref count) == 0)
+                            medium.Resolve();
+                    },
+                    error => {
+                        throw new Exception("The dependency promise is failed", error);
+                    },
+                    () => {
+                        throw new OperationCanceledException("The dependency promise is cancelled");
+                    }
+                );
+
+            return medium;
+        }
             
         #if NET_4_5