FuncChainTaskBase.cs
54 lines
| 1.8 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / FuncChainTaskBase.cs
|
|
r145 | using System; | ||
| namespace Implab { | ||||
|
|
r187 | public class FuncChainTaskBase<TResult> : AbstractTask<TResult> { | ||
|
|
r145 | readonly Func<Exception, IPromise<TResult>> m_error; | ||
| readonly Func<Exception, IPromise<TResult>> m_cancel; | ||||
|
|
r149 | protected FuncChainTaskBase( Func<Exception, IPromise<TResult>> error, Func<Exception, IPromise<TResult>> 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) { | ||||
|
|
r187 | if (LockCancelation()) | ||
| HandleCancelInternal(reason); | ||||
|
|
r145 | } | ||
| protected void HandleErrorInternal(Exception error) { | ||||
| if (m_error != null) { | ||||
| try { | ||||
|
|
r187 | var p = m_error(error); | ||
| p.On(SetResult, SetErrorInternal, SetCancelledInternal); | ||||
| CancellationRequested(p.Cancel); | ||||
|
|
r145 | } catch(Exception err) { | ||
|
|
r187 | SetErrorInternal(err); | ||
|
|
r145 | } | ||
| } else { | ||||
|
|
r187 | SetErrorInternal(error); | ||
|
|
r145 | } | ||
| } | ||||
|
|
r187 | 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()); | ||||
| } | ||||
|
|
r145 | } | ||
| } | ||||
| } | ||||
