annotate Implab/PromiseT.cs @ 141:0fa293bb1351 v2

fixed JSON writer
author cin
date Tue, 24 Feb 2015 00:54:22 +0300
parents f75cfa58e3d4
children 8c0b95069066
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
42 class StubDeferred : IDeferred, IDeferred<T> {
119
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
53 public void Resolve() {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
54 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
55
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
56 public void Reject(Exception error) {
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
59 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
60
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
61 #region ICancellable implementation
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 public void Cancel() {
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
66 public void Cancel(Exception reason) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
67 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
68
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
69 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
70
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
71
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
72 }
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 class RemapDescriptor<T2> : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
75 readonly Func<T,T2> m_remap;
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
76 readonly Func<Exception, T2> m_failed;
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
77 readonly Func<Exception, T2> m_cancel;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
78 readonly IDeferred<T2> m_deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
79
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
80 public RemapDescriptor(Func<T,T2> remap, Func<Exception,T2> failed, Func<Exception, T2> cancel, IDeferred<T2> deferred ) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
81 Debug.Assert(deferred != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
82 m_remap = remap;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
83 m_failed = failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
84 m_cancel = cancel;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
85 m_deferred = deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
86 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
87
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
88
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
89
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
90 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
91
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
92 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
93 if (m_remap != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
94 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
95 m_deferred.Resolve(m_remap(value));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
96 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
97 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
98 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
99 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
100 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
101
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
102 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
103 if (m_failed != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
104 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
105 m_deferred.Resolve(m_failed(error));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
106 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
107 m_deferred.Reject(ex);
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 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
110 m_deferred.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
111 }
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
114
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
115 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
116
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
117 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
118
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
119 public void Cancel(Exception reason) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
120 if (m_cancel != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
121 try {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
122 m_deferred.Resolve(m_cancel(reason));
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
123 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
124 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
125 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
126 } else {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
127 m_deferred.Cancel(reason);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
128 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
129 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
130
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
131 public void Cancel() {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
132 Cancel(null);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
133 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
134 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
135 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
136
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
137 class ListenerDescriptor : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
138 readonly Action m_handler;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
139 readonly PromiseEventType m_events;
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 ListenerDescriptor(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
142 Debug.Assert(handler != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
143
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
144 m_handler = handler;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
145 m_events = events;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
146 }
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 #region IDeferred implementation
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 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
151 if (m_events.HasFlag(PromiseEventType.Success)) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
152 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
153 m_handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
154 // Analysis disable once EmptyGeneralCatchClause
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
155 } catch {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
156 }
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 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
161 if (m_events.HasFlag(PromiseEventType.Error)){
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
162 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
163 m_handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
164 // Analysis disable once EmptyGeneralCatchClause
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
165 } catch {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
166 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
167 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
168 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
169
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
170 #endregion
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 #region ICancellable implementation
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 public void Cancel() {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
175 Cancel(null);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
176 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
177
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
178 public void Cancel(Exception reason) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
179 if (m_events.HasFlag(PromiseEventType.Cancelled)){
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
180 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
181 m_handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
182 // Analysis disable once EmptyGeneralCatchClause
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
183 } catch {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
184 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
185 }
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
188 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
189 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
190
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
191 class ValueEventDescriptor : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
192 readonly Action<T> m_success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
193 readonly Action<Exception> m_failed;
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
194 readonly Action<Exception> m_cancelled;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
195 readonly IDeferred<T> m_deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
196
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
197 public ValueEventDescriptor(Action<T> success, Action<Exception> failed, Action<Exception> cancelled, IDeferred<T> deferred) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
198 Debug.Assert(deferred != null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
199
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
200 m_success = success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
201 m_failed = failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
202 m_cancelled = cancelled;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
203 m_deferred = deferred;
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 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
207
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
208 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
209 if (m_success != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
210 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
211 m_success(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
212 m_deferred.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
213 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
214 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
215 }
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 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
220 if (m_failed != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
221 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
222 m_failed(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
223 m_deferred.Resolve(default(T));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
224 } catch(Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
225 m_deferred.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
226 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
227 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
228 m_deferred.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
229 }
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
232 #endregion
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 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
235
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
236 public void Cancel(Exception reason) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
237 if (m_cancelled != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
238 try {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
239 m_cancelled(reason);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
240 m_deferred.Resolve(default(T));
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
241 } catch (Exception ex) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
242 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
243 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
244 } else {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
245 m_deferred.Cancel(reason);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
246 }
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
249 public void Cancel() {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
250 Cancel(null);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
251 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
252
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
253 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
254 }
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 class EventDescriptor : IDeferred<T> {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
257 readonly Action m_success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
258 readonly Action<Exception> m_failed;
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
259 readonly Action<Exception> m_cancelled;
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
260 readonly IDeferred m_deferred;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
261
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
262 public EventDescriptor(Action success, Action<Exception> failed, Action<Exception> cancelled, IDeferred deferred) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
263 Debug.Assert(deferred != null);
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 m_success = success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
266 m_failed = failed;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
267 m_cancelled = cancelled;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
268 m_deferred = deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
269 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
270
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
271 #region IDeferred implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
272
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
273 public void Resolve(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
274 if (m_success != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
275 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
276 m_success();
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
277 m_deferred.Resolve();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
278 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
279 Reject(ex);
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 }
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 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
285 if (m_failed != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
286 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
287 m_failed(error);
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
288 m_deferred.Resolve();
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
289 } catch (Exception ex) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
290 m_deferred.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
291 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
292 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
293 m_deferred.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
294 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
295 }
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 #endregion
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 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
300
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
301 public void Cancel(Exception reason) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
302 if (m_cancelled != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
303 try {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
304 m_cancelled(reason);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
305 m_deferred.Resolve();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
306 } catch (Exception ex) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
307 Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
308 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
309 } else {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
310 m_deferred.Cancel(reason);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
311 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
312 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
313
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
314 public void Cancel() {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
315 Cancel(null);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
316 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
317
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
318 #endregion
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 T m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
322
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
323 public virtual void Resolve(T value) {
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
324 if (BeginSetResult()) {
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
325 m_result = value;
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
326 EndSetResult();
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 119
diff changeset
327 }
119
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 void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
331 SetError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
332 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
333
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
334 public Type PromiseType {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
335 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
336 return typeof(T);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
337 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
338 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
339
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
340 public new T Join() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
341 WaitResult(-1);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
342 return m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
343 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
344 public new T Join(int timeout) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
345 WaitResult(timeout);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
346 return m_result;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
347 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
348
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
349 public IPromise<T> On(Action<T> success, Action<Exception> error, Action<Exception> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
350 AddHandler(new ValueEventDescriptor(success, error, cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
351 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
352 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
353
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
354 public IPromise<T> On(Action<T> success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
355 AddHandler(new ValueEventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
356 return this;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
359 public IPromise<T> On(Action<T> success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
360 AddHandler(new ValueEventDescriptor(success, null, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
361 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
362 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
363
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
364 public IPromise<T> On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
365 Listen(events, handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
366 return this;
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
369 public IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception, T2> error, Func<Exception, T2> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
370 var promise = new Promise<T2>();
135
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
371 if (mapper != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
372 promise.On((Action)null, null, Cancel);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
373 AddHandler(new RemapDescriptor<T2>(mapper, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
374 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
375 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
376
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
377 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
378 var promise = new Promise<T2>();
135
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
379 if (mapper != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
380 promise.On((Action)null, null, Cancel);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
381 AddHandler(new RemapDescriptor<T2>(mapper, error, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
382 return 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 public IPromise<T2> Then<T2>(Func<T, T2> mapper) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
386 var promise = new Promise<T2>();
135
656815cb7147 Fixed chaining of promises with 'Then' method
cin
parents: 130
diff changeset
387 if (mapper != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
388 promise.On((Action)null, null, Cancel);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
389 AddHandler(new RemapDescriptor<T2>(mapper, null, null, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
390 return promise;
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
393 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception, IPromise<T2>> error, Func<Exception, IPromise<T2>> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
394 // this promise will be resolved when an asyc operation is started
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
395 var promise = new Promise<IPromise<T2>>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
396
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
397 AddHandler(new RemapDescriptor<IPromise<T2>>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
398 chained,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
399 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
400 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
401 promise
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
404 var medium = new Promise<T2>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
405
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
406 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
407 medium.On(Cancel, PromiseEventType.Cancelled);
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 // we need to connect started async operation with the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
410 // if the async operation hasn't been started by the some reason
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
411 // report is to the medium
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
412 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
413 result => ConnectPromise<T2>(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
414 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
415 medium.Cancel
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 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
419 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
420
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
421 static void ConnectPromise<T2>(IPromise<T2> result, Promise<T2> medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
422 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
423 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
424 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
425 medium.Reject,
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
426 medium.Cancel
119
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 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
429 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
430 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
431 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
432 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
433 " 'null' where the promise instance is expected"
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 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
436 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
437 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
438
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
439 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception, IPromise<T2>> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
440 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
441 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
442
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
443 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
444 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
445 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
446
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
447 public IPromise Then(Action success, Action<Exception> error, Action<Exception> cancel) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
448 var promise = new Promise();
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
449 if (success != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
450 promise.On(null, null, Cancel);
119
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 AddHandler(new EventDescriptor(success, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
453
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
454 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
455 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
456
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
457 public IPromise Then(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
458 return Then(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
459 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
460
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
461 public IPromise Then(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
462 return Then(success, null, null);
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
465 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error, Func<Exception, IPromise> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
466 var promise = new Promise<IPromise>();
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 AddHandler(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
469 new RemapDescriptor<IPromise>(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
470 x => chained(),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
471 error,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
472 cancel,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
473 promise
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
477 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
478 if (chained != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
479 medium.On(null, null, Cancel);
119
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 promise.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
482 result => ConnectPromise(result, medium),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
483 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
484 medium.Cancel
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 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
488 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
489
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
490 static void ConnectPromise(IPromise result, Promise medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
491 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
492 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
493 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
494 medium.Reject,
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
495 medium.Cancel
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
496 );
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
497 medium.On(null, null, result.Cancel);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
498 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
499 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
500 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
501 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
502 " 'null' where the promise instance is expected"
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 );
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
508 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
509 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
510 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
511
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
512 public IPromise Chain(Func<IPromise> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
513 return Chain(chained, null, null);
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
516 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
517 AddHandler(new EventDescriptor(success,error,cancel, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
518 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
519 }
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 public IPromise On(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
522 AddHandler(new EventDescriptor(success, error, null, StubDeferred.instance));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
523 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
524 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
525
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
526 public IPromise On(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
527 Listen(PromiseEventType.Success, success);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
528 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
529 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
530
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
531 IPromise IPromise.On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
532 Listen(events,handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
533 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
534 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
535
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
536 public IPromise<T2> Cast<T2>() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
537 return (IPromise<T2>)this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
538 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
539
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
540 #region implemented abstract members of AbstractPromise
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 protected override void SignalSuccess(IDeferred<T> handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
543 handler.Resolve(m_result);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
544 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
545
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
546 protected override void SignalError(IDeferred<T> handler, Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
547 handler.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
548 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
549
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
550 protected override void SignalCancelled(IDeferred<T> handler, Exception reason) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 135
diff changeset
551 handler.Cancel(reason);
119
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
554 protected override void Listen(PromiseEventType events, Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
555 if (handler != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
556 AddHandler(new ListenerDescriptor(handler, events));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
557 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
558
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
559 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
560
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
561 public static IPromise<T> ResultToPromise(T value) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
562 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
563 p.Resolve(value);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
564 return p;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
567 public static IPromise<T> ExceptionToPromise(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
568 var p = new Promise<T>();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
569 p.Reject(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
570 return p;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
573 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
574 }