Mercurial > pub > ImplabNet
comparison Implab/PromiseActionReaction`1.cs @ 248:5cb4826c2c2a v3
Added awaiters to promises
Added static methods to Promise Resolve, Reject, All.
Updated promise helpers
| author | cin |
|---|---|
| date | Tue, 30 Jan 2018 01:37:17 +0300 |
| parents | fb70574741a1 |
| children | d82909310094 |
comparison
equal
deleted
inserted
replaced
| 247:fb70574741a1 | 248:5cb4826c2c2a |
|---|---|
| 1 using System; | 1 using System; |
| 2 using System.Diagnostics; | 2 using System.Diagnostics; |
| 3 | 3 |
| 4 namespace Implab { | 4 namespace Implab { |
| 5 class PromiseActionReaction<T> : PromiseReaction<T> { | 5 class PromiseActionReaction<T> : PromiseReaction<T> { |
| 6 readonly Action<T> m_fulfilled; | |
| 7 | |
| 8 readonly Action<Exception> m_rejected; | |
| 9 | |
| 10 readonly Deferred m_next; | 6 readonly Deferred m_next; |
| 11 | 7 |
| 12 public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | 8 public IPromise Promise { |
| 9 get { return m_next.Promise; } | |
| 10 } | |
| 11 | |
| 12 public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) { | |
| 13 m_next = new Deferred(dispatcher); | |
| 13 if (fulfilled != null) | 14 if (fulfilled != null) |
| 14 m_fulfilled = (x) => { | 15 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); |
| 15 fulfilled(x); | |
| 16 next.Resolve(); | |
| 17 }; | |
| 18 | 16 |
| 19 if (rejected != null) | 17 if (rejected != null) |
| 20 m_rejected = (x) => { | 18 RejectHandler = PromiseHandler.Create(rejected, m_next); |
| 21 rejected(x); | |
| 22 next.Resolve(); | |
| 23 }; | |
| 24 m_next = next; | |
| 25 } | 19 } |
| 26 | 20 |
| 27 public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | 21 public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) { |
| 22 m_next = new Deferred(dispatcher); | |
| 28 if (fulfilled != null) | 23 if (fulfilled != null) |
| 29 m_fulfilled = (x) => { next.Resolve(fulfilled(x)); }; | 24 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); |
| 25 | |
| 30 if (rejected != null) | 26 if (rejected != null) |
| 31 m_rejected = (e) => { next.Resolve(rejected(e)); }; | 27 RejectHandler = PromiseHandler.Create(rejected, m_next); |
| 32 m_next = next; | |
| 33 } | 28 } |
| 34 | 29 |
| 35 public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | 30 public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) { |
| 31 m_next = new Deferred(dispatcher); | |
| 36 if (fulfilled != null) | 32 if (fulfilled != null) |
| 37 m_fulfilled = (x) => { | 33 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); |
| 38 fulfilled(x); | |
| 39 next.Resolve(); | |
| 40 }; | |
| 41 | 34 |
| 42 if (rejected != null) | 35 if (rejected != null) |
| 43 m_rejected = (e) => { next.Resolve(rejected(e)); }; | 36 RejectHandler = PromiseHandler.Create(rejected, m_next); |
| 44 m_next = next; | |
| 45 } | 37 } |
| 46 | 38 |
| 47 public PromiseActionReaction(Func<T, IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | 39 public PromiseActionReaction(Func<T, IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) { |
| 40 m_next = new Deferred(dispatcher); | |
| 48 if (fulfilled != null) | 41 if (fulfilled != null) |
| 49 m_fulfilled = (x) => { next.Resolve(fulfilled(x)); }; | 42 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); |
| 50 | 43 |
| 51 if (rejected != null) | 44 if (rejected != null) |
| 52 m_rejected = (x) => { | 45 RejectHandler = PromiseHandler.Create(rejected, m_next); |
| 53 rejected(x); | |
| 54 next.Resolve(); | |
| 55 }; | |
| 56 m_next = next; | |
| 57 } | 46 } |
| 58 | |
| 59 protected override bool HasFulfilHandler => m_fulfilled != null; | |
| 60 | |
| 61 protected override bool HasRejectHandler => m_rejected != null; | |
| 62 | 47 |
| 63 protected override void DefaultReject(Exception reason) { | 48 protected override void DefaultReject(Exception reason) { |
| 64 m_next.Reject(reason); | 49 m_next.Reject(reason); |
| 65 } | 50 } |
| 66 | 51 |
| 67 protected override void DefaultResolve(T result) { | 52 protected override void DefaultResolve(T result) { |
| 68 m_next.Resolve(); | 53 m_next.Resolve(); |
| 69 } | 54 } |
| 70 | |
| 71 protected override void RejectImpl(Exception reason) { | |
| 72 try { | |
| 73 m_rejected(reason); | |
| 74 } catch (Exception e) { | |
| 75 m_next.Reject(e); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 protected override void ResolveImpl(T result) { | |
| 80 try { | |
| 81 m_fulfilled(result); | |
| 82 } catch (Exception e) { | |
| 83 m_next.Reject(e); | |
| 84 } | |
| 85 } | |
| 86 } | 55 } |
| 87 } | 56 } |
