Mercurial > pub > ImplabNet
annotate Implab/AbstractPromiseT.cs @ 222:98eeb63cedb2
sync
author | cin |
---|---|
date | Tue, 22 Aug 2017 09:34:47 +0300 |
parents | 86187b01c4e0 |
children | cbe10ac0731e |
rev | line source |
---|---|
144 | 1 using System; |
2 using Implab.Parallels; | |
3 | |
4 namespace Implab { | |
5 public abstract class AbstractPromise<T> : AbstractEvent<AbstractPromise<T>.HandlerDescriptor>, IPromise<T> { | |
6 public struct HandlerDescriptor { | |
7 readonly Action m_handler; | |
8 readonly Action<T> m_success; | |
9 readonly Action<Exception> m_error; | |
10 readonly Action<Exception> m_cancel; | |
11 readonly PromiseEventType m_mask; | |
12 | |
13 public HandlerDescriptor(Action<T> success, Action<Exception> error, Action<Exception> cancel) { | |
14 m_success = success; | |
15 m_error = error; | |
16 m_cancel = cancel; | |
145 | 17 |
18 m_handler = null; | |
19 m_mask = 0; | |
144 | 20 } |
21 | |
22 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) { | |
23 m_handler = success; | |
145 | 24 m_success = null; |
144 | 25 m_error = error; |
26 m_cancel = cancel; | |
27 m_mask = PromiseEventType.Success; | |
28 } | |
29 | |
30 public HandlerDescriptor(Action handler, PromiseEventType mask) { | |
31 m_handler = handler; | |
32 m_mask = mask; | |
145 | 33 m_success = null; |
34 m_error = null; | |
35 m_cancel = null; | |
144 | 36 } |
37 | |
38 public void SignalSuccess(T result) { | |
39 if (m_success != null) { | |
40 try { | |
41 m_success(result); | |
197
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
156
diff
changeset
|
42 // Analysis disable once EmptyGeneralCatchClause |
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
156
diff
changeset
|
43 } catch { |
144 | 44 } |
145 | 45 } else if ((m_mask & PromiseEventType.Success) != 0 && m_handler != null) { |
144 | 46 try { |
47 m_handler(); | |
197
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
156
diff
changeset
|
48 // Analysis disable once EmptyGeneralCatchClause |
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
156
diff
changeset
|
49 } catch { |
144 | 50 } |
51 } | |
52 } | |
53 | |
54 public void SignalError(Exception err) { | |
55 if (m_error != null) { | |
56 try { | |
57 m_error(err); | |
58 // Analysis disable once EmptyGeneralCatchClause | |
59 } catch { | |
60 } | |
145 | 61 } else if ((m_mask & PromiseEventType.Error) != 0 && m_handler != null) { |
144 | 62 try { |
63 m_handler(); | |
64 // Analysis disable once EmptyGeneralCatchClause | |
65 } catch { | |
66 } | |
67 } | |
68 } | |
69 | |
70 public void SignalCancel(Exception reason) { | |
71 if (m_cancel != null) { | |
72 try { | |
73 m_cancel(reason); | |
197
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
156
diff
changeset
|
74 // Analysis disable once EmptyGeneralCatchClause |
86187b01c4e0
fixed: the error handler should not handle handlers errors
cin
parents:
156
diff
changeset
|
75 } catch { |
144 | 76 } |
145 | 77 } else if ((m_mask & PromiseEventType.Cancelled) != 0 && m_handler != null) { |
144 | 78 try { |
79 m_handler(); | |
80 // Analysis disable once EmptyGeneralCatchClause | |
81 } catch { | |
82 } | |
83 } | |
84 } | |
85 } | |
86 | |
87 public Type PromiseType { | |
88 get { | |
89 return typeof(T); | |
90 } | |
91 } | |
92 | |
145 | 93 public T Join() { |
144 | 94 WaitResult(-1); |
95 return m_result; | |
96 } | |
145 | 97 public T Join(int timeout) { |
144 | 98 WaitResult(timeout); |
99 return m_result; | |
100 } | |
101 | |
145 | 102 void IPromise.Join() { |
103 WaitResult(-1); | |
104 } | |
105 void IPromise.Join(int timeout) { | |
106 WaitResult(timeout); | |
107 } | |
108 | |
144 | 109 public IPromise<T> On(Action<T> success, Action<Exception> error, Action<Exception> cancel) { |
110 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
111 return this; | |
112 } | |
113 | |
114 public IPromise<T> On(Action<T> success, Action<Exception> error) { | |
115 AddHandler(new HandlerDescriptor(success, error, null)); | |
116 return this; | |
117 } | |
118 | |
119 public IPromise<T> On(Action<T> success) { | |
120 AddHandler(new HandlerDescriptor(success, null, null)); | |
121 return this; | |
122 } | |
123 | |
124 public IPromise<T> On(Action handler, PromiseEventType events) { | |
125 AddHandler(new HandlerDescriptor(handler, events)); | |
126 return this; | |
127 } | |
128 | |
129 public IPromise<T> On(Action success, Action<Exception> error, Action<Exception> cancel) { | |
130 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
131 return this; | |
132 } | |
133 | |
134 public IPromise<T> On(Action success, Action<Exception> error) { | |
135 AddHandler(new HandlerDescriptor(success, error, null)); | |
136 return this; | |
137 } | |
138 | |
139 public IPromise<T> On(Action success) { | |
140 AddHandler(new HandlerDescriptor(success, null, null)); | |
141 return this; | |
142 } | |
143 | |
144 IPromise IPromise.On(Action success, Action<Exception> error, Action<Exception> cancel) { | |
145 AddHandler(new HandlerDescriptor(success, error, cancel)); | |
146 return this; | |
147 } | |
148 | |
149 IPromise IPromise.On(Action success, Action<Exception> error) { | |
150 AddHandler(new HandlerDescriptor(success, error, null)); | |
151 return this; | |
152 } | |
153 | |
154 IPromise IPromise.On(Action success) { | |
155 AddHandler(new HandlerDescriptor(success, null, null)); | |
156 return this; | |
157 } | |
158 | |
145 | 159 IPromise IPromise.On(Action handler, PromiseEventType events) { |
160 AddHandler(new HandlerDescriptor(handler, events)); | |
161 return this; | |
162 } | |
163 | |
144 | 164 public IPromise<T2> Cast<T2>() { |
165 return (IPromise<T2>)this; | |
166 } | |
167 | |
168 #region implemented abstract members of AbstractPromise | |
169 | |
170 protected override Signal GetResolveSignal() { | |
171 var signal = new Signal(); | |
172 AddHandler(new HandlerDescriptor(signal.Set, PromiseEventType.All)); | |
173 return signal; | |
174 } | |
175 | |
156
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
176 protected override void SignalHandler(HandlerDescriptor handler, int signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
177 switch (signal) { |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
178 case SUCCEEDED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
179 handler.SignalSuccess(m_result); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
180 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
181 case REJECTED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
182 handler.SignalError(Error); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
183 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
184 case CANCELLED_STATE: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
185 handler.SignalCancel(CancellationReason); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
186 break; |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
187 default: |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
188 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal)); |
97fbbf816844
Promises: SignalXXX methods merged into SignalHandler method.
cin
parents:
145
diff
changeset
|
189 } |
144 | 190 } |
191 | |
192 #endregion | |
193 | |
194 T m_result; | |
195 | |
196 protected void SetResult(T value) { | |
197 if (BeginSetResult()) { | |
198 m_result = value; | |
199 EndSetResult(); | |
200 } | |
201 } | |
202 } | |
203 } | |
204 |