Mercurial > pub > ImplabNet
annotate Implab/Promise.cs @ 75:4439140706d0 v2
major refactoring, added tasks support
| author | cin |
|---|---|
| date | Wed, 10 Sep 2014 11:17:37 +0400 |
| parents | c4140283575c |
| children | c761fc982e1d |
| rev | line source |
|---|---|
| 2 | 1 using System; |
| 2 using System.Collections.Generic; | |
| 3 using System.Reflection; | |
| 4 using System.Diagnostics; | |
| 5 using System.Threading; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
6 using Implab.Parallels; |
| 2 | 7 |
| 8 namespace Implab { | |
| 9 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
10 public delegate void ErrorHandler(Exception e); |
| 11 | 11 public delegate T ErrorHandler<out T>(Exception e); |
| 10 | 12 public delegate void ResultHandler<in T>(T result); |
| 72 | 13 public delegate TNew ResultMapper<in TSrc,out TNew>(TSrc result); |
| 14 public delegate IPromise<TNew> ChainedOperation<in TSrc,TNew>(TSrc result); | |
| 2 | 15 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
16 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
17 /// Класс для асинхронного получения результатов. Так называемое "обещание". |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
18 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
19 /// <typeparam name="T">Тип получаемого результата</typeparam> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
20 /// <remarks> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
21 /// <para>Сервис при обращении к его методу дает обещаиние о выполнении операции, |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
22 /// клиент получив такое обещание может установить ряд обратных вызово для получения |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
23 /// событий выполнения обещания, тоесть завершения операции и предоставлении результатов.</para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
24 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
25 /// Обещение может быть как выполнено, так и выполнено с ошибкой. Для подписки на |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
26 /// данные события клиент должен использовать методы <c>Then</c>. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
27 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
28 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
29 /// Сервис, в свою очередь, по окончанию выполнения операции (возможно с ошибкой), |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
30 /// использует методы <c>Resolve</c> либо <c>Reject</c> для оповещения клиетна о |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
31 /// выполнении обещания. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
32 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
33 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
34 /// Если сервер успел выполнить обещание еще до того, как клиент на него подписался, |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
35 /// то в момент подписки клиента будут вызваны соответсвующие события в синхронном |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
36 /// режиме и клиент будет оповещен в любом случае. Иначе, обработчики добавляются в |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
37 /// список в порядке подписания и в этом же порядке они будут вызваны при выполнении |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
38 /// обещания. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
39 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
40 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
41 /// Обрабатывая результаты обещания можно преобразовывать результаты либо инициировать |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
42 /// связанные асинхронные операции, которые также возвращают обещания. Для этого следует |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
43 /// использовать соответствующую форму методе <c>Then</c>. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
44 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
45 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
46 /// Также хорошим правилом является то, что <c>Resolve</c> и <c>Reject</c> должен вызывать |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
47 /// только инициатор обещания иначе могут возникнуть противоречия. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
48 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
49 /// </remarks> |
| 25 | 50 public class Promise<T> : IPromise<T> { |
| 2 | 51 |
| 28 | 52 protected struct HandlerDescriptor { |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
53 public ResultHandler<T> resultHandler; |
| 72 | 54 public ErrorHandler<T> errorHandler; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
55 public Action cancellHandler; |
| 72 | 56 public Promise<T> medium; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
57 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
58 public void Resolve(T result) { |
| 72 | 59 if (resultHandler != null) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
60 try { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
61 resultHandler(result); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
62 } catch (Exception e) { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
63 Reject(e); |
| 72 | 64 return; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
65 } |
| 72 | 66 } |
| 67 if (medium != null) | |
| 68 medium.Resolve(result); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
69 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
70 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
71 public void Reject(Exception err) { |
| 72 | 72 if (errorHandler != null) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
73 try { |
| 72 | 74 var res = errorHandler(err); |
| 75 if (medium != null) | |
| 76 medium.Resolve(res); | |
| 77 } catch (TransientPromiseException err2) { | |
| 78 if (medium != null) | |
| 79 medium.Reject(err2.InnerException); | |
| 80 } catch (Exception err2) { | |
| 81 if (medium != null) | |
| 82 medium.Reject(err2); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
83 } |
| 72 | 84 } else if (medium != null) |
| 85 medium.Reject(err); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
86 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
87 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
88 public void Cancel() { |
| 72 | 89 if (cancellHandler != null) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
90 try { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
91 cancellHandler(); |
| 72 | 92 } catch (Exception err) { |
| 93 Reject(err); | |
| 94 return; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
95 } |
| 72 | 96 } |
| 97 if (medium != null) | |
| 98 medium.Cancel(); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
99 } |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
100 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
101 |
| 71 | 102 const int UNRESOLVED_SATE = 0; |
| 103 const int TRANSITIONAL_STATE = 1; | |
| 104 const int SUCCEEDED_STATE = 2; | |
| 105 const int REJECTED_STATE = 3; | |
| 106 const int CANCELLED_STATE = 4; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
107 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
108 readonly bool m_cancellable; |
| 9 | 109 |
| 10 | 110 int m_childrenCount = 0; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
111 int m_state; |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
112 T m_result; |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
113 Exception m_error; |
| 9 | 114 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
115 readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
116 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
117 public Promise() { |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
118 m_cancellable = true; |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
119 } |
| 2 | 120 |
| 66 | 121 public Promise(IPromise parent, bool cancellable) { |
| 7 | 122 m_cancellable = cancellable; |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
123 if (parent != null) |
| 72 | 124 Cancelled(() => { |
| 125 if (parent.IsExclusive) | |
| 126 parent.Cancel(); | |
| 127 }); | |
| 10 | 128 } |
| 2 | 129 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
130 bool BeginTransit() { |
| 71 | 131 return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
132 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
133 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
134 void CompleteTransit(int state) { |
| 71 | 135 if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE)) |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
136 throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
137 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
138 |
| 25 | 139 void WaitTransition() { |
| 71 | 140 while (m_state == TRANSITIONAL_STATE) { |
| 25 | 141 /* noop */ |
| 142 } | |
| 143 } | |
| 144 | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
145 public bool IsResolved { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
146 get { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
147 return m_state > 1; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
148 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
149 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
150 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
151 public bool IsCancelled { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
152 get { |
| 71 | 153 return m_state == CANCELLED_STATE; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
154 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
155 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
156 |
| 29 | 157 public Type PromiseType { |
| 158 get { return typeof(T); } | |
| 159 } | |
| 160 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
161 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
162 /// Выполняет обещание, сообщая об успешном выполнении. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
163 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
164 /// <param name="result">Результат выполнения.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
165 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
166 public void Resolve(T result) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
167 if (BeginTransit()) { |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
168 m_result = result; |
| 71 | 169 CompleteTransit(SUCCEEDED_STATE); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
170 OnStateChanged(); |
| 25 | 171 } else { |
| 172 WaitTransition(); | |
| 71 | 173 if (m_state != CANCELLED_STATE) |
| 25 | 174 throw new InvalidOperationException("The promise is already resolved"); |
| 175 } | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
176 } |
| 2 | 177 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
178 /// <summary> |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
179 /// Выполняет обещание, сообщая об успешном выполнении. Результатом выполнения будет пустое значения. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
180 /// </summary> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
181 /// <remarks> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
182 /// Данный вариант удобен в случаях, когда интересен факт выполнения операции, нежели полученное значение. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
183 /// </remarks> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
184 public void Resolve() { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
185 Resolve(default(T)); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
186 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
187 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
188 /// <summary> |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
189 /// Выполняет обещание, сообщая об ошибке |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
190 /// </summary> |
| 16 | 191 /// <remarks> |
| 192 /// Поскольку обещание должно работать в многопточной среде, при его выполнении сразу несколько потоков | |
| 193 /// могу вернуть ошибку, при этом только первая будет использована в качестве результата, остальные | |
| 194 /// будут проигнорированы. | |
| 195 /// </remarks> | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
196 /// <param name="error">Исключение возникшее при выполнении операции</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
197 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
198 public void Reject(Exception error) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
199 if (BeginTransit()) { |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
200 m_error = error; |
| 71 | 201 CompleteTransit(REJECTED_STATE); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
202 OnStateChanged(); |
| 25 | 203 } else { |
| 204 WaitTransition(); | |
| 71 | 205 if (m_state == SUCCEEDED_STATE) |
| 25 | 206 throw new InvalidOperationException("The promise is already resolved"); |
| 207 } | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
208 } |
| 2 | 209 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
210 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
211 /// Отменяет операцию, если это возможно. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
212 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
213 /// <returns><c>true</c> Операция была отменена, обработчики не будут вызваны.<c>false</c> отмена не возможна, поскольку обещание уже выполнено и обработчики отработали.</returns> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
214 public bool Cancel() { |
| 72 | 215 if (m_cancellable && BeginTransit()) { |
| 71 | 216 CompleteTransit(CANCELLED_STATE); |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
217 OnStateChanged(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
218 return true; |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
219 } |
| 72 | 220 return false; |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
221 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
222 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
223 // сделано для возвращаемого типа void |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
224 protected void InternalCancel() { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
225 Cancel(); |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
226 } |
| 2 | 227 |
| 75 | 228 |
| 229 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel) { | |
| 230 if (success == null && error == null && cancel == null) | |
| 231 return this; | |
| 232 | |
| 233 var medium = new Promise<T>(this, true); | |
| 234 | |
| 235 AddHandler(success, error, cancel, medium); | |
| 236 | |
| 237 return medium; | |
| 238 } | |
| 239 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
240 /// <summary> |
| 11 | 241 /// Adds new handlers to this promise. |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
242 /// </summary> |
| 11 | 243 /// <param name="success">The handler of the successfully completed operation. |
| 244 /// This handler will recieve an operation result as a parameter.</param> | |
| 245 /// <param name="error">Handles an exception that may occur during the operation and returns the value which will be used as the result of the operation.</param> | |
| 246 /// <returns>The new promise chained to this one.</returns> | |
| 26 | 247 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { |
| 11 | 248 if (success == null && error == null) |
| 249 return this; | |
| 250 | |
| 251 var medium = new Promise<T>(this, true); | |
| 252 | |
| 72 | 253 AddHandler(success, error, null, medium); |
| 11 | 254 |
| 255 return medium; | |
| 256 } | |
| 257 | |
| 75 | 258 public IPromise Then(Action success, ErrorHandler error, Action cancel) { |
| 259 return Then( | |
| 260 x => success(), | |
| 261 e => { | |
| 262 error(e); | |
| 263 return default(T); | |
| 264 }, | |
| 265 cancel | |
| 266 ); | |
| 267 } | |
| 268 | |
| 72 | 269 public IPromise Then(Action success, ErrorHandler error) { |
| 270 return Then( | |
| 271 x => success(), | |
| 272 e => { | |
| 273 error(e); | |
| 274 return default(T); | |
| 275 } | |
| 276 ); | |
| 277 } | |
| 278 | |
| 279 public IPromise Then(Action success) { | |
| 280 return Then(x => success()); | |
| 281 } | |
| 282 | |
| 11 | 283 |
| 26 | 284 public IPromise<T> Then(ResultHandler<T> success) { |
| 11 | 285 if (success == null) |
| 286 return this; | |
| 287 | |
| 288 var medium = new Promise<T>(this, true); | |
| 289 | |
| 72 | 290 AddHandler(success, null, null, medium); |
| 11 | 291 |
| 292 return medium; | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
293 } |
| 2 | 294 |
| 75 | 295 public void Last(ResultHandler<T> success, ErrorHandler error, Action cancel) { |
| 296 if (success == null && error == null && cancel == null) | |
| 297 return; | |
| 298 | |
| 299 ErrorHandler<T> errorHandler = null; | |
| 300 if (error != null) | |
| 301 errorHandler = err => { | |
| 302 error(err); | |
| 303 return default(T); | |
| 304 }; | |
| 305 AddHandler(success, errorHandler, cancel, null); | |
| 306 } | |
| 307 | |
| 308 public void Last(ResultHandler<T> success, ErrorHandler error) { | |
| 309 Last(success, error, null); | |
| 310 } | |
| 311 | |
| 312 public void Last(ResultHandler<T> success) { | |
| 313 Last(success, null, null); | |
| 314 } | |
| 315 | |
| 316 public void Last(Action success,ErrorHandler error, Action cancel) { | |
| 317 Last(x => success(), error, cancel); | |
| 318 } | |
| 319 | |
| 320 public void Last(Action success,ErrorHandler error) { | |
| 321 Last(x => success(), error, null); | |
| 322 } | |
| 323 | |
| 324 public void Last(Action success) { | |
| 325 Last(x => success(), null, null); | |
| 326 } | |
| 327 | |
| 72 | 328 public IPromise Error(ErrorHandler error) { |
| 329 if (error == null) | |
| 330 return this; | |
| 331 | |
| 332 var medium = new Promise<T>(this, true); | |
| 333 | |
| 334 AddHandler( | |
| 335 null, | |
| 336 e => { | |
| 337 error(e); | |
| 338 return default(T); | |
| 339 }, | |
| 340 null, | |
| 341 medium | |
| 342 ); | |
| 343 | |
| 344 return medium; | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
345 } |
| 2 | 346 |
| 11 | 347 /// <summary> |
| 348 /// Handles error and allows to keep the promise. | |
| 349 /// </summary> | |
| 350 /// <remarks> | |
| 351 /// If the specified handler throws an exception, this exception will be used to reject the promise. | |
| 352 /// </remarks> | |
| 353 /// <param name="handler">The error handler which returns the result of the promise.</param> | |
| 354 /// <returns>New promise.</returns> | |
| 26 | 355 public IPromise<T> Error(ErrorHandler<T> handler) { |
| 11 | 356 if (handler == null) |
| 357 return this; | |
| 358 | |
| 359 var medium = new Promise<T>(this, true); | |
| 360 | |
| 72 | 361 AddHandler(null, handler, null, medium); |
| 11 | 362 |
| 363 return medium; | |
| 364 } | |
| 365 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
366 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
367 /// Позволяет преобразовать результат выполения операции к новому типу. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
368 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
369 /// <typeparam name="TNew">Новый тип результата.</typeparam> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
370 /// <param name="mapper">Преобразование результата к новому типу.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
371 /// <param name="error">Обработчик ошибки. Данный обработчик получит |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
372 /// исключение возникшее при выполнении операции.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
373 /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns> |
| 75 | 374 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<T> error) { |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
375 if (mapper == null) |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
376 throw new ArgumentNullException("mapper"); |
| 2 | 377 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
378 // создаем прицепленное обещание |
| 72 | 379 var chained = new Promise<TNew>(this, true); |
| 2 | 380 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
381 ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result)); |
| 72 | 382 ErrorHandler<T> errorHandler; |
| 383 if (error != null) | |
| 384 errorHandler = e => { | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
385 try { |
| 72 | 386 return error(e); |
| 387 } catch (Exception e2) { | |
| 388 // в случае ошибки нужно передать исключение дальше по цепочке | |
| 389 chained.Reject(e2); | |
| 390 } | |
| 391 return default(T); | |
| 392 }; | |
| 393 else | |
| 394 errorHandler = e => { | |
| 395 chained.Reject(e); | |
| 396 return default(T); | |
| 397 }; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
398 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
399 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
400 AddHandler( |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
401 resultHandler, |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
402 errorHandler, |
| 72 | 403 chained.InternalCancel, |
| 404 null | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
405 ); |
| 2 | 406 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
407 return chained; |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
408 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
409 |
| 75 | 410 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) { |
| 411 return Then(mapper, null); | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
412 } |
| 2 | 413 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
414 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
415 /// Сцепляет несколько аснхронных операций. Указанная асинхронная операция будет вызвана после |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
416 /// выполнения текущей, а результат текущей операции может быть использован для инициализации |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
417 /// новой операции. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
418 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
419 /// <typeparam name="TNew">Тип результата указанной асинхронной операции.</typeparam> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
420 /// <param name="chained">Асинхронная операция, которая должна будет начаться после выполнения текущей.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
421 /// <param name="error">Обработчик ошибки. Данный обработчик получит |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
422 /// исключение возникшее при выполнении текуещй операции.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
423 /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns> |
| 75 | 424 public IPromise<TNew> Then<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler<T> error) { |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
425 |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
426 // проблема в том, что на момент связывания еще не начата асинхронная операция, поэтому нужно |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
427 // создать посредника, к которому будут подвызяваться следующие обработчики. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
428 // когда будет выполнена реальная асинхронная операция, она обратиться к посреднику, чтобы |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
429 // передать через него результаты работы. |
| 10 | 430 var medium = new Promise<TNew>(this, true); |
| 2 | 431 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
432 ResultHandler<T> resultHandler = delegate(T result) { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
433 if (medium.IsCancelled) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
434 return; |
| 10 | 435 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
436 var promise = chained(result); |
| 10 | 437 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
438 promise.Then( |
| 72 | 439 medium.Resolve, |
| 440 err => { | |
| 441 medium.Reject(err); | |
| 442 throw new TransientPromiseException(err); | |
| 443 } | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
444 ); |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
445 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
446 // notify chained operation that it's not needed anymore |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
447 // порядок вызова Then, Cancelled важен, поскольку от этого |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
448 // зависит IsExclusive |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
449 medium.Cancelled(() => { |
| 72 | 450 if (promise.IsExclusive) |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
451 promise.Cancel(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
452 }); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
453 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
454 // внешняя отмена связанной операции рассматривается как ошибка |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
455 promise.Cancelled(() => medium.Reject(new OperationCanceledException())); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
456 }; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
457 |
| 72 | 458 ErrorHandler<T> errorHandler = delegate(Exception e) { |
| 459 if (error != null) { | |
| 460 try { | |
| 461 return error(e); | |
| 462 } catch (Exception e2) { | |
| 463 medium.Reject(e2); | |
| 464 return default(T); | |
| 465 } | |
| 466 } | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
467 // в случае ошибки нужно передать исключение дальше по цепочке |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
468 medium.Reject(e); |
| 72 | 469 return default(T); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
470 }; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
471 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
472 AddHandler( |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
473 resultHandler, |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
474 errorHandler, |
| 72 | 475 medium.InternalCancel, |
| 476 null | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
477 ); |
| 2 | 478 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
479 return medium; |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
480 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
481 |
| 75 | 482 public IPromise<TNew> Then<TNew>(ChainedOperation<T, TNew> chained) { |
| 483 return Then(chained, null); | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
484 } |
| 2 | 485 |
| 26 | 486 public IPromise<T> Cancelled(Action handler) { |
| 74 | 487 var medium = new Promise<T>(this, true); |
| 488 AddHandler(null, null, handler, medium); | |
| 489 return medium; | |
| 10 | 490 } |
| 491 | |
| 25 | 492 /// <summary> |
| 493 /// Adds the specified handler for all cases (success, error, cancel) | |
| 494 /// </summary> | |
| 495 /// <param name="handler">The handler that will be called anyway</param> | |
| 496 /// <returns>self</returns> | |
| 26 | 497 public IPromise<T> Finally(Action handler) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
498 if (handler == null) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
499 throw new ArgumentNullException("handler"); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
500 AddHandler( |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
501 x => handler(), |
| 72 | 502 e => { |
| 503 handler(); | |
| 504 throw new TransientPromiseException(e); | |
| 505 }, | |
| 506 handler, | |
| 507 null | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
508 ); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
509 return this; |
| 10 | 510 } |
| 511 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
512 /// <summary> |
| 29 | 513 /// Преобразует результат обещания к нужному типу |
| 514 /// </summary> | |
| 515 /// <typeparam name="T2"></typeparam> | |
| 516 /// <returns></returns> | |
| 517 public IPromise<T2> Cast<T2>() { | |
| 75 | 518 return Then(x => (T2)(object)x, null); |
| 29 | 519 } |
| 520 | |
| 521 /// <summary> | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
522 /// Дожидается отложенного обещания и в случае успеха, возвращает |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
523 /// его, результат, в противном случае бросает исключение. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
524 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
525 /// <remarks> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
526 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
527 /// Если ожидание обещания было прервано по таймауту, это не значит, |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
528 /// что обещание было отменено или что-то в этом роде, это только |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
529 /// означает, что мы его не дождались, однако все зарегистрированные |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
530 /// обработчики, как были так остались и они будут вызваны, когда |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
531 /// обещание будет выполнено. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
532 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
533 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
534 /// Такое поведение вполне оправдано поскольку таймаут может истечь |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
535 /// в тот момент, когда началась обработка цепочки обработчиков, и |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
536 /// к тому же текущее обещание может стоять в цепочке обещаний и его |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
537 /// отклонение может привести к непрогнозируемому результату. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
538 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
539 /// </remarks> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
540 /// <param name="timeout">Время ожидания</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
541 /// <returns>Результат выполнения обещания</returns> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
542 public T Join(int timeout) { |
| 10 | 543 var evt = new ManualResetEvent(false); |
| 75 | 544 Finally(() => evt.Set()); |
| 2 | 545 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
546 if (!evt.WaitOne(timeout, true)) |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
547 throw new TimeoutException(); |
| 2 | 548 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
549 switch (m_state) { |
| 71 | 550 case SUCCEEDED_STATE: |
| 10 | 551 return m_result; |
| 71 | 552 case CANCELLED_STATE: |
| 10 | 553 throw new OperationCanceledException(); |
| 71 | 554 case REJECTED_STATE: |
| 10 | 555 throw new TargetInvocationException(m_error); |
| 556 default: | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
557 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); |
| 10 | 558 } |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
559 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
560 |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
561 public T Join() { |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
562 return Join(Timeout.Infinite); |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
563 } |
| 2 | 564 |
| 72 | 565 void AddHandler(ResultHandler<T> success, ErrorHandler<T> error, Action cancel, Promise<T> medium) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
566 if (success != null || error != null) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
567 Interlocked.Increment(ref m_childrenCount); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
568 |
| 72 | 569 var handler = new HandlerDescriptor { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
570 resultHandler = success, |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
571 errorHandler = error, |
| 72 | 572 cancellHandler = cancel, |
| 573 medium = medium | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
574 }; |
| 2 | 575 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
576 bool queued; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
577 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
578 if (!IsResolved) { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
579 m_handlers.Enqueue(handler); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
580 queued = true; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
581 } else { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
582 // the promise is in resolved state, just invoke the handled with minimum overhead |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
583 queued = false; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
584 InvokeHandler(handler); |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
585 } |
| 2 | 586 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
587 if (queued && IsResolved && m_handlers.TryDequeue(out handler)) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
588 // if the promise have been resolved while we was adding handler to the queue |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
589 // we can't guarantee that someone is still processing it |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
590 // therefore we will fetch a handler from the queue and execute it |
| 27 | 591 // note that fetched handler may be not the one that we have added |
| 592 // even we can fetch no handlers at all :) | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
593 InvokeHandler(handler); |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
594 } |
| 2 | 595 |
| 27 | 596 protected virtual void InvokeHandler(HandlerDescriptor handler) { |
| 10 | 597 switch (m_state) { |
| 71 | 598 case SUCCEEDED_STATE: |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
599 handler.Resolve(m_result); |
| 10 | 600 break; |
| 71 | 601 case REJECTED_STATE: |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
602 handler.Reject(m_error); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
603 break; |
| 71 | 604 case CANCELLED_STATE: |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
605 handler.Cancel(); |
| 10 | 606 break; |
| 607 default: | |
| 608 // do nothing | |
| 609 return; | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
610 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
611 } |
| 2 | 612 |
| 65 | 613 void OnStateChanged() { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
614 HandlerDescriptor handler; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
615 while (m_handlers.TryDequeue(out handler)) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
616 InvokeHandler(handler); |
| 11 | 617 } |
| 618 | |
| 9 | 619 public bool IsExclusive { |
| 620 get { | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
621 return m_childrenCount <= 1; |
| 9 | 622 } |
| 623 } | |
| 624 | |
| 25 | 625 /// <summary> |
| 626 /// Объединяет несколько обещаний в одно, результатом которого является массив результатов других обещаний. | |
| 627 /// Если хотябы одно из переданных обещаний не будет выполнено, то новое обещение тоже не будет выполнено. | |
| 628 /// При отмене нового обещания, переданные обещания также будут отменены, если никто больше на них не подписан. | |
| 629 /// </summary> | |
| 630 /// <param name="promises">Список обещаний. Если список пустой, то результирующее обещание возвращается уже выполненным.</param> | |
| 631 /// <returns>Обещание объединяющее в себе результат переданных обещаний.</returns> | |
| 632 /// <exception cref="ArgumentNullException"><paramref name="promises"/> не может быть null</exception> | |
| 30 | 633 public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) { |
| 25 | 634 if (promises == null) |
| 635 throw new ArgumentNullException(); | |
| 636 | |
| 637 // создаем аккумулятор для результатов и результирующее обещание | |
| 638 var result = new T[promises.Count]; | |
| 639 var promise = new Promise<T[]>(); | |
| 640 | |
| 641 // special case | |
| 642 if (promises.Count == 0) { | |
| 643 promise.Resolve(result); | |
| 644 return promise; | |
| 645 } | |
| 646 | |
| 647 int pending = promises.Count; | |
| 648 | |
| 649 for (int i = 0; i < promises.Count; i++) { | |
| 650 var dest = i; | |
| 651 | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
652 if (promises[i] != null) { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
653 promises[i].Then( |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
654 x => { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
655 result[dest] = x; |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
656 if (Interlocked.Decrement(ref pending) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
657 promise.Resolve(result); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
658 }, |
| 72 | 659 e => { |
| 660 promise.Reject(e); | |
| 661 return default(T); | |
| 662 } | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
663 ); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
664 } else { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
665 if (Interlocked.Decrement(ref pending) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
666 promise.Resolve(result); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
667 } |
| 25 | 668 } |
| 669 | |
| 670 promise.Cancelled( | |
| 671 () => { | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
672 foreach (var d in promises) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
673 if (d != null && d.IsExclusive) |
| 25 | 674 d.Cancel(); |
| 675 } | |
| 676 ); | |
| 677 | |
| 678 return promise; | |
| 679 } | |
| 680 | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
681 /// <summary> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
682 /// Объединяет несколько обещаний в одно. Результирующее обещание будет выполнено при |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
683 /// выполнении всех указанных обещаний. При этом возвращаемые значения первичных обещаний |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
684 /// игнорируются. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
685 /// </summary> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
686 /// <param name="promises">Коллекция первичных обещаний, которые будут объеденены в одно.</param> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
687 /// <returns>Новое обещание, объединяющее в себе переданные.</returns> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
688 /// <remarks> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
689 /// Если в коллекции встречаюься <c>null</c>, то они воспринимаются как выполненные обещания. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
690 /// </remarks> |
| 66 | 691 public static IPromise CreateComposite(ICollection<IPromise> promises) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
692 if (promises == null) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
693 throw new ArgumentNullException(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
694 if (promises.Count == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
695 return Promise<object>.ResultToPromise(null); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
696 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
697 int countdown = promises.Count; |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
698 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
699 var result = new Promise<object>(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
700 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
701 foreach (var d in promises) { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
702 if (d == null) { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
703 if (Interlocked.Decrement(ref countdown) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
704 result.Resolve(null); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
705 } else { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
706 d.Then(() => { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
707 if (Interlocked.Decrement(ref countdown) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
708 result.Resolve(null); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
709 }); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
710 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
711 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
712 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
713 result.Cancelled(() => { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
714 foreach (var d in promises) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
715 if (d != null && d.IsExclusive) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
716 d.Cancel(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
717 }); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
718 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
719 return result; |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
720 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
721 |
| 25 | 722 public static Promise<T> ResultToPromise(T result) { |
| 723 var p = new Promise<T>(); | |
| 724 p.Resolve(result); | |
| 725 return p; | |
| 726 } | |
| 727 | |
| 728 public static Promise<T> ExceptionToPromise(Exception error) { | |
| 729 if (error == null) | |
| 730 throw new ArgumentNullException(); | |
| 731 | |
| 732 var p = new Promise<T>(); | |
| 733 p.Reject(error); | |
| 734 return p; | |
| 735 } | |
| 736 | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
737 #region IPromiseBase explicit implementation |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
738 |
| 66 | 739 IPromise IPromise.Error(ErrorHandler error) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
740 return Error(error); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
741 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
742 |
| 66 | 743 IPromise IPromise.Finally(Action handler) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
744 return Finally(handler); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
745 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
746 |
| 66 | 747 IPromise IPromise.Cancelled(Action handler) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
748 return Cancelled(handler); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
749 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
750 |
| 66 | 751 void IPromise.Join() { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
752 Join(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
753 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
754 |
| 66 | 755 void IPromise.Join(int timeout) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
756 Join(timeout); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
757 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
758 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
759 #endregion |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
760 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
761 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
762 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
763 } |
| 2 | 764 } |
