247
|
1 using System;
|
|
2 using System.Diagnostics;
|
|
3
|
|
4 namespace Implab {
|
249
|
5 class PromiseFuncReaction<TRet> : IResolvable {
|
247
|
6 readonly Deferred<TRet> m_next;
|
|
7
|
249
|
8 readonly IDispatcher m_dispatcher;
|
|
9
|
|
10 readonly Action<Deferred<TRet>> m_fulfilled;
|
|
11
|
|
12 readonly Action<Exception, Deferred<TRet>> m_rejected;
|
|
13
|
248
|
14 public IPromise<TRet> Promise {
|
|
15 get { return m_next.Promise; }
|
|
16 }
|
|
17
|
249
|
18 public PromiseFuncReaction(Action<Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> 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() {
|
|
26 if (m_fulfilled != null) {
|
|
27 if (m_dispatcher != null)
|
|
28 m_dispatcher.Enqueue(ResolveImpl);
|
|
29 else
|
|
30 ResolveImpl();
|
|
31 } else {
|
|
32 m_next.Resolve(default(TRet));
|
|
33 }
|
|
34 }
|
248
|
35
|
249
|
36 void ResolveImpl () {
|
|
37 m_fulfilled(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 }
|
248
|
54
|
249
|
55 public static PromiseFuncReaction<TRet> Create(Func<TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
|
|
56 return new PromiseFuncReaction<TRet>(
|
|
57 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
58 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
59 new Deferred<TRet>(),
|
|
60 dispatcher
|
|
61 );
|
247
|
62 }
|
|
63
|
249
|
64 public static PromiseFuncReaction<TRet> Create(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
|
|
65 return new PromiseFuncReaction<TRet>(
|
|
66 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
67 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
68 new Deferred<TRet>(),
|
|
69 dispatcher
|
|
70 );
|
248
|
71 }
|
247
|
72
|
249
|
73 public static PromiseFuncReaction<TRet> Create(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
|
|
74 return new PromiseFuncReaction<TRet>(
|
|
75 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
76 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
77 new Deferred<TRet>(),
|
|
78 dispatcher
|
|
79 );
|
247
|
80 }
|
|
81
|
249
|
82 public static PromiseFuncReaction<TRet> Create(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
|
|
83 return new PromiseFuncReaction<TRet>(
|
|
84 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
|
|
85 rejected != null ? PromiseHandler.Create(rejected) : null,
|
|
86 new Deferred<TRet>(),
|
|
87 dispatcher
|
|
88 );
|
247
|
89 }
|
|
90 }
|
|
91 } |