annotate Implab/PromiseActionReaction`1.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<T> : PromiseReaction<T> {
fb70574741a1 working on promises
cin
parents:
diff changeset
6 readonly Action<T> 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<T> 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 = (x) => {
fb70574741a1 working on promises
cin
parents:
diff changeset
15 fulfilled(x);
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<T, 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 = (x) => { next.Resolve(fulfilled(x)); };
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<T> 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 = (x) => {
fb70574741a1 working on promises
cin
parents:
diff changeset
38 fulfilled(x);
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<T, 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 = (x) => { next.Resolve(fulfilled(x)); };
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 protected override bool HasFulfilHandler => m_fulfilled != null;
fb70574741a1 working on promises
cin
parents:
diff changeset
60
fb70574741a1 working on promises
cin
parents:
diff changeset
61 protected override bool HasRejectHandler => m_rejected != null;
fb70574741a1 working on promises
cin
parents:
diff changeset
62
fb70574741a1 working on promises
cin
parents:
diff changeset
63 protected override void DefaultReject(Exception reason) {
fb70574741a1 working on promises
cin
parents:
diff changeset
64 m_next.Reject(reason);
fb70574741a1 working on promises
cin
parents:
diff changeset
65 }
fb70574741a1 working on promises
cin
parents:
diff changeset
66
fb70574741a1 working on promises
cin
parents:
diff changeset
67 protected override void DefaultResolve(T result) {
fb70574741a1 working on promises
cin
parents:
diff changeset
68 m_next.Resolve();
fb70574741a1 working on promises
cin
parents:
diff changeset
69 }
fb70574741a1 working on promises
cin
parents:
diff changeset
70
fb70574741a1 working on promises
cin
parents:
diff changeset
71 protected override void RejectImpl(Exception reason) {
fb70574741a1 working on promises
cin
parents:
diff changeset
72 try {
fb70574741a1 working on promises
cin
parents:
diff changeset
73 m_rejected(reason);
fb70574741a1 working on promises
cin
parents:
diff changeset
74 } catch (Exception e) {
fb70574741a1 working on promises
cin
parents:
diff changeset
75 m_next.Reject(e);
fb70574741a1 working on promises
cin
parents:
diff changeset
76 }
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 protected override void ResolveImpl(T result) {
fb70574741a1 working on promises
cin
parents:
diff changeset
80 try {
fb70574741a1 working on promises
cin
parents:
diff changeset
81 m_fulfilled(result);
fb70574741a1 working on promises
cin
parents:
diff changeset
82 } catch (Exception e) {
fb70574741a1 working on promises
cin
parents:
diff changeset
83 m_next.Reject(e);
fb70574741a1 working on promises
cin
parents:
diff changeset
84 }
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 }