diff Implab/PromiseExtensions.cs @ 145:706fccb85524 v2

RC: cancellation support for promises + tests
author cin
date Sun, 08 Mar 2015 02:52:27 +0300
parents f75cfa58e3d4
children eb793fbbe4ea
line wrap: on
line diff
--- a/Implab/PromiseExtensions.cs	Fri Mar 06 15:45:26 2015 +0300
+++ b/Implab/PromiseExtensions.cs	Sun Mar 08 02:52:27 2015 +0300
@@ -174,6 +174,116 @@
 
             return medium;
         }
+
+        public static IPromise Then(this IPromise that, Action success, Action<Exception> error, Action<Exception> cancel) {
+            Safe.ArgumentNotNull(that, "that");
+
+            var d = new ActionTask(success, error, cancel);
+            that.On(d.Resolve, d.Reject, d.CancelOperation);
+            if (success != null)
+                d.CancellationRequested(that.Cancel);
+            return d;
+        }
+
+        public static IPromise Then(this IPromise that, Action success, Action<Exception> error) {
+            return Then(that, success, error, null);
+        }
+
+        public static IPromise Then(this IPromise that, Action success) {
+            return Then(that, success, null, null);
+        }
+
+        public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error, Func<Exception, T> cancel) {
+            Safe.ArgumentNotNull(that, "that");
+
+            var d = new FuncTask<T>(success, error, cancel);
+            that.On(d.Resolve, d.Reject, d.CancelOperation);
+            if (success != null)
+                d.CancellationRequested(that.Cancel);
+            return d;
+        }
+
+        public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error) {
+            return Then(that, success, error, null);
+        }
+
+        public static IPromise<T> Then<T>(this IPromise that, Func<T> success) {
+            return Then(that, success, null, null);
+        }
+
+        public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) {
+            Safe.ArgumentNotNull(that, "that");
+            var d = new FuncTask<T,T2>(success, error, cancel);
+            that.On(d.Resolve, d.Reject, d.CancelOperation);
+            if (success != null)
+                d.CancellationRequested(that.Cancel);
+            return d;
+        }
+
+        public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) {
+            return Then(that, success, error, null);
+        }
+
+        public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success) {
+            return Then(that, success, null, null);
+        }
+
+        #region chain traits
+        public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error, Func<Exception,IPromise> cancel) {
+            Safe.ArgumentNotNull(that, "that");
+
+            var d = new ActionChainTask(success, error, cancel);
+            that.On(d.Resolve, d.Reject, d.CancelOperation);
+            if (success != null)
+                d.CancellationRequested(that.Cancel);
+            return d;
+        }
+
+        public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error) {
+            return Chain(that, success, error, null);
+        }
+
+        public static IPromise Chain(this IPromise that, Func<IPromise> success) {
+            return Chain(that, success, null, null);
+        }
+
+        public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success, Func<Exception, IPromise<T>> error, Func<Exception, IPromise<T>> cancel) {
+            Safe.ArgumentNotNull(that, "that");
+
+            var d = new FuncChainTask<T>(success, error, cancel);
+            that.On(d.Resolve, d.Reject, d.CancelOperation);
+            if (success != null)
+                d.CancellationRequested(that.Cancel);
+            return d;
+        }
+
+        public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success, Func<Exception, IPromise<T>> error) {
+            return Chain(that, success, error, null);
+        }
+
+        public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success) {
+            return Chain(that, success, null, null);
+        }
+
+        public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success, Func<Exception, IPromise<T2>> error, Func<Exception, IPromise<T2>> cancel) {
+            Safe.ArgumentNotNull(that, "that");
+            var d = new FuncChainTask<T,T2>(success, error, cancel);
+            that.On(d.Resolve, d.Reject, d.CancelOperation);
+            if (success != null)
+                d.CancellationRequested(that.Cancel);
+            return d;
+        }
+
+        public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success, Func<Exception, IPromise<T2>> error) {
+            return Chain(that, success, error, null);
+        }
+
+        public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success) {
+            return Chain(that, success, null, null);
+        }
+
+        #endregion
+
             
         #if NET_4_5