annotate Implab/PromiseActionReaction.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents d82909310094
children
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 {
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
5 class PromiseActionReaction : IResolvable {
247
fb70574741a1 working on promises
cin
parents:
diff changeset
6
fb70574741a1 working on promises
cin
parents:
diff changeset
7 readonly Deferred m_next;
fb70574741a1 working on promises
cin
parents:
diff changeset
8
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
9 readonly IDispatcher m_dispatcher;
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
10
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
11 readonly Action<Deferred> m_fulfilled;
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
12
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
13 readonly Action<Exception, Deferred> m_rejected;
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
14
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
15 public IPromise Promise {
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
16 get { return m_next.Promise; }
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
17 }
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
18
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
19 public PromiseActionReaction(Action<Deferred> fulfilled, Action<Exception, Deferred> rejected, Deferred next, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
20 m_next = next;
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
21 m_fulfilled = fulfilled;
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
22 m_rejected = rejected;
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
23 m_dispatcher = dispatcher;
247
fb70574741a1 working on promises
cin
parents:
diff changeset
24 }
fb70574741a1 working on promises
cin
parents:
diff changeset
25
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
26 public void Resolve() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
27 if (m_fulfilled != null) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
28 if (m_dispatcher != null)
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
29 m_dispatcher.Enqueue(ResolveImpl);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
30 else
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
31 ResolveImpl();
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
32 } else {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
33 m_next.Resolve();
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
34 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
35 }
247
fb70574741a1 working on promises
cin
parents:
diff changeset
36
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
37 void ResolveImpl() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
38 m_fulfilled(m_next);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
39 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
40
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
41 public void Reject(Exception error) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
42 if (m_fulfilled != null) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
43 if (m_dispatcher != null)
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
44 m_dispatcher.Enqueue(RejectImpl, error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
45 else
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
46 RejectImpl(error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
47 } else {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
48 m_next.Reject(error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
49 }
247
fb70574741a1 working on promises
cin
parents:
diff changeset
50 }
fb70574741a1 working on promises
cin
parents:
diff changeset
51
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
52 void RejectImpl(Exception error) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
53 m_rejected(error, m_next);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
54 }
247
fb70574741a1 working on promises
cin
parents:
diff changeset
55
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
56 public static PromiseActionReaction Create(Action fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
57 return new PromiseActionReaction(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
58 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
59 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
60 new Deferred(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
61 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
62 );
247
fb70574741a1 working on promises
cin
parents:
diff changeset
63 }
fb70574741a1 working on promises
cin
parents:
diff changeset
64
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
65 public static PromiseActionReaction Create(Func<IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
66 return new PromiseActionReaction(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
67 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
68 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
69 new Deferred(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
70 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
71 );
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
72 }
247
fb70574741a1 working on promises
cin
parents:
diff changeset
73
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
74 public static PromiseActionReaction Create(Action fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
75 return new PromiseActionReaction(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
76 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
77 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
78 new Deferred(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
79 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
80 );
247
fb70574741a1 working on promises
cin
parents:
diff changeset
81 }
fb70574741a1 working on promises
cin
parents:
diff changeset
82
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
83 public static PromiseActionReaction Create(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
84 return new PromiseActionReaction(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
85 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
86 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
87 new Deferred(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
88 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
89 );
247
fb70574741a1 working on promises
cin
parents:
diff changeset
90 }
fb70574741a1 working on promises
cin
parents:
diff changeset
91 }
fb70574741a1 working on promises
cin
parents:
diff changeset
92 }