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