PromiseFuncReaction`2.cs
96 lines
| 3.5 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / PromiseFuncReaction`2.cs
cin
|
r248 | using System; | |
using System.Diagnostics; | |||
namespace Implab { | |||
cin
|
r249 | class PromiseFuncReaction<TIn, TRet> : IResolvable<TIn> { | |
cin
|
r248 | readonly Deferred<TRet> m_next; | |
cin
|
r249 | readonly IDispatcher m_dispatcher; | |
readonly Action<TIn, Deferred<TRet>> m_fulfilled; | |||
readonly Action<Exception, Deferred<TRet>> m_rejected; | |||
public IPromise<TRet> Promise { | |||
cin
|
r248 | get { return m_next.Promise; } | |
} | |||
cin
|
r249 | public PromiseFuncReaction(Action<TIn, Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) { | |
m_next = next; | |||
m_fulfilled = fulfilled; | |||
m_rejected = rejected; | |||
m_dispatcher = dispatcher; | |||
cin
|
r248 | } | |
cin
|
r249 | public void Resolve(TIn result) { | |
if (m_fulfilled != null) { | |||
if (m_dispatcher != null) | |||
m_dispatcher.Enqueue(ResolveImpl, result); | |||
else | |||
ResolveImpl(result); | |||
} else { | |||
try { | |||
m_next.Resolve((TRet)(object)result); | |||
} catch(Exception error) { | |||
// handle cast exceptions | |||
m_next.Reject(error); | |||
} | |||
} | |||
} | |||
cin
|
r248 | ||
cin
|
r249 | void ResolveImpl (TIn result) { | |
m_fulfilled(result, 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); | |||
} | |||
cin
|
r248 | } | |
cin
|
r249 | void RejectImpl(Exception error) { | |
m_rejected(error, m_next); | |||
} | |||
cin
|
r248 | ||
cin
|
r249 | public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) { | |
return new PromiseFuncReaction<TIn,TRet>( | |||
fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
rejected != null ? PromiseHandler.Create(rejected) : null, | |||
new Deferred<TRet>(), | |||
dispatcher | |||
); | |||
cin
|
r248 | } | |
cin
|
r249 | public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) { | |
return new PromiseFuncReaction<TIn,TRet>( | |||
fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
rejected != null ? PromiseHandler.Create(rejected) : null, | |||
new Deferred<TRet>(), | |||
dispatcher | |||
); | |||
cin
|
r248 | } | |
cin
|
r249 | public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) { | |
return new PromiseFuncReaction<TIn,TRet>( | |||
fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
rejected != null ? PromiseHandler.Create(rejected) : null, | |||
new Deferred<TRet>(), | |||
dispatcher | |||
); | |||
cin
|
r248 | } | |
cin
|
r249 | public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) { | |
return new PromiseFuncReaction<TIn,TRet>( | |||
fulfilled != null ? PromiseHandler.Create(fulfilled) : null, | |||
rejected != null ? PromiseHandler.Create(rejected) : null, | |||
new Deferred<TRet>(), | |||
dispatcher | |||
); | |||
cin
|
r248 | } | |
} | |||
} |