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