##// END OF EJS Templates
fixed timeout handling in promises
fixed timeout handling in promises

File last commit:

r145:706fccb85524 v2
r148:e6d4b41f0101 v2
Show More
FuncChainTaskBase.cs
60 lines | 1.8 KiB | text/x-csharp | CSharpLexer
/ Implab / FuncChainTaskBase.cs
using System;
using System.Threading;
namespace Implab {
public class FuncChainTaskBase<TResult> : AbstractPromise<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) {
m_error = error;
m_cancel = cancel;
}
public void Reject(Exception error) {
if (LockCancelation())
HandleErrorInternal(error);
}
public override void CancelOperation(Exception reason) {
if (m_cancel != null && LockCancelation()) {
try {
Observe(m_cancel(reason));
} catch(Exception err) {
HandleErrorInternal(err);
}
}
}
protected void HandleErrorInternal(Exception error) {
if (m_error != null) {
try {
Observe(m_error(error));
} catch(Exception err) {
SetError(err);
}
} else {
SetError(error);
}
}
protected void Observe(IPromise<TResult> operation) {
if (operation == null)
throw new NullReferenceException("The task returned null promise");
// pass operation results to the current promise
operation.On(SetResult, SetError, SetCancelled);
// pass the cancelation request
CancellationRequested(operation.Cancel);
}
protected bool LockCancelation() {
return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
}
}
}