annotate Implab/Promise.cs @ 1:6fb3b01ee971

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