diff Implab/FuncChainTaskBase.cs @ 190:1c2a16d071a7 v2

Слияние с ref20160224
author cin
date Fri, 22 Apr 2016 13:08:08 +0300
parents dd4a3590f9c6
children 40d7fed4a09e
line wrap: on
line diff
--- a/Implab/FuncChainTaskBase.cs	Fri Feb 19 18:07:17 2016 +0300
+++ b/Implab/FuncChainTaskBase.cs	Fri Apr 22 13:08:08 2016 +0300
@@ -1,13 +1,10 @@
 using System;
-using System.Threading;
 
 namespace Implab {
-    public class FuncChainTaskBase<TResult> : AbstractPromise<TResult> {
+    public class FuncChainTaskBase<TResult> : AbstractTask<TResult> {
         readonly Func<Exception, IPromise<TResult>> m_error;
         readonly Func<Exception, IPromise<TResult>> m_cancel;
 
-        int m_cancelationLock;
-
         protected FuncChainTaskBase( Func<Exception, IPromise<TResult>> error, Func<Exception, IPromise<TResult>> cancel, bool autoCancellable) {
             m_error = error;
             m_cancel = cancel;
@@ -21,37 +18,36 @@
         }
 
         public override void CancelOperation(Exception reason) {
-            if (LockCancelation()) {
-                if (m_cancel != null) {
-                    try {
-                        m_cancel(reason).On(SetResult, HandleErrorInternal, SetCancelled);
-                    } catch (Exception err) {
-                        HandleErrorInternal(err);
-                    }
-                } else {
-                    SetCancelled(reason);
-                }
-            }
-
+            if (LockCancelation())
+                HandleCancelInternal(reason);
         }
 
         protected void HandleErrorInternal(Exception error) {
             if (m_error != null) {
                 try {
-                    var operation = m_error(error);
-
-                    operation.On(SetResult, SetError, SetCancelled);
-                    CancellationRequested(operation.Cancel);
+                    var p = m_error(error);
+                    p.On(SetResult, SetErrorInternal, SetCancelledInternal);
+                    CancellationRequested(p.Cancel);
                 } catch(Exception err) {
-                    SetError(err);
+                    SetErrorInternal(err);
                 }
             } else {
-                SetError(error);
+                SetErrorInternal(error);
             }
         }
 
-        protected bool LockCancelation() {
-            return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
+        protected void HandleCancelInternal(Exception reason) {
+            if (m_cancel != null) {
+                try {
+                    var p = m_cancel(reason);
+                    p.On(SetResult, HandleErrorInternal, SetCancelledInternal);
+                    CancellationRequested(p.Cancel);
+                } catch (Exception err) {
+                    HandleErrorInternal(err);
+                }
+            } else {
+                HandleErrorInternal(reason ?? new OperationCanceledException());
+            }
         }
     }
 }