comparison Implab/PromiseActionReaction`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 PromiseActionReaction<T> : PromiseReaction<T> {
6 readonly Action<T> m_fulfilled;
7
8 readonly Action<Exception> m_rejected;
9
10 readonly Deferred m_next;
11
12 public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
13 if (fulfilled != null)
14 m_fulfilled = (x) => {
15 fulfilled(x);
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<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
28 if (fulfilled != null)
29 m_fulfilled = (x) => { next.Resolve(fulfilled(x)); };
30 if (rejected != null)
31 m_rejected = (e) => { next.Resolve(rejected(e)); };
32 m_next = next;
33 }
34
35 public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
36 if (fulfilled != null)
37 m_fulfilled = (x) => {
38 fulfilled(x);
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<T, IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
48 if (fulfilled != null)
49 m_fulfilled = (x) => { next.Resolve(fulfilled(x)); };
50
51 if (rejected != null)
52 m_rejected = (x) => {
53 rejected(x);
54 next.Resolve();
55 };
56 m_next = next;
57 }
58
59 protected override bool HasFulfilHandler => m_fulfilled != null;
60
61 protected override bool HasRejectHandler => m_rejected != null;
62
63 protected override void DefaultReject(Exception reason) {
64 m_next.Reject(reason);
65 }
66
67 protected override void DefaultResolve(T result) {
68 m_next.Resolve();
69 }
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 }
87 }