annotate Implab/PromiseFuncReaction`1.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 PromiseFuncReaction<TRet> : IResolvable {
247
fb70574741a1 working on promises
cin
parents:
diff changeset
6 readonly Deferred<TRet> 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<Deferred<TRet>> 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<TRet>> 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<TRet> 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 PromiseFuncReaction(Action<Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> 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() {
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);
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();
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(default(TRet));
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
33 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
34 }
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
35
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
36 void ResolveImpl () {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
37 m_fulfilled(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 }
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
54
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
55 public static PromiseFuncReaction<TRet> Create(Func<TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
56 return new PromiseFuncReaction<TRet>(
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<TRet>(),
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 PromiseFuncReaction<TRet> Create(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
65 return new PromiseFuncReaction<TRet>(
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<TRet>(),
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 PromiseFuncReaction<TRet> Create(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
74 return new PromiseFuncReaction<TRet>(
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<TRet>(),
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 PromiseFuncReaction<TRet> Create(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
83 return new PromiseFuncReaction<TRet>(
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<TRet>(),
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 }