ActionChainTaskBase.cs
58 lines
| 1.7 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / ActionChainTaskBase.cs
|
|
r145 | using System; | ||
| using System.Threading; | ||||
| namespace Implab { | ||||
| public class ActionChainTaskBase : AbstractPromise { | ||||
| readonly Func<Exception, IPromise> m_error; | ||||
| readonly Func<Exception, IPromise> m_cancel; | ||||
| int m_cancelationLock; | ||||
|
|
r149 | protected ActionChainTaskBase(Func<Exception, IPromise> error, Func<Exception, IPromise> cancel, bool autoCancellable) { | ||
|
|
r145 | m_error = error; | ||
| m_cancel = cancel; | ||||
|
|
r149 | if (autoCancellable) | ||
| CancellationRequested(CancelOperation); | ||||
|
|
r145 | } | ||
| public void Reject(Exception error) { | ||||
| if (LockCancelation()) | ||||
| HandleErrorInternal(error); | ||||
| } | ||||
| public override void CancelOperation(Exception reason) { | ||||
|
|
r149 | if (LockCancelation()) { | ||
| if (m_cancel != null) { | ||||
| try { | ||||
| m_cancel(reason).On(SetResult, SetError, SetCancelled); | ||||
| } catch (Exception err) { | ||||
| HandleErrorInternal(err); | ||||
| } | ||||
| } else { | ||||
| SetCancelled(reason); | ||||
|
|
r145 | } | ||
| } | ||||
| } | ||||
| protected void HandleErrorInternal(Exception error) { | ||||
| if (m_error != null) { | ||||
| try { | ||||
|
|
r149 | var p = m_error(error); | ||
| p.On(SetResult,SetError,SetCancelled); | ||||
| CancellationRequested(p.Cancel); | ||||
| } catch (Exception err) { | ||||
|
|
r145 | SetError(err); | ||
| } | ||||
| } else { | ||||
| SetError(error); | ||||
| } | ||||
| } | ||||
| protected bool LockCancelation() { | ||||
| return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0); | ||||
| } | ||||
| } | ||||
| } | ||||
