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