annotate Implab/PromiseT.cs @ 130:671f60cd0250 v2

fixed Resove method bug when calling it on already cancelled promise
author cin
date Fri, 30 Jan 2015 17:07:17 +0300
parents 2573b562e328
children 656815cb7147
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>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
352 AddHandler(new RemapDescriptor<T2>(mapper, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
353 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
356 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
357 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
358 AddHandler(new RemapDescriptor<T2>(mapper, error, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
359 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
362 public IPromise<T2> Then<T2>(Func<T, T2> mapper) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
363 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
364 AddHandler(new RemapDescriptor<T2>(mapper, null, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
365 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
368 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
369 // this promise will be resolved when an asyc operation is started
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
370 var promise = new Promise<IPromise<T2>>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
371
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
372 AddHandler(new RemapDescriptor<IPromise<T2>>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
373 chained,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
374 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
375 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
376 promise
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
379 var medium = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
380
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
381 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
382 medium.On(Cancel, PromiseEventType.Cancelled);
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 // we need to connect started async operation with the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
385 // if the async operation hasn't been started by the some reason
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
386 // report is to the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
387 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
388 result => ConnectPromise<T2>(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
389 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
390 medium.Cancel
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
393 return medium;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
396 static void ConnectPromise<T2>(IPromise<T2> result, Promise<T2> medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
397 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
398 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
399 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
400 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
401 () => medium.Reject(new OperationCanceledException())
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
402 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
403 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
404 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
405 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
406 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
407 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
408 " 'null' where the promise instance is expected"
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
414 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
415 return Chain(chained, error, null);
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 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
419 return Chain(chained, null, null);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
422 public IPromise<T2> Error<T2>(Func<Exception, T2> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
423 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
424 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
425 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
426 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
427 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
428 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
429 promise.Resolve(error(ex));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
430 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
431 promise.Reject(ex2);
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 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
435 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
436 Listen(PromiseEventType.Error, () => promise.Resolve(default(T2)));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
437 return promise;
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 public IPromise<T2> Cancelled<T2>(Func<T2> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
441 var promise = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
442 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
443 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
444 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
445 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
446 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
447 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
448 promise.Resolve(handler());
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
449 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
450 promise.Reject(ex);
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 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
453 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
454 Listen(PromiseEventType.Cancelled, () => promise.Resolve(default(T2)));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
455 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
458 public IPromise Then(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
459 var promise = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
460 if (success != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
461 promise.On(Cancel, PromiseEventType.Cancelled);
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 AddHandler(new EventDescriptor(success, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
464
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
465 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
468 public IPromise Then(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
469 return Then(success, error, null);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
472 public IPromise Then(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
473 return Then(success, null, null);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
476 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
477 var promise = new Promise<IPromise>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
478
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
479 AddHandler(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
480 new RemapDescriptor<IPromise>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
481 x => chained(),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
482 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
483 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
484 promise
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
488 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
489 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
490 medium.On(Cancel, PromiseEventType.Cancelled);
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 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
493 result => ConnectPromise(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
494 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
495 medium.Cancel
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
498 return medium;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
501 static void ConnectPromise(IPromise result, Promise medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
502 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
503 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
504 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
505 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
506 () => medium.Reject(new OperationCanceledException())
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
507 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
508 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
509 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
510 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
511 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
512 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
513 " 'null' where the promise instance is expected"
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
519 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
520 return Chain(chained, error, null);
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 public IPromise Chain(Func<IPromise> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
524 return Chain(chained, null, null);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
527 public IPromise On(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
528 AddHandler(new EventDescriptor(success,error,cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
529 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
532 public IPromise On(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
533 AddHandler(new EventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
534 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
537 public IPromise On(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
538 Listen(PromiseEventType.Success, success);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
539 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
542 IPromise IPromise.On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
543 Listen(events,handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
544 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
547 public IPromise Error(Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
548 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
549 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
550 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
551 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
552 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
553 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
554 error(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
555 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
556 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
557 promise.Reject(ex2);
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 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
560 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
561 Listen(PromiseEventType.Error, promise.Resolve);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
562 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
565 public IPromise Cancelled(Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
566 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
567 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
568 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
569 (Action<T>)null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
570 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
571 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
572 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
573 handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
574 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
575 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
576 promise.Reject(ex);
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 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
579 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
580 Listen(PromiseEventType.Cancelled, promise.Resolve);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
581 return promise;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
584 public IPromise<T2> Cast<T2>() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
585 return (IPromise<T2>)this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
588 #region implemented abstract members of AbstractPromise
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 protected override void SignalSuccess(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
591 handler.Resolve(m_result);
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 protected override void SignalError(IDeferred<T> handler, Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
595 handler.Reject(error);
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
598 protected override void SignalCancelled(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
599 handler.Cancel();
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
602 protected override void Listen(PromiseEventType events, Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
603 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
604 AddHandler(new ListenerDescriptor(handler, events));
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
607 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
608
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
609 public static IPromise<T> ResultToPromise(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
610 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
611 p.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
612 return p;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
615 public static IPromise<T> ExceptionToPromise(Exception error) {
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.Reject(error);
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
622 }