annotate Implab/PromiseFuncReaction`2.cs @ 275:6fefd5811b9b v3

refactoring
author cin
date Fri, 27 Apr 2018 16:57:30 +0300
parents d82909310094
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
1 using System;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
2 using System.Diagnostics;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 namespace Implab {
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
5 class PromiseFuncReaction<TIn, TRet> : IResolvable<TIn> {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
6 readonly Deferred<TRet> m_next;
5cb4826c2c2a Added awaiters to 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<TIn, 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
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
14 public IPromise<TRet> Promise {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 get { return m_next.Promise; }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
17
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
18 public PromiseFuncReaction(Action<TIn, 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;
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
25 public void Resolve(TIn 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 try {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
33 m_next.Resolve((TRet)(object)result);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
34 } catch(Exception error) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
35 // handle cast exceptions
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
36 m_next.Reject(error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
37 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
38 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
39 }
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
41 void ResolveImpl (TIn result) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
42 m_fulfilled(result, m_next);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
43 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
44
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
45 public void Reject(Exception error) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
46 if (m_fulfilled != null) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
47 if (m_dispatcher != null)
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
48 m_dispatcher.Enqueue(RejectImpl, error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
49 else
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
50 RejectImpl(error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
51 } else {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
52 m_next.Reject(error);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
53 }
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
54 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
55
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
56 void RejectImpl(Exception error) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
57 m_rejected(error, m_next);
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
58 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
59
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
60
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
61 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
62 return new PromiseFuncReaction<TIn,TRet>(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
63 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
64 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
65 new Deferred<TRet>(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
66 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
67 );
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
68 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
69
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
70 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
71 return new PromiseFuncReaction<TIn,TRet>(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
72 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
73 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
74 new Deferred<TRet>(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
75 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
76 );
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
77 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
78
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
79 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
80 return new PromiseFuncReaction<TIn,TRet>(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
81 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
82 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
83 new Deferred<TRet>(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
84 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
85 );
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
86 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
87
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
88 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
89 return new PromiseFuncReaction<TIn,TRet>(
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
90 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
91 rejected != null ? PromiseHandler.Create(rejected) : null,
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
92 new Deferred<TRet>(),
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
93 dispatcher
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
94 );
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
95 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
96 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
97 }