annotate Implab/Promise.cs @ 5:f2559580b481

implemented overlay helpers and some animation heplers improvments
author cin
date Tue, 24 Sep 2013 03:31:24 +0400
parents aa367305156b
children dfa21d507bc5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
aa367305156b small fixes
cin
parents: 1
diff changeset
1 using System;
aa367305156b small fixes
cin
parents: 1
diff changeset
2 using System.Collections.Generic;
aa367305156b small fixes
cin
parents: 1
diff changeset
3 using System.Linq;
aa367305156b small fixes
cin
parents: 1
diff changeset
4 using System.Reflection;
aa367305156b small fixes
cin
parents: 1
diff changeset
5 using System.Text;
aa367305156b small fixes
cin
parents: 1
diff changeset
6 using System.Diagnostics;
aa367305156b small fixes
cin
parents: 1
diff changeset
7 using System.Threading;
aa367305156b small fixes
cin
parents: 1
diff changeset
8
aa367305156b small fixes
cin
parents: 1
diff changeset
9 namespace Implab {
aa367305156b small fixes
cin
parents: 1
diff changeset
10
aa367305156b small fixes
cin
parents: 1
diff changeset
11 public delegate void ErrorHandler(Exception e);
aa367305156b small fixes
cin
parents: 1
diff changeset
12
aa367305156b small fixes
cin
parents: 1
diff changeset
13 public delegate void ResultHandler<T>(T result);
aa367305156b small fixes
cin
parents: 1
diff changeset
14 public delegate TNew ResultMapper<TSrc,TNew>(TSrc result);
aa367305156b small fixes
cin
parents: 1
diff changeset
15 public delegate Promise<TNew> ChainedOperation<TSrc,TNew>(TSrc result);
aa367305156b small fixes
cin
parents: 1
diff changeset
16
aa367305156b small fixes
cin
parents: 1
diff changeset
17 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
18 /// Класс для асинхронного получения результатов. Так называемое "обещание".
aa367305156b small fixes
cin
parents: 1
diff changeset
19 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
20 /// <typeparam name="T">Тип получаемого результата</typeparam>
aa367305156b small fixes
cin
parents: 1
diff changeset
21 /// <remarks>
aa367305156b small fixes
cin
parents: 1
diff changeset
22 /// <para>Сервис при обращении к его методу дает обещаиние о выполнении операции,
aa367305156b small fixes
cin
parents: 1
diff changeset
23 /// клиент получив такое обещание может установить ряд обратных вызово для получения
aa367305156b small fixes
cin
parents: 1
diff changeset
24 /// событий выполнения обещания, тоесть завершения операции и предоставлении результатов.</para>
aa367305156b small fixes
cin
parents: 1
diff changeset
25 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
26 /// Обещение может быть как выполнено, так и выполнено с ошибкой. Для подписки на
aa367305156b small fixes
cin
parents: 1
diff changeset
27 /// данные события клиент должен использовать методы <c>Then</c>.
aa367305156b small fixes
cin
parents: 1
diff changeset
28 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
29 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
30 /// Сервис, в свою очередь, по окончанию выполнения операции (возможно с ошибкой),
aa367305156b small fixes
cin
parents: 1
diff changeset
31 /// использует методы <c>Resolve</c> либо <c>Reject</c> для оповещения клиетна о
aa367305156b small fixes
cin
parents: 1
diff changeset
32 /// выполнении обещания.
aa367305156b small fixes
cin
parents: 1
diff changeset
33 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
34 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
35 /// Если сервер успел выполнить обещание еще до того, как клиент на него подписался,
aa367305156b small fixes
cin
parents: 1
diff changeset
36 /// то в момент подписки клиента будут вызваны соответсвующие события в синхронном
aa367305156b small fixes
cin
parents: 1
diff changeset
37 /// режиме и клиент будет оповещен в любом случае. Иначе, обработчики добавляются в
aa367305156b small fixes
cin
parents: 1
diff changeset
38 /// список в порядке подписания и в этом же порядке они будут вызваны при выполнении
aa367305156b small fixes
cin
parents: 1
diff changeset
39 /// обещания.
aa367305156b small fixes
cin
parents: 1
diff changeset
40 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
41 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
42 /// Обрабатывая результаты обещания можно преобразовывать результаты либо инициировать
aa367305156b small fixes
cin
parents: 1
diff changeset
43 /// связанные асинхронные операции, которые также возвращают обещания. Для этого следует
aa367305156b small fixes
cin
parents: 1
diff changeset
44 /// использовать соответствующую форму методе <c>Then</c>.
aa367305156b small fixes
cin
parents: 1
diff changeset
45 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
46 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
47 /// Также хорошим правилом является то, что <c>Resolve</c> и <c>Reject</c> должен вызывать
aa367305156b small fixes
cin
parents: 1
diff changeset
48 /// только инициатор обещания иначе могут возникнуть противоречия.
aa367305156b small fixes
cin
parents: 1
diff changeset
49 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
50 /// </remarks>
aa367305156b small fixes
cin
parents: 1
diff changeset
51 public class Promise<T> {
aa367305156b small fixes
cin
parents: 1
diff changeset
52
aa367305156b small fixes
cin
parents: 1
diff changeset
53 struct ResultHandlerInfo {
aa367305156b small fixes
cin
parents: 1
diff changeset
54 public ResultHandler<T> resultHandler;
aa367305156b small fixes
cin
parents: 1
diff changeset
55 public ErrorHandler errorHandler;
aa367305156b small fixes
cin
parents: 1
diff changeset
56 }
aa367305156b small fixes
cin
parents: 1
diff changeset
57
aa367305156b small fixes
cin
parents: 1
diff changeset
58 enum State {
aa367305156b small fixes
cin
parents: 1
diff changeset
59 Unresolved,
aa367305156b small fixes
cin
parents: 1
diff changeset
60 Resolving,
aa367305156b small fixes
cin
parents: 1
diff changeset
61 Resolved,
aa367305156b small fixes
cin
parents: 1
diff changeset
62 Cancelled
aa367305156b small fixes
cin
parents: 1
diff changeset
63 }
aa367305156b small fixes
cin
parents: 1
diff changeset
64
aa367305156b small fixes
cin
parents: 1
diff changeset
65 LinkedList<ResultHandlerInfo> m_handlersChain = new LinkedList<ResultHandlerInfo>();
aa367305156b small fixes
cin
parents: 1
diff changeset
66 State m_state;
aa367305156b small fixes
cin
parents: 1
diff changeset
67 bool m_cancellable;
aa367305156b small fixes
cin
parents: 1
diff changeset
68 T m_result;
aa367305156b small fixes
cin
parents: 1
diff changeset
69 Exception m_error;
aa367305156b small fixes
cin
parents: 1
diff changeset
70
aa367305156b small fixes
cin
parents: 1
diff changeset
71 public Promise() {
aa367305156b small fixes
cin
parents: 1
diff changeset
72 m_cancellable = true;
aa367305156b small fixes
cin
parents: 1
diff changeset
73 }
aa367305156b small fixes
cin
parents: 1
diff changeset
74
aa367305156b small fixes
cin
parents: 1
diff changeset
75 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
76 /// Событие, возникающее при отмене асинхронной операции.
aa367305156b small fixes
cin
parents: 1
diff changeset
77 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
78 /// <description>
aa367305156b small fixes
cin
parents: 1
diff changeset
79 /// Как правило используется для оповещения объекта, выполняющего асинхронную операцию, о том, что ее следует отменить.
aa367305156b small fixes
cin
parents: 1
diff changeset
80 /// </description>
aa367305156b small fixes
cin
parents: 1
diff changeset
81 public event EventHandler Cancelled;
aa367305156b small fixes
cin
parents: 1
diff changeset
82
aa367305156b small fixes
cin
parents: 1
diff changeset
83 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
84 /// Выполняет обещание, сообщая об успешном выполнении.
aa367305156b small fixes
cin
parents: 1
diff changeset
85 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
86 /// <param name="result">Результат выполнения.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
87 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
aa367305156b small fixes
cin
parents: 1
diff changeset
88 public void Resolve(T result) {
aa367305156b small fixes
cin
parents: 1
diff changeset
89 lock (this) {
aa367305156b small fixes
cin
parents: 1
diff changeset
90 if (m_state == State.Cancelled)
aa367305156b small fixes
cin
parents: 1
diff changeset
91 return;
aa367305156b small fixes
cin
parents: 1
diff changeset
92 if (m_state != State.Unresolved)
aa367305156b small fixes
cin
parents: 1
diff changeset
93 throw new InvalidOperationException("The promise is already resolved");
aa367305156b small fixes
cin
parents: 1
diff changeset
94 m_result = result;
aa367305156b small fixes
cin
parents: 1
diff changeset
95 m_state = State.Resolving;
aa367305156b small fixes
cin
parents: 1
diff changeset
96 }
aa367305156b small fixes
cin
parents: 1
diff changeset
97
aa367305156b small fixes
cin
parents: 1
diff changeset
98 ResultHandlerInfo handler;
aa367305156b small fixes
cin
parents: 1
diff changeset
99 while (FetchNextHandler(out handler))
aa367305156b small fixes
cin
parents: 1
diff changeset
100 InvokeHandler(handler);
aa367305156b small fixes
cin
parents: 1
diff changeset
101 }
aa367305156b small fixes
cin
parents: 1
diff changeset
102
aa367305156b small fixes
cin
parents: 1
diff changeset
103 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
104 /// Выполняет обещание, сообщая об ошибке
aa367305156b small fixes
cin
parents: 1
diff changeset
105 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
106 /// <param name="error">Исключение возникшее при выполнении операции</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
107 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
aa367305156b small fixes
cin
parents: 1
diff changeset
108 public void Reject(Exception error) {
aa367305156b small fixes
cin
parents: 1
diff changeset
109 lock (this) {
aa367305156b small fixes
cin
parents: 1
diff changeset
110 if (m_state == State.Cancelled)
aa367305156b small fixes
cin
parents: 1
diff changeset
111 return;
aa367305156b small fixes
cin
parents: 1
diff changeset
112 if (m_state != State.Unresolved)
aa367305156b small fixes
cin
parents: 1
diff changeset
113 throw new InvalidOperationException("The promise is already resolved");
aa367305156b small fixes
cin
parents: 1
diff changeset
114 m_error = error;
aa367305156b small fixes
cin
parents: 1
diff changeset
115 m_state = State.Resolving;
aa367305156b small fixes
cin
parents: 1
diff changeset
116 }
aa367305156b small fixes
cin
parents: 1
diff changeset
117
aa367305156b small fixes
cin
parents: 1
diff changeset
118 ResultHandlerInfo handler;
aa367305156b small fixes
cin
parents: 1
diff changeset
119 while (FetchNextHandler(out handler))
aa367305156b small fixes
cin
parents: 1
diff changeset
120 InvokeHandler(handler);
aa367305156b small fixes
cin
parents: 1
diff changeset
121 }
aa367305156b small fixes
cin
parents: 1
diff changeset
122
aa367305156b small fixes
cin
parents: 1
diff changeset
123 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
124 /// Отменяет операцию, если это возможно.
aa367305156b small fixes
cin
parents: 1
diff changeset
125 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
126 /// <returns><c>true</c> Операция была отменена, обработчики не будут вызваны.<c>false</c> отмена не возможна, поскольку обещание уже выполнено и обработчики отработали.</returns>
aa367305156b small fixes
cin
parents: 1
diff changeset
127 public bool Cancel() {
aa367305156b small fixes
cin
parents: 1
diff changeset
128 lock(this) {
aa367305156b small fixes
cin
parents: 1
diff changeset
129 if (m_state == State.Unresolved && m_cancellable) {
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents: 0
diff changeset
130 m_state = State.Cancelled;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents: 0
diff changeset
131 EventHandler temp = Cancelled;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents: 0
diff changeset
132
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents: 0
diff changeset
133 if (temp != null)
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents: 0
diff changeset
134 temp(this,new EventArgs());
2
aa367305156b small fixes
cin
parents: 1
diff changeset
135
aa367305156b small fixes
cin
parents: 1
diff changeset
136 return true;
aa367305156b small fixes
cin
parents: 1
diff changeset
137 } else
aa367305156b small fixes
cin
parents: 1
diff changeset
138 return false;
aa367305156b small fixes
cin
parents: 1
diff changeset
139 }
aa367305156b small fixes
cin
parents: 1
diff changeset
140 }
aa367305156b small fixes
cin
parents: 1
diff changeset
141
aa367305156b small fixes
cin
parents: 1
diff changeset
142 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
143 /// Добавляет обработчики событий выполнения обещания.
aa367305156b small fixes
cin
parents: 1
diff changeset
144 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
145 /// <param name="success">Обработчик успешного выполнения обещания.
aa367305156b small fixes
cin
parents: 1
diff changeset
146 /// Данному обработчику будет передан результат выполнения операции.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
147 /// <param name="error">Обработчик ошибки. Данный обработчик получит
aa367305156b small fixes
cin
parents: 1
diff changeset
148 /// исключение возникшее при выполнении операции.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
149 /// <returns>Само обещание</returns>
aa367305156b small fixes
cin
parents: 1
diff changeset
150 public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) {
aa367305156b small fixes
cin
parents: 1
diff changeset
151 if (success == null && error == null)
aa367305156b small fixes
cin
parents: 1
diff changeset
152 return this;
aa367305156b small fixes
cin
parents: 1
diff changeset
153
aa367305156b small fixes
cin
parents: 1
diff changeset
154 AddHandler(new ResultHandlerInfo() {
aa367305156b small fixes
cin
parents: 1
diff changeset
155 resultHandler = success,
aa367305156b small fixes
cin
parents: 1
diff changeset
156 errorHandler = error
aa367305156b small fixes
cin
parents: 1
diff changeset
157 });
aa367305156b small fixes
cin
parents: 1
diff changeset
158
aa367305156b small fixes
cin
parents: 1
diff changeset
159 return this;
aa367305156b small fixes
cin
parents: 1
diff changeset
160 }
aa367305156b small fixes
cin
parents: 1
diff changeset
161
aa367305156b small fixes
cin
parents: 1
diff changeset
162 public Promise<T> Then(ResultHandler<T> success) {
aa367305156b small fixes
cin
parents: 1
diff changeset
163 return Then (success, null);
aa367305156b small fixes
cin
parents: 1
diff changeset
164 }
aa367305156b small fixes
cin
parents: 1
diff changeset
165
aa367305156b small fixes
cin
parents: 1
diff changeset
166 public Promise<T> Anyway(Action handler) {
aa367305156b small fixes
cin
parents: 1
diff changeset
167 if (handler == null)
aa367305156b small fixes
cin
parents: 1
diff changeset
168 return this;
aa367305156b small fixes
cin
parents: 1
diff changeset
169 AddHandler(new ResultHandlerInfo {
aa367305156b small fixes
cin
parents: 1
diff changeset
170 resultHandler = x => handler(),
aa367305156b small fixes
cin
parents: 1
diff changeset
171 errorHandler = x => handler()
aa367305156b small fixes
cin
parents: 1
diff changeset
172 });
aa367305156b small fixes
cin
parents: 1
diff changeset
173
aa367305156b small fixes
cin
parents: 1
diff changeset
174 return this;
aa367305156b small fixes
cin
parents: 1
diff changeset
175 }
aa367305156b small fixes
cin
parents: 1
diff changeset
176
aa367305156b small fixes
cin
parents: 1
diff changeset
177 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
178 /// Позволяет преобразовать результат выполения операции к новому типу.
aa367305156b small fixes
cin
parents: 1
diff changeset
179 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
180 /// <typeparam name="TNew">Новый тип результата.</typeparam>
aa367305156b small fixes
cin
parents: 1
diff changeset
181 /// <param name="mapper">Преобразование результата к новому типу.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
182 /// <param name="error">Обработчик ошибки. Данный обработчик получит
aa367305156b small fixes
cin
parents: 1
diff changeset
183 /// исключение возникшее при выполнении операции.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
184 /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns>
aa367305156b small fixes
cin
parents: 1
diff changeset
185 public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) {
aa367305156b small fixes
cin
parents: 1
diff changeset
186 if (mapper == null)
aa367305156b small fixes
cin
parents: 1
diff changeset
187 throw new ArgumentNullException("mapper");
aa367305156b small fixes
cin
parents: 1
diff changeset
188
aa367305156b small fixes
cin
parents: 1
diff changeset
189 // создаем прицепленное обещание
aa367305156b small fixes
cin
parents: 1
diff changeset
190 Promise<TNew> chained = new Promise<TNew>();
aa367305156b small fixes
cin
parents: 1
diff changeset
191
aa367305156b small fixes
cin
parents: 1
diff changeset
192 AddHandler(new ResultHandlerInfo() {
aa367305156b small fixes
cin
parents: 1
diff changeset
193 resultHandler = delegate(T result) {
aa367305156b small fixes
cin
parents: 1
diff changeset
194 try {
aa367305156b small fixes
cin
parents: 1
diff changeset
195 // если преобразование выдаст исключение, то сработает reject сцепленного deferred
aa367305156b small fixes
cin
parents: 1
diff changeset
196 chained.Resolve(mapper(result));
aa367305156b small fixes
cin
parents: 1
diff changeset
197 } catch (Exception e) {
aa367305156b small fixes
cin
parents: 1
diff changeset
198 chained.Reject(e);
aa367305156b small fixes
cin
parents: 1
diff changeset
199 }
aa367305156b small fixes
cin
parents: 1
diff changeset
200 },
aa367305156b small fixes
cin
parents: 1
diff changeset
201 errorHandler = delegate(Exception e) {
aa367305156b small fixes
cin
parents: 1
diff changeset
202 if (error != null)
aa367305156b small fixes
cin
parents: 1
diff changeset
203 error(e);
aa367305156b small fixes
cin
parents: 1
diff changeset
204 // в случае ошибки нужно передать исключение дальше по цепочке
aa367305156b small fixes
cin
parents: 1
diff changeset
205 chained.Reject(e);
aa367305156b small fixes
cin
parents: 1
diff changeset
206 }
aa367305156b small fixes
cin
parents: 1
diff changeset
207 });
aa367305156b small fixes
cin
parents: 1
diff changeset
208
aa367305156b small fixes
cin
parents: 1
diff changeset
209 return chained;
aa367305156b small fixes
cin
parents: 1
diff changeset
210 }
aa367305156b small fixes
cin
parents: 1
diff changeset
211
aa367305156b small fixes
cin
parents: 1
diff changeset
212 public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
aa367305156b small fixes
cin
parents: 1
diff changeset
213 return Map (mapper, null);
aa367305156b small fixes
cin
parents: 1
diff changeset
214 }
aa367305156b small fixes
cin
parents: 1
diff changeset
215
aa367305156b small fixes
cin
parents: 1
diff changeset
216 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
217 /// Сцепляет несколько аснхронных операций. Указанная асинхронная операция будет вызвана после
aa367305156b small fixes
cin
parents: 1
diff changeset
218 /// выполнения текущей, а результат текущей операции может быть использован для инициализации
aa367305156b small fixes
cin
parents: 1
diff changeset
219 /// новой операции.
aa367305156b small fixes
cin
parents: 1
diff changeset
220 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
221 /// <typeparam name="TNew">Тип результата указанной асинхронной операции.</typeparam>
aa367305156b small fixes
cin
parents: 1
diff changeset
222 /// <param name="chained">Асинхронная операция, которая должна будет начаться после выполнения текущей.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
223 /// <param name="error">Обработчик ошибки. Данный обработчик получит
aa367305156b small fixes
cin
parents: 1
diff changeset
224 /// исключение возникшее при выполнении текуещй операции.</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
225 /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns>
aa367305156b small fixes
cin
parents: 1
diff changeset
226 public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) {
aa367305156b small fixes
cin
parents: 1
diff changeset
227
aa367305156b small fixes
cin
parents: 1
diff changeset
228 // проблема в том, что на момент связывания еще не начата асинхронная операция, поэтому нужно
aa367305156b small fixes
cin
parents: 1
diff changeset
229 // создать посредника, к которому будут подвызяваться следующие обработчики.
aa367305156b small fixes
cin
parents: 1
diff changeset
230 // когда будет выполнена реальная асинхронная операция, она обратиться к посреднику, чтобы
aa367305156b small fixes
cin
parents: 1
diff changeset
231 // передать через него результаты работы.
aa367305156b small fixes
cin
parents: 1
diff changeset
232 Promise<TNew> medium = new Promise<TNew>();
aa367305156b small fixes
cin
parents: 1
diff changeset
233
aa367305156b small fixes
cin
parents: 1
diff changeset
234 AddHandler(new ResultHandlerInfo() {
aa367305156b small fixes
cin
parents: 1
diff changeset
235 resultHandler = delegate(T result) {
aa367305156b small fixes
cin
parents: 1
diff changeset
236 try {
aa367305156b small fixes
cin
parents: 1
diff changeset
237 chained(result).Then(
aa367305156b small fixes
cin
parents: 1
diff changeset
238 x => medium.Resolve(x),
aa367305156b small fixes
cin
parents: 1
diff changeset
239 e => medium.Reject(e)
aa367305156b small fixes
cin
parents: 1
diff changeset
240 );
aa367305156b small fixes
cin
parents: 1
diff changeset
241 } catch(Exception e) {
aa367305156b small fixes
cin
parents: 1
diff changeset
242 // если сцепленное действие выдало исключение вместо обещания, то передаем ошибку по цепочке
aa367305156b small fixes
cin
parents: 1
diff changeset
243 medium.Reject(e);
aa367305156b small fixes
cin
parents: 1
diff changeset
244 }
aa367305156b small fixes
cin
parents: 1
diff changeset
245 },
aa367305156b small fixes
cin
parents: 1
diff changeset
246 errorHandler = delegate(Exception e) {
aa367305156b small fixes
cin
parents: 1
diff changeset
247 if (error != null)
aa367305156b small fixes
cin
parents: 1
diff changeset
248 error(e);
aa367305156b small fixes
cin
parents: 1
diff changeset
249 // в случае ошибки нужно передать исключение дальше по цепочке
aa367305156b small fixes
cin
parents: 1
diff changeset
250 medium.Reject(e);
aa367305156b small fixes
cin
parents: 1
diff changeset
251 }
aa367305156b small fixes
cin
parents: 1
diff changeset
252 });
aa367305156b small fixes
cin
parents: 1
diff changeset
253
aa367305156b small fixes
cin
parents: 1
diff changeset
254 return medium;
aa367305156b small fixes
cin
parents: 1
diff changeset
255 }
aa367305156b small fixes
cin
parents: 1
diff changeset
256
aa367305156b small fixes
cin
parents: 1
diff changeset
257 public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
aa367305156b small fixes
cin
parents: 1
diff changeset
258 return Chain (chained, null);
aa367305156b small fixes
cin
parents: 1
diff changeset
259 }
aa367305156b small fixes
cin
parents: 1
diff changeset
260
aa367305156b small fixes
cin
parents: 1
diff changeset
261 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
262 /// Дожидается отложенного обещания и в случае успеха, возвращает
aa367305156b small fixes
cin
parents: 1
diff changeset
263 /// его, результат, в противном случае бросает исключение.
aa367305156b small fixes
cin
parents: 1
diff changeset
264 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
265 /// <remarks>
aa367305156b small fixes
cin
parents: 1
diff changeset
266 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
267 /// Если ожидание обещания было прервано по таймауту, это не значит,
aa367305156b small fixes
cin
parents: 1
diff changeset
268 /// что обещание было отменено или что-то в этом роде, это только
aa367305156b small fixes
cin
parents: 1
diff changeset
269 /// означает, что мы его не дождались, однако все зарегистрированные
aa367305156b small fixes
cin
parents: 1
diff changeset
270 /// обработчики, как были так остались и они будут вызваны, когда
aa367305156b small fixes
cin
parents: 1
diff changeset
271 /// обещание будет выполнено.
aa367305156b small fixes
cin
parents: 1
diff changeset
272 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
273 /// <para>
aa367305156b small fixes
cin
parents: 1
diff changeset
274 /// Такое поведение вполне оправдано поскольку таймаут может истечь
aa367305156b small fixes
cin
parents: 1
diff changeset
275 /// в тот момент, когда началась обработка цепочки обработчиков, и
aa367305156b small fixes
cin
parents: 1
diff changeset
276 /// к тому же текущее обещание может стоять в цепочке обещаний и его
aa367305156b small fixes
cin
parents: 1
diff changeset
277 /// отклонение может привести к непрогнозируемому результату.
aa367305156b small fixes
cin
parents: 1
diff changeset
278 /// </para>
aa367305156b small fixes
cin
parents: 1
diff changeset
279 /// </remarks>
aa367305156b small fixes
cin
parents: 1
diff changeset
280 /// <param name="timeout">Время ожидания</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
281 /// <returns>Результат выполнения обещания</returns>
aa367305156b small fixes
cin
parents: 1
diff changeset
282 public T Join(int timeout) {
aa367305156b small fixes
cin
parents: 1
diff changeset
283 ManualResetEvent evt = new ManualResetEvent(false);
aa367305156b small fixes
cin
parents: 1
diff changeset
284 Anyway(() => evt.Set());
aa367305156b small fixes
cin
parents: 1
diff changeset
285
aa367305156b small fixes
cin
parents: 1
diff changeset
286 if (!evt.WaitOne(timeout, true))
aa367305156b small fixes
cin
parents: 1
diff changeset
287 throw new TimeoutException();
aa367305156b small fixes
cin
parents: 1
diff changeset
288
aa367305156b small fixes
cin
parents: 1
diff changeset
289 if (m_error != null)
aa367305156b small fixes
cin
parents: 1
diff changeset
290 throw new TargetInvocationException( m_error );
aa367305156b small fixes
cin
parents: 1
diff changeset
291 else
aa367305156b small fixes
cin
parents: 1
diff changeset
292 return m_result;
aa367305156b small fixes
cin
parents: 1
diff changeset
293 }
aa367305156b small fixes
cin
parents: 1
diff changeset
294
aa367305156b small fixes
cin
parents: 1
diff changeset
295 public T Join() {
aa367305156b small fixes
cin
parents: 1
diff changeset
296 return Join(Timeout.Infinite);
aa367305156b small fixes
cin
parents: 1
diff changeset
297 }
aa367305156b small fixes
cin
parents: 1
diff changeset
298
aa367305156b small fixes
cin
parents: 1
diff changeset
299 /// <summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
300 /// Данный метод последовательно извлекает обработчики обещания и когда
aa367305156b small fixes
cin
parents: 1
diff changeset
301 /// их больше не осталось - ставит состояние "разрешено".
aa367305156b small fixes
cin
parents: 1
diff changeset
302 /// </summary>
aa367305156b small fixes
cin
parents: 1
diff changeset
303 /// <param name="handler">Информация об обработчике</param>
aa367305156b small fixes
cin
parents: 1
diff changeset
304 /// <returns>Признак того, что еще остались обработчики в очереди</returns>
aa367305156b small fixes
cin
parents: 1
diff changeset
305 bool FetchNextHandler(out ResultHandlerInfo handler) {
aa367305156b small fixes
cin
parents: 1
diff changeset
306 handler = default(ResultHandlerInfo);
aa367305156b small fixes
cin
parents: 1
diff changeset
307
aa367305156b small fixes
cin
parents: 1
diff changeset
308 lock (this) {
aa367305156b small fixes
cin
parents: 1
diff changeset
309 Debug.Assert(m_state == State.Resolving);
aa367305156b small fixes
cin
parents: 1
diff changeset
310
aa367305156b small fixes
cin
parents: 1
diff changeset
311 if (m_handlersChain.Count > 0) {
aa367305156b small fixes
cin
parents: 1
diff changeset
312 handler = m_handlersChain.First.Value;
aa367305156b small fixes
cin
parents: 1
diff changeset
313 m_handlersChain.RemoveFirst();
aa367305156b small fixes
cin
parents: 1
diff changeset
314 return true;
aa367305156b small fixes
cin
parents: 1
diff changeset
315 } else {
aa367305156b small fixes
cin
parents: 1
diff changeset
316 m_state = State.Resolved;
aa367305156b small fixes
cin
parents: 1
diff changeset
317 return false;
aa367305156b small fixes
cin
parents: 1
diff changeset
318 }
aa367305156b small fixes
cin
parents: 1
diff changeset
319 }
aa367305156b small fixes
cin
parents: 1
diff changeset
320 }
aa367305156b small fixes
cin
parents: 1
diff changeset
321
aa367305156b small fixes
cin
parents: 1
diff changeset
322 void AddHandler(ResultHandlerInfo handler) {
aa367305156b small fixes
cin
parents: 1
diff changeset
323 bool invokeRequired = false;
aa367305156b small fixes
cin
parents: 1
diff changeset
324
aa367305156b small fixes
cin
parents: 1
diff changeset
325 lock (this) {
aa367305156b small fixes
cin
parents: 1
diff changeset
326 if (m_state != State.Resolved)
aa367305156b small fixes
cin
parents: 1
diff changeset
327 m_handlersChain.AddLast(handler);
aa367305156b small fixes
cin
parents: 1
diff changeset
328 else
aa367305156b small fixes
cin
parents: 1
diff changeset
329 invokeRequired = true;
aa367305156b small fixes
cin
parents: 1
diff changeset
330 }
aa367305156b small fixes
cin
parents: 1
diff changeset
331
aa367305156b small fixes
cin
parents: 1
diff changeset
332 // обработчики не должны блокировать сам объект
aa367305156b small fixes
cin
parents: 1
diff changeset
333 if (invokeRequired)
aa367305156b small fixes
cin
parents: 1
diff changeset
334 InvokeHandler(handler);
aa367305156b small fixes
cin
parents: 1
diff changeset
335 }
aa367305156b small fixes
cin
parents: 1
diff changeset
336
aa367305156b small fixes
cin
parents: 1
diff changeset
337 void InvokeHandler(ResultHandlerInfo handler) {
aa367305156b small fixes
cin
parents: 1
diff changeset
338 if (m_error == null) {
aa367305156b small fixes
cin
parents: 1
diff changeset
339 try {
aa367305156b small fixes
cin
parents: 1
diff changeset
340 if (handler.resultHandler != null)
aa367305156b small fixes
cin
parents: 1
diff changeset
341 handler.resultHandler(m_result);
aa367305156b small fixes
cin
parents: 1
diff changeset
342 } catch { }
aa367305156b small fixes
cin
parents: 1
diff changeset
343 }
aa367305156b small fixes
cin
parents: 1
diff changeset
344
aa367305156b small fixes
cin
parents: 1
diff changeset
345 if (m_error != null) {
aa367305156b small fixes
cin
parents: 1
diff changeset
346 try {
aa367305156b small fixes
cin
parents: 1
diff changeset
347 if (handler.errorHandler !=null)
aa367305156b small fixes
cin
parents: 1
diff changeset
348 handler.errorHandler(m_error);
aa367305156b small fixes
cin
parents: 1
diff changeset
349 } catch { }
aa367305156b small fixes
cin
parents: 1
diff changeset
350 }
aa367305156b small fixes
cin
parents: 1
diff changeset
351 }
aa367305156b small fixes
cin
parents: 1
diff changeset
352
aa367305156b small fixes
cin
parents: 1
diff changeset
353
aa367305156b small fixes
cin
parents: 1
diff changeset
354 }
aa367305156b small fixes
cin
parents: 1
diff changeset
355 }