PromiseActionReaction.cs
91 lines
| 3.1 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / PromiseActionReaction.cs
|
|
r247 | using System; | |
| using System.Diagnostics; | |||
| namespace Implab { | |||
|
|
r249 | class PromiseActionReaction : IResolvable { | |
|
|
r247 | ||
| readonly Deferred m_next; | |||
|
|
r249 | readonly IDispatcher m_dispatcher; | |
| readonly Action<Deferred> m_fulfilled; | |||
| readonly Action<Exception, Deferred> m_rejected; | |||
|
|
r248 | public IPromise Promise { | |
| get { return m_next.Promise; } | |||
| } | |||
|
|
r249 | public PromiseActionReaction(Action<Deferred> fulfilled, Action<Exception, Deferred> rejected, Deferred next, IDispatcher dispatcher) { | |
| m_next = next; | |||
| m_fulfilled = fulfilled; | |||
| m_rejected = rejected; | |||
| m_dispatcher = dispatcher; | |||
|
|
r247 | } | |
|
|
r249 | public void Resolve() { | |
| if (m_fulfilled != null) { | |||
| if (m_dispatcher != null) | |||
| m_dispatcher.Enqueue(ResolveImpl); | |||
| else | |||
| ResolveImpl(); | |||
| } else { | |||
| m_next.Resolve(); | |||
| } | |||
| } | |||
|
|
r247 | ||
|
|
r249 | void ResolveImpl() { | |
| m_fulfilled(m_next); | |||
| } | |||
| public void Reject(Exception error) { | |||
| if (m_fulfilled != null) { | |||
| if (m_dispatcher != null) | |||
| m_dispatcher.Enqueue(RejectImpl, error); | |||
| else | |||
| RejectImpl(error); | |||
| } else { | |||
| m_next.Reject(error); | |||
| } | |||
|
|
r247 | } | |
|
|
r249 | void RejectImpl(Exception error) { | |
| m_rejected(error, m_next); | |||
| } | |||
|
|
r247 | ||
|
|
r249 | public static PromiseActionReaction Create(Action fulfilled, Action<Exception> rejected, IDispatcher dispatcher) { | |
| return new PromiseActionReaction( | |||
| fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
| rejected != null ? PromiseHandler.Create(rejected) : null, | |||
| new Deferred(), | |||
| dispatcher | |||
| ); | |||
|
|
r247 | } | |
|
|
r249 | public static PromiseActionReaction Create(Func<IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) { | |
| return new PromiseActionReaction( | |||
| fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
| rejected != null ? PromiseHandler.Create(rejected) : null, | |||
| new Deferred(), | |||
| dispatcher | |||
| ); | |||
|
|
r248 | } | |
|
|
r247 | ||
|
|
r249 | public static PromiseActionReaction Create(Action fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) { | |
| return new PromiseActionReaction( | |||
| fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
| rejected != null ? PromiseHandler.Create(rejected) : null, | |||
| new Deferred(), | |||
| dispatcher | |||
| ); | |||
|
|
r247 | } | |
|
|
r249 | public static PromiseActionReaction Create(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) { | |
| return new PromiseActionReaction( | |||
| fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
| rejected != null ? PromiseHandler.Create(rejected) : null, | |||
| new Deferred(), | |||
| dispatcher | |||
| ); | |||
|
|
r247 | } | |
| } | |||
| } |
