248
+ − 1 using System;
+ − 2 using System.Diagnostics;
+ − 3
+ − 4 namespace Implab {
249
+ − 5 class PromiseFuncReaction<TIn, TRet> : IResolvable<TIn> {
248
+ − 6 readonly Deferred<TRet> m_next;
+ − 7
249
+ − 8 readonly IDispatcher m_dispatcher;
+ − 9
+ − 10 readonly Action<TIn, Deferred<TRet>> m_fulfilled;
+ − 11
+ − 12 readonly Action<Exception, Deferred<TRet>> m_rejected;
+ − 13
+ − 14 public IPromise<TRet> Promise {
248
+ − 15 get { return m_next.Promise; }
+ − 16 }
+ − 17
249
+ − 18 public PromiseFuncReaction(Action<TIn, Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) {
+ − 19 m_next = next;
+ − 20 m_fulfilled = fulfilled;
+ − 21 m_rejected = rejected;
+ − 22 m_dispatcher = dispatcher;
248
+ − 23 }
+ − 24
249
+ − 25 public void Resolve(TIn result) {
+ − 26 if (m_fulfilled != null) {
+ − 27 if (m_dispatcher != null)
+ − 28 m_dispatcher.Enqueue(ResolveImpl, result);
+ − 29 else
+ − 30 ResolveImpl(result);
+ − 31 } else {
+ − 32 try {
+ − 33 m_next.Resolve((TRet)(object)result);
+ − 34 } catch(Exception error) {
+ − 35 // handle cast exceptions
+ − 36 m_next.Reject(error);
+ − 37 }
+ − 38 }
+ − 39 }
248
+ − 40
249
+ − 41 void ResolveImpl (TIn result) {
+ − 42 m_fulfilled(result, m_next);
+ − 43 }
+ − 44
+ − 45 public void Reject(Exception error) {
+ − 46 if (m_fulfilled != null) {
+ − 47 if (m_dispatcher != null)
+ − 48 m_dispatcher.Enqueue(RejectImpl, error);
+ − 49 else
+ − 50 RejectImpl(error);
+ − 51 } else {
+ − 52 m_next.Reject(error);
+ − 53 }
248
+ − 54 }
+ − 55
249
+ − 56 void RejectImpl(Exception error) {
+ − 57 m_rejected(error, m_next);
+ − 58 }
+ − 59
248
+ − 60
249
+ − 61 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
+ − 62 return new PromiseFuncReaction<TIn,TRet>(
+ − 63 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+ − 64 rejected != null ? PromiseHandler.Create(rejected) : null,
+ − 65 new Deferred<TRet>(),
+ − 66 dispatcher
+ − 67 );
248
+ − 68 }
+ − 69
249
+ − 70 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
+ − 71 return new PromiseFuncReaction<TIn,TRet>(
+ − 72 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+ − 73 rejected != null ? PromiseHandler.Create(rejected) : null,
+ − 74 new Deferred<TRet>(),
+ − 75 dispatcher
+ − 76 );
248
+ − 77 }
+ − 78
249
+ − 79 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
+ − 80 return new PromiseFuncReaction<TIn,TRet>(
+ − 81 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+ − 82 rejected != null ? PromiseHandler.Create(rejected) : null,
+ − 83 new Deferred<TRet>(),
+ − 84 dispatcher
+ − 85 );
248
+ − 86 }
+ − 87
249
+ − 88 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
+ − 89 return new PromiseFuncReaction<TIn,TRet>(
+ − 90 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
+ − 91 rejected != null ? PromiseHandler.Create(rejected) : null,
+ − 92 new Deferred<TRet>(),
+ − 93 dispatcher
+ − 94 );
248
+ − 95 }
+ − 96 }
+ − 97 }