##// END OF EJS Templates
docs
docs

File last commit:

r145:706fccb85524 v2
r154:2dcdee4c0810 v2
Show More
AbstractPromise.cs
138 lines | 4.3 KiB | text/x-csharp | CSharpLexer
/ Implab / AbstractPromise.cs
cin
Promises rewritten, added improved version of AsyncQueue
r119 using System;
using Implab.Parallels;
namespace Implab {
cin
DRAFT: refactoring
r144 public abstract class AbstractPromise : AbstractEvent<AbstractPromise.HandlerDescriptor>, IPromise {
public struct HandlerDescriptor {
readonly Action m_handler;
readonly Action<Exception> m_error;
readonly Action<Exception> m_cancel;
readonly PromiseEventType m_mask;
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) {
m_handler = success;
m_error = error;
m_cancel = cancel;
m_mask = PromiseEventType.Success;
}
cin
improved performance of promises
r125
cin
DRAFT: refactoring
r144 public HandlerDescriptor(Action handler, PromiseEventType mask) {
m_handler = handler;
cin
RC: cancellation support for promises + tests
r145 m_error = null;
m_cancel = null;
cin
DRAFT: refactoring
r144 m_mask = mask;
}
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 public void SignalSuccess() {
cin
RC: cancellation support for promises + tests
r145 if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) {
cin
DRAFT: refactoring
r144 try {
m_handler();
} catch (Exception err) {
// avoid calling handler twice in case of error
if (m_error != null)
SignalError(err);
}
}
}
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 public void SignalError(Exception err) {
if (m_error != null) {
try {
m_error(err);
// Analysis disable once EmptyGeneralCatchClause
} catch {
}
cin
RC: cancellation support for promises + tests
r145 } else if ((m_mask & PromiseEventType.Error ) != 0 && m_handler != null) {
cin
DRAFT: refactoring
r144 try {
m_handler();
// Analysis disable once EmptyGeneralCatchClause
} catch {
}
}
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 public void SignalCancel(Exception reason) {
if (m_cancel != null) {
try {
m_cancel(reason);
} catch (Exception err) {
SignalError(err);
}
cin
RC: cancellation support for promises + tests
r145 } else if ( (m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) {
cin
DRAFT: refactoring
r144 try {
m_handler();
// Analysis disable once EmptyGeneralCatchClause
} catch {
}
}
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
}
cin
DRAFT: refactoring
r144 #region implemented abstract members of AbstractPromise
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 protected override void SignalSuccess(HandlerDescriptor handler) {
handler.SignalSuccess();
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 protected override void SignalError(HandlerDescriptor handler, Exception error) {
handler.SignalError(error);
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) {
handler.SignalCancel(reason);
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 protected override Signal GetResolveSignal() {
var signal = new Signal();
On(signal.Set, PromiseEventType.All);
cin
RC: cancellation support for promises + tests
r145 return signal;
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
#endregion
cin
DRAFT: refactoring
r144 public Type PromiseType {
get {
return typeof(void);
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
}
cin
DRAFT: refactoring
r144 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) {
AddHandler(new HandlerDescriptor(success, error, cancel));
return this;
}
public IPromise On(Action success, Action<Exception> error) {
AddHandler(new HandlerDescriptor(success, error, null));
return this;
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 public IPromise On(Action success) {
AddHandler(new HandlerDescriptor(success, null, null));
return this;
}
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 public IPromise On(Action handler, PromiseEventType events) {
AddHandler(new HandlerDescriptor(handler,events));
return this;
}
cin
Promises rewritten, added improved version of AsyncQueue
r119
cin
DRAFT: refactoring
r144 public IPromise<T> Cast<T>() {
throw new InvalidCastException();
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
public void Join() {
WaitResult(-1);
}
cin
DRAFT: refactoring
r144 public void Join(int timeout) {
WaitResult(timeout);
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
cin
DRAFT: refactoring
r144 protected void SetResult() {
BeginSetResult();
EndSetResult();
cin
added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
r138 }
cin
Promises rewritten, added improved version of AsyncQueue
r119 }
}