annotate Implab/PromiseFuncReaction`1.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents
children 5cb4826c2c2a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
247
fb70574741a1 working on promises
cin
parents:
diff changeset
1 using System;
fb70574741a1 working on promises
cin
parents:
diff changeset
2 using System.Diagnostics;
fb70574741a1 working on promises
cin
parents:
diff changeset
3
fb70574741a1 working on promises
cin
parents:
diff changeset
4 namespace Implab {
fb70574741a1 working on promises
cin
parents:
diff changeset
5 class PromiseFuncReaction<TRet> : PromiseReaction {
fb70574741a1 working on promises
cin
parents:
diff changeset
6 readonly Action m_fulfilled;
fb70574741a1 working on promises
cin
parents:
diff changeset
7
fb70574741a1 working on promises
cin
parents:
diff changeset
8 readonly Action<Exception> m_rejected;
fb70574741a1 working on promises
cin
parents:
diff changeset
9
fb70574741a1 working on promises
cin
parents:
diff changeset
10 readonly Deferred<TRet> m_next;
fb70574741a1 working on promises
cin
parents:
diff changeset
11
fb70574741a1 working on promises
cin
parents:
diff changeset
12 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
13 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
14 m_fulfilled = () => { next.Resolve(fulfilled()); };
fb70574741a1 working on promises
cin
parents:
diff changeset
15
fb70574741a1 working on promises
cin
parents:
diff changeset
16 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
17 m_rejected = (e) => { next.Resolve(rejected(e)); };
fb70574741a1 working on promises
cin
parents:
diff changeset
18 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
19 }
fb70574741a1 working on promises
cin
parents:
diff changeset
20
fb70574741a1 working on promises
cin
parents:
diff changeset
21 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
22 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
23 m_fulfilled = () => { next.Resolve(fulfilled()); };
fb70574741a1 working on promises
cin
parents:
diff changeset
24 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
25 m_rejected = (e) => { next.Resolve(rejected(e)); };
fb70574741a1 working on promises
cin
parents:
diff changeset
26 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
27 }
fb70574741a1 working on promises
cin
parents:
diff changeset
28
fb70574741a1 working on promises
cin
parents:
diff changeset
29 public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
30 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
31 m_fulfilled = () => { next.Resolve(fulfilled()); };
fb70574741a1 working on promises
cin
parents:
diff changeset
32 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
33 m_rejected = (e) => { next.Resolve(rejected(e)); };
fb70574741a1 working on promises
cin
parents:
diff changeset
34
fb70574741a1 working on promises
cin
parents:
diff changeset
35 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
36 }
fb70574741a1 working on promises
cin
parents:
diff changeset
37
fb70574741a1 working on promises
cin
parents:
diff changeset
38 public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) {
fb70574741a1 working on promises
cin
parents:
diff changeset
39 if (fulfilled != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
40 m_fulfilled = () => { next.Resolve(fulfilled()); };
fb70574741a1 working on promises
cin
parents:
diff changeset
41 if (rejected != null)
fb70574741a1 working on promises
cin
parents:
diff changeset
42 m_rejected = (e) => { next.Resolve(rejected(e)); };
fb70574741a1 working on promises
cin
parents:
diff changeset
43
fb70574741a1 working on promises
cin
parents:
diff changeset
44 m_next = next;
fb70574741a1 working on promises
cin
parents:
diff changeset
45 }
fb70574741a1 working on promises
cin
parents:
diff changeset
46
fb70574741a1 working on promises
cin
parents:
diff changeset
47
fb70574741a1 working on promises
cin
parents:
diff changeset
48 protected override bool HasFulfilHandler => m_fulfilled != null;
fb70574741a1 working on promises
cin
parents:
diff changeset
49
fb70574741a1 working on promises
cin
parents:
diff changeset
50 protected override bool HasRejectHandler => m_rejected != null;
fb70574741a1 working on promises
cin
parents:
diff changeset
51
fb70574741a1 working on promises
cin
parents:
diff changeset
52 protected override void DefaultReject(Exception reason) {
fb70574741a1 working on promises
cin
parents:
diff changeset
53 m_next.Reject(reason);
fb70574741a1 working on promises
cin
parents:
diff changeset
54 }
fb70574741a1 working on promises
cin
parents:
diff changeset
55
fb70574741a1 working on promises
cin
parents:
diff changeset
56 protected override void DefaultResolve() {
fb70574741a1 working on promises
cin
parents:
diff changeset
57 throw new NotImplementedException();
fb70574741a1 working on promises
cin
parents:
diff changeset
58 }
fb70574741a1 working on promises
cin
parents:
diff changeset
59
fb70574741a1 working on promises
cin
parents:
diff changeset
60 protected override void RejectImpl(Exception reason) {
fb70574741a1 working on promises
cin
parents:
diff changeset
61 try {
fb70574741a1 working on promises
cin
parents:
diff changeset
62 m_rejected(reason);
fb70574741a1 working on promises
cin
parents:
diff changeset
63 } catch (Exception e){
fb70574741a1 working on promises
cin
parents:
diff changeset
64 m_next.Reject(e);
fb70574741a1 working on promises
cin
parents:
diff changeset
65 }
fb70574741a1 working on promises
cin
parents:
diff changeset
66 }
fb70574741a1 working on promises
cin
parents:
diff changeset
67
fb70574741a1 working on promises
cin
parents:
diff changeset
68 protected override void ResolveImpl() {
fb70574741a1 working on promises
cin
parents:
diff changeset
69 try {
fb70574741a1 working on promises
cin
parents:
diff changeset
70 m_fulfilled();
fb70574741a1 working on promises
cin
parents:
diff changeset
71 } catch (Exception e){
fb70574741a1 working on promises
cin
parents:
diff changeset
72 m_next.Reject(e);
fb70574741a1 working on promises
cin
parents:
diff changeset
73 }
fb70574741a1 working on promises
cin
parents:
diff changeset
74 }
fb70574741a1 working on promises
cin
parents:
diff changeset
75 }
fb70574741a1 working on promises
cin
parents:
diff changeset
76 }