comparison Implab/PromiseFuncReaction`2.cs @ 249:d82909310094 v3

Implab.Test moved to xunit Complete set of PromiseHelpers (Then, Catch, Finally) Removed obsolete types ICancellable, ICancellationToken
author cin
date Wed, 31 Jan 2018 11:28:38 +0300
parents 5cb4826c2c2a
children
comparison
equal deleted inserted replaced
248:5cb4826c2c2a 249:d82909310094
1 using System; 1 using System;
2 using System.Diagnostics; 2 using System.Diagnostics;
3 3
4 namespace Implab { 4 namespace Implab {
5 class PromiseFuncReaction<TIn, TRet> : PromiseReaction<TIn> { 5 class PromiseFuncReaction<TIn, TRet> : IResolvable<TIn> {
6 readonly Deferred<TRet> m_next; 6 readonly Deferred<TRet> m_next;
7 7
8 public IPromise<TRet> Promise { 8 readonly IDispatcher m_dispatcher;
9
10 readonly Action<TIn, Deferred<TRet>> m_fulfilled;
11
12 readonly Action<Exception, Deferred<TRet>> m_rejected;
13
14 public IPromise<TRet> Promise {
9 get { return m_next.Promise; } 15 get { return m_next.Promise; }
10 } 16 }
11 17
12 public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) { 18 public PromiseFuncReaction(Action<TIn, Deferred<TRet>> fulfilled, Action<Exception, Deferred<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) {
13 m_next = new Deferred<TRet>(dispatcher); 19 m_next = next;
14 if (fulfilled != null) 20 m_fulfilled = fulfilled;
15 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); 21 m_rejected = rejected;
16 22 m_dispatcher = dispatcher;
17 if (rejected != null)
18 RejectHandler = PromiseHandler.Create(rejected, m_next);
19 } 23 }
20 24
21 public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) { 25 public void Resolve(TIn result) {
22 m_next = new Deferred<TRet>(dispatcher); 26 if (m_fulfilled != null) {
23 if (fulfilled != null) 27 if (m_dispatcher != null)
24 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); 28 m_dispatcher.Enqueue(ResolveImpl, result);
25 29 else
26 if (rejected != null) 30 ResolveImpl(result);
27 RejectHandler = PromiseHandler.Create(rejected, m_next); 31 } else {
32 try {
33 m_next.Resolve((TRet)(object)result);
34 } catch(Exception error) {
35 // handle cast exceptions
36 m_next.Reject(error);
37 }
38 }
28 } 39 }
29 40
30 public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) { 41 void ResolveImpl (TIn result) {
31 m_next = new Deferred<TRet>(dispatcher); 42 m_fulfilled(result, m_next);
32 if (fulfilled != null)
33 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
34
35 if (rejected != null)
36 RejectHandler = PromiseHandler.Create(rejected, m_next);
37 } 43 }
38 44
39 public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) { 45 public void Reject(Exception error) {
40 m_next = new Deferred<TRet>(dispatcher); 46 if (m_fulfilled != null) {
41 if (fulfilled != null) 47 if (m_dispatcher != null)
42 FulfilHandler = PromiseHandler.Create(fulfilled, m_next); 48 m_dispatcher.Enqueue(RejectImpl, error);
43 49 else
44 if (rejected != null) 50 RejectImpl(error);
45 RejectHandler = PromiseHandler.Create(rejected, m_next); 51 } else {
52 m_next.Reject(error);
53 }
46 } 54 }
47 55
48 protected override void DefaultReject(Exception reason) { 56 void RejectImpl(Exception error) {
49 m_next.Reject(reason); 57 m_rejected(error, m_next);
50 } 58 }
51 59
52 protected override void DefaultResolve(TIn result) { 60
53 m_next.Resolve((TRet)(object)result); 61 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
62 return new PromiseFuncReaction<TIn,TRet>(
63 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
64 rejected != null ? PromiseHandler.Create(rejected) : null,
65 new Deferred<TRet>(),
66 dispatcher
67 );
68 }
69
70 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) {
71 return new PromiseFuncReaction<TIn,TRet>(
72 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
73 rejected != null ? PromiseHandler.Create(rejected) : null,
74 new Deferred<TRet>(),
75 dispatcher
76 );
77 }
78
79 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
80 return new PromiseFuncReaction<TIn,TRet>(
81 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
82 rejected != null ? PromiseHandler.Create(rejected) : null,
83 new Deferred<TRet>(),
84 dispatcher
85 );
86 }
87
88 public static PromiseFuncReaction<TIn,TRet> Create(Func<TIn,IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) {
89 return new PromiseFuncReaction<TIn,TRet>(
90 fulfilled != null ? PromiseHandler.Create(fulfilled) : null,
91 rejected != null ? PromiseHandler.Create(rejected) : null,
92 new Deferred<TRet>(),
93 dispatcher
94 );
54 } 95 }
55 } 96 }
56 } 97 }