247
|
1 using System;
|
|
2 using System.Diagnostics;
|
|
3
|
|
4 namespace Implab {
|
249
|
5 class PromiseActionReaction<T> : IResolvable<T> {
|
247
|
6 readonly Deferred m_next;
|
|
7
|
249
|
8 readonly IDispatcher m_dispatcher;
|
|
9
|
|
10 readonly Action<T, Deferred> m_fulfilled;
|
|
11
|
|
12 readonly Action<Exception, Deferred> m_rejected;
|
|
13
|
248
|
14 public IPromise Promise {
|
|
15 get { return m_next.Promise; }
|
|
16 }
|
|
17
|
249
|
18 public PromiseActionReaction(Action<T, Deferred> fulfilled, Action<Exception, Deferred> rejected, Deferred next, IDispatcher dispatcher) {
|
|
19 m_next = next;
|
|
20 m_fulfilled = fulfilled;
|
|
21 m_rejected = rejected;
|
|
22 m_dispatcher = dispatcher;
|
247
|
23 }
|
|
24
|
249
|
25 public void Resolve(T result) {
|
|
26 if (m_fulfilled != null) {
|
|
27 if (m_dispatcher != null)
|
|
28 m_dispatcher.Enqueue(ResolveImpl, result);
|
|
29 else
|
|
30 ResolveImpl(result);
|
|
31 } else {
|
|
32 m_next.Resolve();
|
|
33 }
|
|
34 }
|
247
|
35
|
249
|
36 void ResolveImpl (T result) {
|
|
37 m_fulfilled(result, m_next);
|
|
38 }
|
|
39
|
|
40 public void Reject(Exception error) {
|
|
41 if (m_fulfilled != null) {
|
|
42 if (m_dispatcher != null)
|
|
43 m_dispatcher.Enqueue(RejectImpl, error);
|
|
44 else
|
|
45 RejectImpl(error);
|
|
46 } else {
|
|
47 m_next.Reject(error);
|
|
48 }
|
247
|
49 }
|
|
50
|
249
|
51 void RejectImpl(Exception error) {
|
|
52 m_rejected(error, m_next);
|
|
53 }
|
247
|
54
|
249
|
55 public static PromiseActionReaction<T> Create(Action<T> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
|
|
56 return new PromiseActionReaction<T>(
|
|
57 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
58 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
59 new Deferred(),
|
|
60 dispatcher
|
|
61 );
|
247
|
62 }
|
|
63
|
249
|
64 public static PromiseActionReaction<T> Create(Func<T,IPromise> fulfilled, Action<Exception> rejected, IDispatcher dispatcher) {
|
|
65 return new PromiseActionReaction<T>(
|
|
66 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
67 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
68 new Deferred(),
|
|
69 dispatcher
|
|
70 );
|
248
|
71 }
|
247
|
72
|
249
|
73 public static PromiseActionReaction<T> Create(Action<T> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
|
|
74 return new PromiseActionReaction<T>(
|
|
75 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
76 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
77 new Deferred(),
|
|
78 dispatcher
|
|
79 );
|
247
|
80 }
|
|
81
|
249
|
82 public static PromiseActionReaction<T> Create(Func<T,IPromise> fulfilled, Func<Exception, IPromise> rejected, IDispatcher dispatcher) {
|
|
83 return new PromiseActionReaction<T>(
|
|
84 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
85 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
86 new Deferred(),
|
|
87 dispatcher
|
|
88 );
|
247
|
89 }
|
|
90 }
|
|
91 } |