annotate Implab/PromiseActionReaction`1.cs @ 255:b00441e04738 v3

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