annotate Implab/PromiseActionReaction.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents
children 5cb4826c2c2a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
247
fb70574741a1 working on promises
cin
parents:
diff changeset
1 using System;
fb70574741a1 working on promises
cin
parents:
diff changeset
2 using System.Diagnostics;
fb70574741a1 working on promises
cin
parents:
diff changeset
3
fb70574741a1 working on promises
cin
parents:
diff changeset
4 namespace Implab {
fb70574741a1 working on promises
cin
parents:
diff changeset
5 class PromiseActionReaction : PromiseReaction {
fb70574741a1 working on promises
cin
parents:
diff changeset
6 readonly Action m_fulfilled;
fb70574741a1 working on promises
cin
parents:
diff changeset
7
fb70574741a1 working on promises
cin
parents:
diff changeset
8 readonly Action<Exception> m_rejected;
fb70574741a1 working on promises
cin
parents:
diff changeset
9
fb70574741a1 working on promises
cin
parents:
diff changeset
10 readonly Deferred m_next;
fb70574741a1 working on promises
cin
parents:
diff changeset
11
fb70574741a1 working on promises
cin
parents:
diff changeset
12 public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
13 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
14 m_fulfilled = () => {
fb70574741a1 working on promises
cin
parents:
diff changeset
15 fulfilled();
fb70574741a1 working on promises
cin
parents:
diff changeset
16 next.Resolve();
fb70574741a1 working on promises
cin
parents:
diff changeset
17 };
fb70574741a1 working on promises
cin
parents:
diff changeset
18
fb70574741a1 working on promises
cin
parents:
diff changeset
19 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
20 m_rejected = (x) => {
fb70574741a1 working on promises
cin
parents:
diff changeset
21 rejected(x);
fb70574741a1 working on promises
cin
parents:
diff changeset
22 next.Resolve();
fb70574741a1 working on promises
cin
parents:
diff changeset
23 };
fb70574741a1 working on promises
cin
parents:
diff changeset
24 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
25 }
fb70574741a1 working on promises
cin
parents:
diff changeset
26
fb70574741a1 working on promises
cin
parents:
diff changeset
27 public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
28 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
29 m_fulfilled = () => { next.Resolve(fulfilled()); };
fb70574741a1 working on promises
cin
parents:
diff changeset
30 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
31 m_rejected = (e) => { next.Resolve(rejected(e)); };
fb70574741a1 working on promises
cin
parents:
diff changeset
32 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
33 }
fb70574741a1 working on promises
cin
parents:
diff changeset
34
fb70574741a1 working on promises
cin
parents:
diff changeset
35 public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
36 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
37 m_fulfilled = () => {
fb70574741a1 working on promises
cin
parents:
diff changeset
38 fulfilled();
fb70574741a1 working on promises
cin
parents:
diff changeset
39 next.Resolve();
fb70574741a1 working on promises
cin
parents:
diff changeset
40 };
fb70574741a1 working on promises
cin
parents:
diff changeset
41
fb70574741a1 working on promises
cin
parents:
diff changeset
42 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
43 m_rejected = (e) => { next.Resolve(rejected(e)); };
fb70574741a1 working on promises
cin
parents:
diff changeset
44 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
45 }
fb70574741a1 working on promises
cin
parents:
diff changeset
46
fb70574741a1 working on promises
cin
parents:
diff changeset
47 public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
48 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
49 m_fulfilled = () => { next.Resolve(fulfilled()); };
fb70574741a1 working on promises
cin
parents:
diff changeset
50
fb70574741a1 working on promises
cin
parents:
diff changeset
51 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
52 m_rejected = (x) => {
fb70574741a1 working on promises
cin
parents:
diff changeset
53 rejected(x);
fb70574741a1 working on promises
cin
parents:
diff changeset
54 next.Resolve();
fb70574741a1 working on promises
cin
parents:
diff changeset
55 };
fb70574741a1 working on promises
cin
parents:
diff changeset
56 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
57 }
fb70574741a1 working on promises
cin
parents:
diff changeset
58
fb70574741a1 working on promises
cin
parents:
diff changeset
59
fb70574741a1 working on promises
cin
parents:
diff changeset
60 protected override bool HasFulfilHandler => m_fulfilled != null;
fb70574741a1 working on promises
cin
parents:
diff changeset
61
fb70574741a1 working on promises
cin
parents:
diff changeset
62 protected override bool HasRejectHandler => m_rejected != null;
fb70574741a1 working on promises
cin
parents:
diff changeset
63
fb70574741a1 working on promises
cin
parents:
diff changeset
64 protected override void DefaultReject(Exception reason) {
fb70574741a1 working on promises
cin
parents:
diff changeset
65 m_next.Reject(reason);
fb70574741a1 working on promises
cin
parents:
diff changeset
66 }
fb70574741a1 working on promises
cin
parents:
diff changeset
67
fb70574741a1 working on promises
cin
parents:
diff changeset
68 protected override void DefaultResolve() {
fb70574741a1 working on promises
cin
parents:
diff changeset
69 m_next.Resolve();
fb70574741a1 working on promises
cin
parents:
diff changeset
70 }
fb70574741a1 working on promises
cin
parents:
diff changeset
71
fb70574741a1 working on promises
cin
parents:
diff changeset
72 protected override void RejectImpl(Exception reason) {
fb70574741a1 working on promises
cin
parents:
diff changeset
73 try {
fb70574741a1 working on promises
cin
parents:
diff changeset
74 m_rejected(reason);
fb70574741a1 working on promises
cin
parents:
diff changeset
75 } catch (Exception e){
fb70574741a1 working on promises
cin
parents:
diff changeset
76 m_next.Reject(e);
fb70574741a1 working on promises
cin
parents:
diff changeset
77 }
fb70574741a1 working on promises
cin
parents:
diff changeset
78 }
fb70574741a1 working on promises
cin
parents:
diff changeset
79
fb70574741a1 working on promises
cin
parents:
diff changeset
80 protected override void ResolveImpl() {
fb70574741a1 working on promises
cin
parents:
diff changeset
81 try {
fb70574741a1 working on promises
cin
parents:
diff changeset
82 m_fulfilled();
fb70574741a1 working on promises
cin
parents:
diff changeset
83 } catch (Exception e){
fb70574741a1 working on promises
cin
parents:
diff changeset
84 m_next.Reject(e);
fb70574741a1 working on promises
cin
parents:
diff changeset
85 }
fb70574741a1 working on promises
cin
parents:
diff changeset
86 }
fb70574741a1 working on promises
cin
parents:
diff changeset
87 }
fb70574741a1 working on promises
cin
parents:
diff changeset
88 }