comparison Implab/PromiseActionReaction.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 : PromiseReaction { 5 class PromiseActionReaction : PromiseReaction {
6 readonly Action m_fulfilled;
7
8 readonly Action<Exception> m_rejected;
9 6
10 readonly Deferred m_next; 7 readonly Deferred m_next;
11 8
12 public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { 9 public IPromise Promise {
10 get { return m_next.Promise; }
11 }
12
13 public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
14 m_next = new Deferred(dispatcher);
13 if (fulfilled != null) 15 if (fulfilled != null)
14 m_fulfilled = () => { 16 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
15 fulfilled();
16 next.Resolve();
17 };
18 17
19 if (rejected != null) 18 if (rejected != null)
20 m_rejected = (x) => { 19 RejectHandler = PromiseHandler.Create(rejected, m_next);
21 rejected(x);
22 next.Resolve();
23 };
24 m_next = next;
25 } 20 }
26 21
27 public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { 22 public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
23 m_next = new Deferred(dispatcher);
28 if (fulfilled != null) 24 if (fulfilled != null)
29 m_fulfilled = () => { next.Resolve(fulfilled()); }; 25 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
26
30 if (rejected != null) 27 if (rejected != null)
31 m_rejected = (e) => { next.Resolve(rejected(e)); }; 28 RejectHandler = PromiseHandler.Create(rejected, m_next);
32 m_next = next;
33 } 29 }
34 30
35 public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { 31 public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) : base(dispatcher) {
32 m_next = new Deferred(dispatcher);
36 if (fulfilled != null) 33 if (fulfilled != null)
37 m_fulfilled = () => { 34 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
38 fulfilled();
39 next.Resolve();
40 };
41 35
42 if (rejected != null) 36 if (rejected != null)
43 m_rejected = (e) => { next.Resolve(rejected(e)); }; 37 RejectHandler = PromiseHandler.Create(rejected, m_next);
44 m_next = next;
45 } 38 }
46 39
47 public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { 40 public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) : base(dispatcher) {
41 m_next = new Deferred(dispatcher);
48 if (fulfilled != null) 42 if (fulfilled != null)
49 m_fulfilled = () => { next.Resolve(fulfilled()); }; 43 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
50 44
51 if (rejected != null) 45 if (rejected != null)
52 m_rejected = (x) => { 46 RejectHandler = PromiseHandler.Create(rejected, m_next);
53 rejected(x);
54 next.Resolve();
55 };
56 m_next = next;
57 } 47 }
58
59
60 protected override bool HasFulfilHandler => m_fulfilled != null;
61
62 protected override bool HasRejectHandler => m_rejected != null;
63 48
64 protected override void DefaultReject(Exception reason) { 49 protected override void DefaultReject(Exception reason) {
65 m_next.Reject(reason); 50 m_next.Reject(reason);
66 } 51 }
67 52
68 protected override void DefaultResolve() { 53 protected override void DefaultResolve() {
69 m_next.Resolve(); 54 m_next.Resolve();
70 } 55 }
71
72 protected override void RejectImpl(Exception reason) {
73 try {
74 m_rejected(reason);
75 } catch (Exception e){
76 m_next.Reject(e);
77 }
78 }
79
80 protected override void ResolveImpl() {
81 try {
82 m_fulfilled();
83 } catch (Exception e){
84 m_next.Reject(e);
85 }
86 }
87 } 56 }
88 } 57 }