annotate Implab/PromiseFuncReaction`2.cs @ 248:5cb4826c2c2a v3

Added awaiters to promises Added static methods to Promise Resolve, Reject, All. Updated promise helpers
author cin
date Tue, 30 Jan 2018 01:37:17 +0300
parents
children d82909310094
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 {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5 class PromiseFuncReaction<TIn, TRet> : PromiseReaction<TIn> {
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
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8 public IPromise<TRet> Promise {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
9 get { return m_next.Promise; }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12 public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13 m_next = new Deferred<TRet>(dispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14 if (fulfilled != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
17 if (rejected != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
18 RejectHandler = PromiseHandler.Create(rejected, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
19 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
21 public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
22 m_next = new Deferred<TRet>(dispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 if (fulfilled != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
25
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
26 if (rejected != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
27 RejectHandler = PromiseHandler.Create(rejected, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30 public PromiseFuncReaction(Func<TIn, TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, IDispatcher dispatcher) : base(dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
31 m_next = new Deferred<TRet>(dispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 if (fulfilled != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
34
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
35 if (rejected != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
36 RejectHandler = PromiseHandler.Create(rejected, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
39 public PromiseFuncReaction(Func<TIn, IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, IDispatcher dispatcher) : base(dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40 m_next = new Deferred<TRet>(dispatcher);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
41 if (fulfilled != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
42 FulfilHandler = PromiseHandler.Create(fulfilled, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
43
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
44 if (rejected != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
45 RejectHandler = PromiseHandler.Create(rejected, m_next);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
46 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
47
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
48 protected override void DefaultReject(Exception reason) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
49 m_next.Reject(reason);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
50 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
51
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
52 protected override void DefaultResolve(TIn result) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
53 m_next.Resolve((TRet)(object)result);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
54 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
55 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
56 }