annotate Implab/PromiseT.cs @ 123:f4d6ea6969cc v2

async queue improvements
author cin
date Tue, 13 Jan 2015 01:42:38 +0300
parents 2573b562e328
children 671f60cd0250
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
1 using System;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
2 using System.Diagnostics;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
3
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
4 namespace Implab {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
5
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
6 /// <summary>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
7 /// Класс для асинхронного получения результатов. Так называемое "обещание".
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
8 /// </summary>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
9 /// <typeparam name="T">Тип получаемого результата</typeparam>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
10 /// <remarks>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
11 /// <para>Сервис при обращении к его методу дает обещаиние о выполнении операции,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
12 /// клиент получив такое обещание может установить ряд обратных вызово для получения
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
13 /// событий выполнения обещания, тоесть завершения операции и предоставлении результатов.</para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
14 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
15 /// Обещение может быть как выполнено, так и выполнено с ошибкой. Для подписки на
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
16 /// данные события клиент должен использовать методы <c>Then</c>.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
17 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
18 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
19 /// Сервис, в свою очередь, по окончанию выполнения операции (возможно с ошибкой),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
20 /// использует методы <c>Resolve</c> либо <c>Reject</c> для оповещения клиетна о
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
21 /// выполнении обещания.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
22 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
23 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
24 /// Если сервер успел выполнить обещание еще до того, как клиент на него подписался,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
25 /// то в момент подписки клиента будут вызваны соответсвующие события в синхронном
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
26 /// режиме и клиент будет оповещен в любом случае. Иначе, обработчики добавляются в
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
27 /// список в порядке подписания и в этом же порядке они будут вызваны при выполнении
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
28 /// обещания.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
29 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
30 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
31 /// Обрабатывая результаты обещания можно преобразовывать результаты либо инициировать
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
32 /// связанные асинхронные операции, которые также возвращают обещания. Для этого следует
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
33 /// использовать соответствующую форму методе <c>Then</c>.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
34 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
35 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
36 /// Также хорошим правилом является то, что <c>Resolve</c> и <c>Reject</c> должен вызывать
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
37 /// только инициатор обещания иначе могут возникнуть противоречия.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
38 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
39 /// </remarks>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
40 public class Promise<T> : AbstractPromise<IDeferred<T>>, IPromise<T>, IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
41
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
42 class StubDeferred : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
43 public static readonly StubDeferred instance = new StubDeferred();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
44
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
45 StubDeferred() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
46 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
47
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
48 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
49
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
50 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
51 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
52
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
53 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
54 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
55
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
56 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
57
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
58 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
59
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
60 public void Cancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
61 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
62
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
63 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
64
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
65
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
66 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
67
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
68 class RemapDescriptor<T2> : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
69 readonly Func<T,T2> m_remap;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
70 readonly Func<Exception,T2> m_failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
71 readonly Func<T2> m_cancel;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
72 readonly IDeferred<T2> m_deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
73
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
74 public RemapDescriptor(Func<T,T2> remap, Func<Exception,T2> failed, Func<T2> cancel, IDeferred<T2> deferred ) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
75 Debug.Assert(deferred != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
76 m_remap = remap;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
77 m_failed = failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
78 m_cancel = cancel;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
79 m_deferred = deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
80 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
81
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
82
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
83
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
84 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
85
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
86 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
87 if (m_remap != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
88 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
89 m_deferred.Resolve(m_remap(value));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
90 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
91 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
92 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
93 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
94 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
95
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
96 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
97 if (m_failed != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
98 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
99 m_deferred.Resolve(m_failed(error));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
100 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
101 m_deferred.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
102 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
103 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
104 m_deferred.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
105 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
106 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
107
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
108
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
109 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
110
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
111 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
112
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
113 public void Cancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
114 if (m_cancel != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
115 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
116 m_deferred.Resolve(m_cancel());
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
117 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
118 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
119 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
120 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
121 m_deferred.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
122 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
123 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
124
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
125 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
126 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
127
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
128 class ListenerDescriptor : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
129 readonly Action m_handler;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
130 readonly PromiseEventType m_events;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
131
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
132 public ListenerDescriptor(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
133 Debug.Assert(handler != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
134
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
135 m_handler = handler;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
136 m_events = events;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
137 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
138
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
139 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
140
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
141 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
142 if (m_events.HasFlag(PromiseEventType.Success)) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
143 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
144 m_handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
145 // Analysis disable once EmptyGeneralCatchClause
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
146 } catch {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
147 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
148 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
149 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
150
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
151 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
152 if (m_events.HasFlag(PromiseEventType.Error)){
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
153 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
154 m_handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
155 // Analysis disable once EmptyGeneralCatchClause
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
156 } catch {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
157 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
158 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
159 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
160
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
161 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
162
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
163 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
164
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
165 public void Cancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
166 if (m_events.HasFlag(PromiseEventType.Cancelled)){
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
167 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
168 m_handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
169 // Analysis disable once EmptyGeneralCatchClause
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
170 } catch {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
171 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
172 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
173 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
174
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
175 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
176 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
177
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
178 class ValueEventDescriptor : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
179 readonly Action<T> m_success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
180 readonly Action<Exception> m_failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
181 readonly Action m_cancelled;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
182 readonly IDeferred<T> m_deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
183
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
184 public ValueEventDescriptor(Action<T> success, Action<Exception> failed, Action cancelled, IDeferred<T> deferred) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
185 Debug.Assert(deferred != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
186
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
187 m_success = success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
188 m_failed = failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
189 m_cancelled = cancelled;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
190 m_deferred = deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
191 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
192
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
193 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
194
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
195 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
196 if (m_success != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
197 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
198 m_success(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
199 m_deferred.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
200 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
201 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
202 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
203 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
204 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
205
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
206 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
207 if (m_failed != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
208 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
209 m_failed(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
210 m_deferred.Resolve(default(T));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
211 } catch(Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
212 m_deferred.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
213 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
214 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
215 m_deferred.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
216 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
217 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
218
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
219 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
220
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
221 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
222
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
223 public void Cancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
224 if (m_cancelled != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
225 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
226 m_cancelled();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
227 m_deferred.Resolve(default(T));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
228 } catch(Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
229 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
230 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
231 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
232 m_deferred.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
233 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
234 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
235
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
236 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
237 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
238
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
239 public class EventDescriptor : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
240 readonly Action m_success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
241 readonly Action<Exception> m_failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
242 readonly Action m_cancelled;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
243 readonly IDeferred<T> m_deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
244
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
245 public EventDescriptor(Action success, Action<Exception> failed, Action cancelled, IDeferred<T> deferred) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
246 Debug.Assert(deferred != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
247
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
248 m_success = success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
249 m_failed = failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
250 m_cancelled = cancelled;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
251 m_deferred = deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
252 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
253
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
254 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
255
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
256 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
257 if (m_success != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
258 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
259 m_success();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
260 m_deferred.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
261 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
262 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
263 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
264 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
265 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
266
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
267 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
268 if (m_failed != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
269 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
270 m_failed(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
271 m_deferred.Resolve(default(T));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
272 }catch (Exception ex)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
273 {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
274 m_deferred.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
275 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
276 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
277 m_deferred.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
278 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
279
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
280 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
281
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
282 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
283
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
284 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
285
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
286 public void Cancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
287 if (m_cancelled != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
288 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
289 m_cancelled();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
290 m_deferred.Resolve(default(T));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
291 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
292 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
293 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
294 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
295 m_deferred.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
296 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
297 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
298
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
299 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
300 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
301
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
302 T m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
303
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
304 public virtual void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
305 BeginSetResult();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
306 m_result = value;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
307 EndSetResult();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
308 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
309
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
310 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
311 SetError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
312 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
313
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
314 public Type PromiseType {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
315 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
316 return typeof(T);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
317 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
318 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
319
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
320 public new T Join() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
321 WaitResult(-1);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
322 return m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
323 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
324 public new T Join(int timeout) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
325 WaitResult(timeout);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
326 return m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
327 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
328
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
329 public IPromise<T> On(Action<T> success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
330 AddHandler(new ValueEventDescriptor(success, error, cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
331 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
332 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
333
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
334 public IPromise<T> On(Action<T> success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
335 AddHandler(new ValueEventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
336 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
337 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
338
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
339 public IPromise<T> On(Action<T> success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
340 AddHandler(new ValueEventDescriptor(success, null, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
341 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
342 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
343
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
344 public IPromise<T> On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
345 Listen(events, handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
346 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
347 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
348
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
349 public IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception, T2> error, Func<T2> cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
350 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
351 AddHandler(new RemapDescriptor<T2>(mapper, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
352 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
353 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
354
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
355 public IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception, T2> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
356 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
357 AddHandler(new RemapDescriptor<T2>(mapper, error, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
358 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
359 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
360
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
361 public IPromise<T2> Then<T2>(Func<T, T2> mapper) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
362 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
363 AddHandler(new RemapDescriptor<T2>(mapper, null, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
364 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
365 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
366
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
367 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception, IPromise<T2>> error, Func<IPromise<T2>> cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
368 // this promise will be resolved when an asyc operation is started
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
369 var promise = new Promise<IPromise<T2>>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
370
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
371 AddHandler(new RemapDescriptor<IPromise<T2>>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
372 chained,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
373 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
374 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
375 promise
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
376 ));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
377
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
378 var medium = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
379
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
380 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
381 medium.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
382
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
383 // we need to connect started async operation with the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
384 // if the async operation hasn't been started by the some reason
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
385 // report is to the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
386 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
387 result => ConnectPromise<T2>(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
388 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
389 medium.Cancel
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
390 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
391
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
392 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
393 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
394
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
395 static void ConnectPromise<T2>(IPromise<T2> result, Promise<T2> medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
396 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
397 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
398 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
399 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
400 () => medium.Reject(new OperationCanceledException())
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
401 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
402 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
403 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
404 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
405 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
406 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
407 " 'null' where the promise instance is expected"
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
408 )
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
409 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
410 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
411 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
412
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
413 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception, IPromise<T2>> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
414 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
415 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
416
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
417 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
418 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
419 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
420
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
421 public IPromise<T2> Error<T2>(Func<Exception, T2> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
422 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
423 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
424 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
425 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
426 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
427 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
428 promise.Resolve(error(ex));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
429 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
430 promise.Reject(ex2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
431 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
432 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
433 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
434 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
435 Listen(PromiseEventType.Error, () => promise.Resolve(default(T2)));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
436 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
437 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
438
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
439 public IPromise<T2> Cancelled<T2>(Func<T2> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
440 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
441 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
442 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
443 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
444 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
445 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
446 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
447 promise.Resolve(handler());
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
448 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
449 promise.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
450 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
451 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
452 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
453 Listen(PromiseEventType.Cancelled, () => promise.Resolve(default(T2)));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
454 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
455 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
456
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
457 public IPromise Then(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
458 var promise = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
459 if (success != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
460 promise.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
461
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
462 AddHandler(new EventDescriptor(success, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
463
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
464 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
465 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
466
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
467 public IPromise Then(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
468 return Then(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
469 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
470
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
471 public IPromise Then(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
472 return Then(success, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
473 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
474
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
475 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error, Func<IPromise> cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
476 var promise = new Promise<IPromise>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
477
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
478 AddHandler(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
479 new RemapDescriptor<IPromise>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
480 x => chained(),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
481 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
482 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
483 promise
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
484 )
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
485 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
486
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
487 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
488 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
489 medium.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
490
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
491 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
492 result => ConnectPromise(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
493 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
494 medium.Cancel
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
495 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
496
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
497 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
498 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
499
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
500 static void ConnectPromise(IPromise result, Promise medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
501 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
502 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
503 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
504 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
505 () => medium.Reject(new OperationCanceledException())
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
506 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
507 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
508 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
509 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
510 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
511 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
512 " 'null' where the promise instance is expected"
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
513 )
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
514 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
515 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
516 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
517
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
518 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
519 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
520 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
521
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
522 public IPromise Chain(Func<IPromise> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
523 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
524 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
525
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
526 public IPromise On(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
527 AddHandler(new EventDescriptor(success,error,cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
528 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
529 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
530
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
531 public IPromise On(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
532 AddHandler(new EventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
533 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
534 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
535
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
536 public IPromise On(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
537 Listen(PromiseEventType.Success, success);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
538 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
539 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
540
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
541 IPromise IPromise.On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
542 Listen(events,handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
543 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
544 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
545
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
546 public IPromise Error(Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
547 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
548 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
549 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
550 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
551 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
552 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
553 error(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
554 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
555 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
556 promise.Reject(ex2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
557 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
558 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
559 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
560 Listen(PromiseEventType.Error, promise.Resolve);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
561 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
562 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
563
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
564 public IPromise Cancelled(Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
565 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
566 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
567 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
568 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
569 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
570 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
571 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
572 handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
573 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
574 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
575 promise.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
576 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
577 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
578 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
579 Listen(PromiseEventType.Cancelled, promise.Resolve);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
580 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
581 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
582
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
583 public IPromise<T2> Cast<T2>() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
584 return (IPromise<T2>)this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
585 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
586
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
587 #region implemented abstract members of AbstractPromise
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
588
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
589 protected override void SignalSuccess(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
590 handler.Resolve(m_result);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
591 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
592
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
593 protected override void SignalError(IDeferred<T> handler, Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
594 handler.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
595 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
596
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
597 protected override void SignalCancelled(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
598 handler.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
599 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
600
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
601 protected override void Listen(PromiseEventType events, Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
602 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
603 AddHandler(new ListenerDescriptor(handler, events));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
604 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
605
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
606 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
607
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
608 public static IPromise<T> ResultToPromise(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
609 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
610 p.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
611 return p;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
612 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
613
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
614 public static IPromise<T> ExceptionToPromise(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
615 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
616 p.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
617 return p;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
618 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
619
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
620 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
621 }