ActionChainTaskBase.cs
75 lines
| 2.5 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / ActionChainTaskBase.cs
cin
|
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; | ||||
cin
|
r149 | protected ActionChainTaskBase(Func<Exception, IPromise> error, Func<Exception, IPromise> 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
|
r149 | if (LockCancelation()) { | ||
cin
|
r186 | if (!(reason is OperationCanceledException)) | ||
reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException(); | ||||
cin
|
r149 | if (m_cancel != null) { | ||
try { | ||||
cin
|
r186 | m_cancel(reason).On(SetResult, HandleErrorInternal, HandleCancelInternal); | ||
cin
|
r149 | } catch (Exception err) { | ||
HandleErrorInternal(err); | ||||
} | ||||
} else { | ||||
cin
|
r186 | HandleErrorInternal(reason); | ||
cin
|
r145 | } | ||
} | ||||
} | ||||
cin
|
r186 | void HandleCancelInternal(Exception reason) { | ||
if (!(reason is OperationCanceledException)) | ||||
reason = reason != null ? new OperationCanceledException(null, reason) : new OperationCanceledException(); | ||||
HandleErrorInternal(reason); | ||||
} | ||||
void HandleErrorInternal(Exception error) { | ||||
cin
|
r145 | if (m_error != null) { | ||
try { | ||||
cin
|
r149 | var p = m_error(error); | ||
cin
|
r186 | p.On(SetResult, SetError, SetCancelled); | ||
cin
|
r149 | CancellationRequested(p.Cancel); | ||
} catch (Exception err) { | ||||
cin
|
r186 | error = err; | ||
cin
|
r145 | } | ||
} else { | ||||
cin
|
r186 | SetErrorInternal(error); | ||
} | ||||
} | ||||
void SetErrorInternal(Exception error) { | ||||
while (error is PromiseTransientException) | ||||
error = error.InnerException; | ||||
if (error is OperationCanceledException) | ||||
SetCancelled(error); | ||||
else | ||||
cin
|
r145 | SetError(error); | ||
} | ||||
protected bool LockCancelation() { | ||||
return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0); | ||||
} | ||||
} | ||||
} | ||||