comparison Implab/PromiseFuncReaction`1.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents
children 5cb4826c2c2a
comparison
equal deleted inserted replaced
246:5aa9cfbe56c3 247:fb70574741a1
1 using System;
2 using System.Diagnostics;
3
4 namespace Implab {
5 class PromiseFuncReaction<TRet> : PromiseReaction {
6 readonly Action m_fulfilled;
7
8 readonly Action<Exception> m_rejected;
9
10 readonly Deferred<TRet> m_next;
11
12 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
13 if (fulfilled != null)
14 m_fulfilled = () => { next.Resolve(fulfilled()); };
15
16 if (rejected != null)
17 m_rejected = (e) => { next.Resolve(rejected(e)); };
18 m_next = next;
19 }
20
21 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
22 if (fulfilled != null)
23 m_fulfilled = () => { next.Resolve(fulfilled()); };
24 if (rejected != null)
25 m_rejected = (e) => { next.Resolve(rejected(e)); };
26 m_next = next;
27 }
28
29 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
30 if (fulfilled != null)
31 m_fulfilled = () => { next.Resolve(fulfilled()); };
32 if (rejected != null)
33 m_rejected = (e) => { next.Resolve(rejected(e)); };
34
35 m_next = next;
36 }
37
38 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
39 if (fulfilled != null)
40 m_fulfilled = () => { next.Resolve(fulfilled()); };
41 if (rejected != null)
42 m_rejected = (e) => { next.Resolve(rejected(e)); };
43
44 m_next = next;
45 }
46
47
48 protected override bool HasFulfilHandler => m_fulfilled != null;
49
50 protected override bool HasRejectHandler => m_rejected != null;
51
52 protected override void DefaultReject(Exception reason) {
53 m_next.Reject(reason);
54 }
55
56 protected override void DefaultResolve() {
57 throw new NotImplementedException();
58 }
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 }
76 }