Mercurial > pub > ImplabNet
diff Implab/PromiseActionReaction`1.cs @ 249:d82909310094 v3
Implab.Test moved to xunit
Complete set of PromiseHelpers (Then, Catch, Finally)
Removed obsolete types ICancellable, ICancellationToken
author | cin |
---|---|
date | Wed, 31 Jan 2018 11:28:38 +0300 |
parents | 5cb4826c2c2a |
children |
line wrap: on
line diff
--- a/Implab/PromiseActionReaction`1.cs Tue Jan 30 01:37:17 2018 +0300 +++ b/Implab/PromiseActionReaction`1.cs Wed Jan 31 11:28:38 2018 +0300 @@ -2,55 +2,90 @@ using System.Diagnostics; namespace Implab { - class PromiseActionReaction<T> : PromiseReaction<T> { + class PromiseActionReaction<T> : IResolvable<T> { readonly Deferred m_next; + readonly IDispatcher m_dispatcher; + + readonly Action<T, Deferred> m_fulfilled; + + readonly Action<Exception, Deferred> m_rejected; + public IPromise Promise { get { return m_next.Promise; } } - public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) { - m_next = new Deferred(dispatcher); - if (fulfilled != null) - FulfilHandler = PromiseHandler.Create(fulfilled, m_next); - - if (rejected != null) - RejectHandler = PromiseHandler.Create(rejected, m_next); + public PromiseActionReaction(Action<T, Deferred> fulfilled, Action<Exception, Deferred> rejected, Deferred next, IDispatcher dispatcher) { + m_next = next; + m_fulfilled = fulfilled; + m_rejected = rejected; + m_dispatcher = dispatcher; } - public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) { - m_next = new Deferred(dispatcher); - if (fulfilled != null) - FulfilHandler = PromiseHandler.Create(fulfilled, m_next); + public void Resolve(T result) { + if (m_fulfilled != null) { + if (m_dispatcher != null) + m_dispatcher.Enqueue(ResolveImpl, result); + else + ResolveImpl(result); + } else { + m_next.Resolve(); + } + } - if (rejected != null) - RejectHandler = PromiseHandler.Create(rejected, m_next); + void ResolveImpl (T 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); + } } - public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) { - m_next = new Deferred(dispatcher); - if (fulfilled != null) - FulfilHandler = PromiseHandler.Create(fulfilled, m_next); + void RejectImpl(Exception error) { + m_rejected(error, m_next); + } - if (rejected != null) - RejectHandler = PromiseHandler.Create(rejected, m_next); + public static PromiseActionReaction<T> Create(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) { + return new PromiseActionReaction<T>( + fulfilled != null ? PromiseHandler.Create(fulfilled) : null, + rejected != null ? PromiseHandler.Create(rejected) : null, + new Deferred(), + dispatcher + ); } - public PromiseActionReaction(Func<T, IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) { - m_next = new Deferred(dispatcher); - if (fulfilled != null) - FulfilHandler = PromiseHandler.Create(fulfilled, m_next); - - if (rejected != null) - RejectHandler = PromiseHandler.Create(rejected, m_next); + public static PromiseActionReaction<T> Create(Func<T,IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) { + return new PromiseActionReaction<T>( + fulfilled != null ? PromiseHandler.Create(fulfilled) : null, + rejected != null ? PromiseHandler.Create(rejected) : null, + new Deferred(), + dispatcher + ); } - protected override void DefaultReject(Exception reason) { - m_next.Reject(reason); + public static PromiseActionReaction<T> Create(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) { + return new PromiseActionReaction<T>( + fulfilled != null ? PromiseHandler.Create(fulfilled) : null, + rejected != null ? PromiseHandler.Create(rejected) : null, + new Deferred(), + dispatcher + ); } - protected override void DefaultResolve(T result) { - m_next.Resolve(); + public static PromiseActionReaction<T> Create(Func<T,IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) { + return new PromiseActionReaction<T>( + fulfilled != null ? PromiseHandler.Create(fulfilled) : null, + rejected != null ? PromiseHandler.Create(rejected) : null, + new Deferred(), + dispatcher + ); } } } \ No newline at end of file