annotate Implab/PromiseT.cs @ 136:e9e7940c7d98 v2

shared locks + tests
author cin
date Mon, 16 Feb 2015 01:14:09 +0300
parents 656815cb7147
children f75cfa58e3d4
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) {
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
305 if (BeginSetResult()) {
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
306 m_result = value;
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
307 EndSetResult();
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
308 }
119
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
311 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
312 SetError(error);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
315 public Type PromiseType {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
316 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
317 return typeof(T);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
321 public new T Join() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
322 WaitResult(-1);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
323 return m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
324 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
325 public new T Join(int timeout) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
326 WaitResult(timeout);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
327 return m_result;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
330 public IPromise<T> On(Action<T> success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
331 AddHandler(new ValueEventDescriptor(success, error, cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
332 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
335 public IPromise<T> On(Action<T> success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
336 AddHandler(new ValueEventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
337 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
340 public IPromise<T> On(Action<T> success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
341 AddHandler(new ValueEventDescriptor(success, null, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
342 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
345 public IPromise<T> On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
346 Listen(events, handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
347 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
350 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
351 var promise = new Promise<T2>();
135
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
352 if (mapper != null)
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
353 promise.On(Cancel, PromiseEventType.Cancelled);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
354 AddHandler(new RemapDescriptor<T2>(mapper, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
355 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
356 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
357
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
358 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
359 var promise = new Promise<T2>();
135
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
360 if (mapper != null)
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
361 promise.On(Cancel, PromiseEventType.Cancelled);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
362 AddHandler(new RemapDescriptor<T2>(mapper, error, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
363 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
364 }
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 public IPromise<T2> Then<T2>(Func<T, T2> mapper) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
367 var promise = new Promise<T2>();
135
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
368 if (mapper != null)
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
369 promise.On(Cancel, PromiseEventType.Cancelled);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
370 AddHandler(new RemapDescriptor<T2>(mapper, null, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
371 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
372 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
373
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
374 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
375 // this promise will be resolved when an asyc operation is started
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
376 var promise = new Promise<IPromise<T2>>();
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 AddHandler(new RemapDescriptor<IPromise<T2>>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
379 chained,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
380 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
381 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
382 promise
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
383 ));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
384
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
385 var medium = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
386
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
387 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
388 medium.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
389
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
390 // we need to connect started async operation with the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
391 // if the async operation hasn't been started by the some reason
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
392 // report is to the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
393 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
394 result => ConnectPromise<T2>(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
395 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
396 medium.Cancel
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
397 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
398
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
399 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
400 }
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 static void ConnectPromise<T2>(IPromise<T2> result, Promise<T2> medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
403 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
404 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
405 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
406 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
407 () => medium.Reject(new OperationCanceledException())
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 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
410 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
411 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
412 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
413 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
414 " 'null' where the promise instance is expected"
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
418 }
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 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
421 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
422 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
423
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
424 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
425 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
426 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
427
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
428 public IPromise<T2> Error<T2>(Func<Exception, T2> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
429 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
430 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
431 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
432 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
433 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
434 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
435 promise.Resolve(error(ex));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
436 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
437 promise.Reject(ex2);
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
440 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
441 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
442 Listen(PromiseEventType.Error, () => promise.Resolve(default(T2)));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
443 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
444 }
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 public IPromise<T2> Cancelled<T2>(Func<T2> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
447 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
448 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
449 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
450 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
451 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
452 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
453 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
454 promise.Resolve(handler());
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
455 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
456 promise.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
457 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
458 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
459 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
460 Listen(PromiseEventType.Cancelled, () => promise.Resolve(default(T2)));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
461 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
462 }
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 public IPromise Then(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
465 var promise = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
466 if (success != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
467 promise.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
468
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
469 AddHandler(new EventDescriptor(success, error, cancel, promise));
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 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
472 }
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 public IPromise Then(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
475 return Then(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
476 }
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 public IPromise Then(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
479 return Then(success, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
480 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
481
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
482 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
483 var promise = new Promise<IPromise>();
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 AddHandler(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
486 new RemapDescriptor<IPromise>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
487 x => chained(),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
488 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
489 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
490 promise
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
491 )
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
492 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
493
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
494 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
495 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
496 medium.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
497
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
498 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
499 result => ConnectPromise(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
500 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
501 medium.Cancel
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
502 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
503
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
504 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
505 }
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 static void ConnectPromise(IPromise result, Promise medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
508 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
509 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
510 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
511 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
512 () => medium.Reject(new OperationCanceledException())
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 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
515 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
516 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
517 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
518 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
519 " 'null' where the promise instance is expected"
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
523 }
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 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
526 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
527 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
528
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
529 public IPromise Chain(Func<IPromise> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
530 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
531 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
532
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
533 public IPromise On(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
534 AddHandler(new EventDescriptor(success,error,cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
535 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
536 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
537
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
538 public IPromise On(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
539 AddHandler(new EventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
540 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
541 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
542
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
543 public IPromise On(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
544 Listen(PromiseEventType.Success, success);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
545 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
546 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
547
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
548 IPromise IPromise.On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
549 Listen(events,handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
550 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
551 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
552
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
553 public IPromise Error(Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
554 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
555 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
556 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
557 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
558 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
559 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
560 error(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
561 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
562 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
563 promise.Reject(ex2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
564 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
565 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
566 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
567 Listen(PromiseEventType.Error, promise.Resolve);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
568 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
569 }
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 public IPromise Cancelled(Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
572 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
573 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
574 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
575 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
576 null,
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 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
579 handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
580 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
581 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
582 promise.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
583 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
584 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
585 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
586 Listen(PromiseEventType.Cancelled, promise.Resolve);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
587 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
590 public IPromise<T2> Cast<T2>() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
591 return (IPromise<T2>)this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
594 #region implemented abstract members of AbstractPromise
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 protected override void SignalSuccess(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
597 handler.Resolve(m_result);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
598 }
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 protected override void SignalError(IDeferred<T> handler, Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
601 handler.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
602 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
603
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
604 protected override void SignalCancelled(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
605 handler.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
606 }
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 protected override void Listen(PromiseEventType events, Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
609 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
610 AddHandler(new ListenerDescriptor(handler, events));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
611 }
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 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
614
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
615 public static IPromise<T> ResultToPromise(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
616 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
617 p.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
618 return p;
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 public static IPromise<T> ExceptionToPromise(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
622 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
623 p.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
624 return p;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
625 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
626
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
627 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
628 }