annotate Implab/Promise.cs @ 0:279591fb4df3

initial commit promises async model
author user@factory.site.local
date Fri, 23 Aug 2013 04:38:46 +0400
parents
children 6fb3b01ee971
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) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
130 m_state = State.Cancelled;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
131 return true;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
132 } else
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
133 return false;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
134 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
135 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
136
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
137 /// <summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
138 /// Добавляет обработчики событий выполнения обещания.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
139 /// </summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
140 /// <param name="success">Обработчик успешного выполнения обещания.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
141 /// Данному обработчику будет передан результат выполнения операции.</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
142 /// <param name="error">Обработчик ошибки. Данный обработчик получит
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
143 /// исключение возникшее при выполнении операции.</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
144 /// <returns>Само обещание</returns>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
145 public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
146 if (success == null && error == null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
147 return this;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
148
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
149 AddHandler(new ResultHandlerInfo() {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
150 resultHandler = success,
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
151 errorHandler = error
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
152 });
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
153
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
154 return this;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
155 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
156
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
157 public Promise<T> Then(ResultHandler<T> success) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
158 return Then (success, null);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
159 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
160
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
161 public Promise<T> Anyway(Action handler) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
162 if (handler == null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
163 return this;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
164 AddHandler(new ResultHandlerInfo {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
165 resultHandler = x => handler(),
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
166 errorHandler = x => handler()
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
167 });
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
168
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
169 return this;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
170 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
171
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
172 /// <summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
173 /// Позволяет преобразовать результат выполения операции к новому типу.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
174 /// </summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
175 /// <typeparam name="TNew">Новый тип результата.</typeparam>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
176 /// <param name="mapper">Преобразование результата к новому типу.</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
177 /// <param name="error">Обработчик ошибки. Данный обработчик получит
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
178 /// исключение возникшее при выполнении операции.</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
179 /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
180 public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
181 if (mapper == null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
182 throw new ArgumentNullException("mapper");
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
183
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
184 // создаем прицепленное обещание
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
185 Promise<TNew> chained = new Promise<TNew>();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
186
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
187 AddHandler(new ResultHandlerInfo() {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
188 resultHandler = delegate(T result) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
189 try {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
190 // если преобразование выдаст исключение, то сработает reject сцепленного deferred
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
191 chained.Resolve(mapper(result));
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
192 } catch (Exception e) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
193 chained.Reject(e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
194 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
195 },
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
196 errorHandler = delegate(Exception e) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
197 if (error != null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
198 error(e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
199 // в случае ошибки нужно передать исключение дальше по цепочке
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
200 chained.Reject(e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
201 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
202 });
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
203
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
204 return chained;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
205 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
206
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
207 public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
208 return Map (mapper, null);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
209 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
210
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
211 /// <summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
212 /// Сцепляет несколько аснхронных операций. Указанная асинхронная операция будет вызвана после
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
213 /// выполнения текущей, а результат текущей операции может быть использован для инициализации
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
214 /// новой операции.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
215 /// </summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
216 /// <typeparam name="TNew">Тип результата указанной асинхронной операции.</typeparam>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
217 /// <param name="chained">Асинхронная операция, которая должна будет начаться после выполнения текущей.</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
218 /// <param name="error">Обработчик ошибки. Данный обработчик получит
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
219 /// исключение возникшее при выполнении текуещй операции.</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
220 /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
221 public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
222
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
223 // проблема в том, что на момент связывания еще не начата асинхронная операция, поэтому нужно
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
224 // создать посредника, к которому будут подвызяваться следующие обработчики.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
225 // когда будет выполнена реальная асинхронная операция, она обратиться к посреднику, чтобы
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
226 // передать через него результаты работы.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
227 Promise<TNew> medium = new Promise<TNew>();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
228
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
229 AddHandler(new ResultHandlerInfo() {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
230 resultHandler = delegate(T result) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
231 try {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
232 chained(result).Then(
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
233 x => medium.Resolve(x),
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
234 e => medium.Reject(e)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
235 );
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
236 } catch(Exception e) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
237 // если сцепленное действие выдало исключение вместо обещания, то передаем ошибку по цепочке
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
238 medium.Reject(e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
239 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
240 },
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
241 errorHandler = delegate(Exception e) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
242 if (error != null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
243 error(e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
244 // в случае ошибки нужно передать исключение дальше по цепочке
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
245 medium.Reject(e);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
246 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
247 });
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
248
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
249 return medium;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
250 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
251
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
252 public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
253 return Chain (chained, null);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
254 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
255
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
256 /// <summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
257 /// Дожидается отложенного обещания и в случае успеха, возвращает
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
258 /// его, результат, в противном случае бросает исключение.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
259 /// </summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
260 /// <remarks>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
261 /// <para>
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 /// означает, что мы его не дождались, однако все зарегистрированные
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
265 /// обработчики, как были так остались и они будут вызваны, когда
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
266 /// обещание будет выполнено.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
267 /// </para>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
268 /// <para>
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 /// отклонение может привести к непрогнозируемому результату.
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
273 /// </para>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
274 /// </remarks>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
275 /// <param name="timeout">Время ожидания</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
276 /// <returns>Результат выполнения обещания</returns>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
277 public T Join(int timeout) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
278 ManualResetEvent evt = new ManualResetEvent(false);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
279 Anyway(() => evt.Set());
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
280
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
281 if (!evt.WaitOne(timeout, true))
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
282 throw new TimeoutException();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
283
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
284 if (m_error != null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
285 throw new TargetInvocationException( m_error );
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
286 else
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
287 return m_result;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
288 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
289
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
290 public T Join() {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
291 return Join(Timeout.Infinite);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
292 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
293
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
294 /// <summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
295 /// Данный метод последовательно извлекает обработчики обещания и когда
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
296 /// их больше не осталось - ставит состояние "разрешено".
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
297 /// </summary>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
298 /// <param name="handler">Информация об обработчике</param>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
299 /// <returns>Признак того, что еще остались обработчики в очереди</returns>
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
300 bool FetchNextHandler(out ResultHandlerInfo handler) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
301 handler = default(ResultHandlerInfo);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
302
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
303 lock (this) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
304 Debug.Assert(m_state == State.Resolving);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
305
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
306 if (m_handlersChain.Count > 0) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
307 handler = m_handlersChain.First.Value;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
308 m_handlersChain.RemoveFirst();
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
309 return true;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
310 } else {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
311 m_state = State.Resolved;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
312 return false;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
313 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
314 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
315 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
316
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
317 void AddHandler(ResultHandlerInfo handler) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
318 bool invokeRequired = false;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
319
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
320 lock (this) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
321 if (m_state != State.Resolved)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
322 m_handlersChain.AddLast(handler);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
323 else
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
324 invokeRequired = true;
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
325 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
326
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
327 // обработчики не должны блокировать сам объект
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
328 if (invokeRequired)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
329 InvokeHandler(handler);
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 void InvokeHandler(ResultHandlerInfo handler) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
333 if (m_error == null) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
334 try {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
335 if (handler.resultHandler != null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
336 handler.resultHandler(m_result);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
337 } catch { }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
338 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
339
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
340 if (m_error != null) {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
341 try {
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
342 if (handler.errorHandler !=null)
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
343 handler.errorHandler(m_error);
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
344 } catch { }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
345 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
346 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
347
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
348
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
349 }
279591fb4df3 initial commit
user@factory.site.local
parents:
diff changeset
350 }