diff Implab/PromiseExtensions.cs @ 185:822aab37b107 ref20160224

runnable component, work in progress
author cin
date Mon, 18 Apr 2016 16:41:17 +0300
parents ec91a6dfa5b3
children 8200ab154c8a
line wrap: on
line diff
--- a/Implab/PromiseExtensions.cs	Sat Apr 16 03:23:26 2016 +0300
+++ b/Implab/PromiseExtensions.cs	Mon Apr 18 16:41:17 2016 +0300
@@ -3,11 +3,6 @@
 using Implab.Diagnostics;
 using System.Collections.Generic;
 
-
-#if NET_4_5
-using System.Threading.Tasks;
-#endif
-
 namespace Implab {
     public static class PromiseExtensions {
         public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
@@ -17,12 +12,12 @@
                 return that;
 
             var p = new SyncContextPromise<T>(context);
-            p.On(that.Cancel, PromiseEventType.Cancelled);
+            p.CancellationRequested(that.Cancel);
 
             that.On(
                 p.Resolve,
                 p.Reject,
-                p.Cancel
+                p.CancelOperation
             );
             return p;
         }
@@ -32,13 +27,12 @@
             Safe.ArgumentNotNull(context, "context");
 
             var p = new SyncContextPromise<T>(context);
-            p.On(that.Cancel, PromiseEventType.Cancelled);
-
+            p.CancellationRequested(that.Cancel);
 
             that.On(
                 p.Resolve,
                 p.Reject,
-                p.Cancel
+                p.CancelOperation
             );
             return p;
         }
@@ -77,8 +71,8 @@
             };
         }
 
-        static void CancelCallback(object cookie) {
-            ((ICancellable)cookie).Cancel();
+        static void CancelByTimeoutCallback(object cookie) {
+            ((ICancellable)cookie).Cancel(new TimeoutException());
         }
 
         /// <summary>
@@ -89,7 +83,7 @@
         /// <typeparam name="TPromise">The 1st type parameter.</typeparam>
         public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise {
             Safe.ArgumentNotNull(that, "that");
-            var timer = new Timer(CancelCallback, that, milliseconds, -1);
+            var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1);
             that.On(timer.Dispose, PromiseEventType.All);
             return that;
         }
@@ -180,8 +174,7 @@
 
             var d = new ActionTask(success, error, cancel, false);
             that.On(d.Resolve, d.Reject, d.CancelOperation);
-            if (success != null)
-                d.CancellationRequested(that.Cancel);
+            d.CancellationRequested(that.Cancel);
             return d;
         }
 
@@ -198,8 +191,7 @@
 
             var d = new FuncTask<T>(success, error, cancel, false);
             that.On(d.Resolve, d.Reject, d.CancelOperation);
-            if (success != null)
-                d.CancellationRequested(that.Cancel);
+            d.CancellationRequested(that.Cancel);
             return d;
         }
 
@@ -215,8 +207,7 @@
             Safe.ArgumentNotNull(that, "that");
             var d = new FuncTask<T,T2>(success, error, cancel, false);
             that.On(d.Resolve, d.Reject, d.CancelOperation);
-            if (success != null)
-                d.CancellationRequested(that.Cancel);
+            d.CancellationRequested(that.Cancel);
             return d;
         }
 
@@ -234,8 +225,7 @@
 
             var d = new ActionChainTask(success, error, cancel, false);
             that.On(d.Resolve, d.Reject, d.CancelOperation);
-            if (success != null)
-                d.CancellationRequested(that.Cancel);
+            d.CancellationRequested(that.Cancel);
             return d;
         }