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