Mercurial > pub > ImplabNet
diff Implab/FuncChainTaskBase.cs @ 187:dd4a3590f9c6 ref20160224
Reworked cancelation handling, if the cancel handler isn't specified the OperationCanceledException will be handled by the error handler
Any unhandled OperationCanceledException will cause the promise cancelation
author | cin |
---|---|
date | Tue, 19 Apr 2016 17:35:20 +0300 |
parents | eb793fbbe4ea |
children | 40d7fed4a09e |
line wrap: on
line diff
--- a/Implab/FuncChainTaskBase.cs Tue Apr 19 00:50:14 2016 +0300 +++ b/Implab/FuncChainTaskBase.cs Tue Apr 19 17:35:20 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()); + } } } }