changeset 209:a867536c68fc v2

Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise
author cin
date Wed, 16 Nov 2016 03:06:08 +0300
parents 7d07503621fe
children 5dc21f6a3222
files Implab/Components/ExecutionState.cs Implab/PromiseExtensions.cs Implab/Safe.cs
diffstat 3 files changed, 38 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Implab/Components/ExecutionState.cs	Sun Nov 13 18:28:17 2016 +0300
+++ b/Implab/Components/ExecutionState.cs	Wed Nov 16 03:06:08 2016 +0300
@@ -13,6 +13,12 @@
 
         Running,
 
+        Suspending,
+
+        Suspended,
+
+        Resuming,
+
         Stopping,
 
         Failed,
--- a/Implab/PromiseExtensions.cs	Sun Nov 13 18:28:17 2016 +0300
+++ b/Implab/PromiseExtensions.cs	Wed Nov 16 03:06:08 2016 +0300
@@ -180,8 +180,8 @@
         /// <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");
-            
+            Safe.ArgumentNotNull(that, "that");
+
             int count = that.Count;
 
             if (count == 0)
@@ -263,6 +263,8 @@
 
         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");
+            Safe.ArgumentNotNull(success, "success");
+
             var d = new FuncTask<T, T2>(success, error, cancel, false);
             that.On(d.Resolve, d.Reject, d.CancelOperation);
             d.CancellationRequested(that.Cancel);
@@ -276,8 +278,8 @@
                     success(x);
                     return x;
                 },
-                error, 
-                cancel, 
+                error,
+                cancel,
                 false
             );
             that.On(d.Resolve, d.Reject, d.CancelOperation);
@@ -427,6 +429,13 @@
 
         #endregion
 
+        public static IPromise<T2> Guard<T, T2>(this IPromise<T> that, Func<IPromise<T>, IPromise<T2>> continuation, Action<T> cleanup) {
+            Safe.ArgumentNotNull(that, "that");
+            Safe.ArgumentNotNull(continuation, "continuation");
+            return continuation(that).Error((err) => {
+                that.On(cleanup);
+            }, true);
+        }
 
 #if NET_4_5
 
@@ -442,6 +451,24 @@
             return new PromiseAwaiter(that);
         }
 
+        public static IPromise BoundCancellationToken(this IPromise that, CancellationToken ct) {
+            Safe.ArgumentNotNull(that, "that");
+            ct.Register(that.Cancel);
+            return that.Then(null, null, (err) => {
+                ct.ThrowIfCancellationRequested();
+                throw new PromiseTransientException(err);
+            });
+        }
+
+        public static IPromise<T> BoundCancellationToken<T>(this IPromise<T> that, CancellationToken ct) {
+            Safe.ArgumentNotNull(that, "that");
+            ct.Register(that.Cancel);
+            return that.Then(null, null, (err) => {
+                ct.ThrowIfCancellationRequested();
+                throw new PromiseTransientException(err);
+            });
+        }
+
 #endif
     }
 }
--- a/Implab/Safe.cs	Sun Nov 13 18:28:17 2016 +0300
+++ b/Implab/Safe.cs	Wed Nov 16 03:06:08 2016 +0300
@@ -127,5 +127,6 @@
                 return Promise<T>.FromException(err);
             }
         }
+
     }
 }