Mercurial > pub > ImplabNet
annotate Implab/Promise.cs @ 105:4d308952fd5e v2
minor fixes
| author | cin |
|---|---|
| date | Mon, 10 Nov 2014 10:17:54 +0300 |
| parents | 5f10d54b45df |
| children | d4e38929ce36 |
| rev | line source |
|---|---|
| 2 | 1 using System; |
| 2 using System.Collections.Generic; | |
| 3 using System.Reflection; | |
| 4 using System.Threading; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
5 using Implab.Parallels; |
| 2 | 6 |
| 7 namespace Implab { | |
| 8 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
9 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
10 /// Класс для асинхронного получения результатов. Так называемое "обещание". |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
11 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
12 /// <typeparam name="T">Тип получаемого результата</typeparam> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
13 /// <remarks> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
14 /// <para>Сервис при обращении к его методу дает обещаиние о выполнении операции, |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
15 /// клиент получив такое обещание может установить ряд обратных вызово для получения |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
16 /// событий выполнения обещания, тоесть завершения операции и предоставлении результатов.</para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
17 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
18 /// Обещение может быть как выполнено, так и выполнено с ошибкой. Для подписки на |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
19 /// данные события клиент должен использовать методы <c>Then</c>. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
20 /// </para> |
|
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 /// использует методы <c>Resolve</c> либо <c>Reject</c> для оповещения клиетна о |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
24 /// выполнении обещания. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
25 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
26 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
27 /// Если сервер успел выполнить обещание еще до того, как клиент на него подписался, |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
28 /// то в момент подписки клиента будут вызваны соответсвующие события в синхронном |
|
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 /// список в порядке подписания и в этом же порядке они будут вызваны при выполнении |
|
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 /// использовать соответствующую форму методе <c>Then</c>. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
37 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
38 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
39 /// Также хорошим правилом является то, что <c>Resolve</c> и <c>Reject</c> должен вызывать |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
40 /// только инициатор обещания иначе могут возникнуть противоречия. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
41 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
42 /// </remarks> |
| 25 | 43 public class Promise<T> : IPromise<T> { |
| 2 | 44 |
| 28 | 45 protected struct HandlerDescriptor { |
| 101 | 46 public Action<T> resultHandler; |
| 47 public Func<Exception,T> errorHandler; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
48 public Action cancellHandler; |
| 72 | 49 public Promise<T> medium; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
50 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
51 public void Resolve(T result) { |
| 72 | 52 if (resultHandler != null) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
53 try { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
54 resultHandler(result); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
55 } catch (Exception e) { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
56 Reject(e); |
| 72 | 57 return; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
58 } |
| 72 | 59 } |
| 60 if (medium != null) | |
| 61 medium.Resolve(result); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
62 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
63 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
64 public void Reject(Exception err) { |
| 72 | 65 if (errorHandler != null) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
66 try { |
| 72 | 67 var res = errorHandler(err); |
| 68 if (medium != null) | |
| 69 medium.Resolve(res); | |
| 99 | 70 /*} catch (TransientPromiseException err2) { |
| 72 | 71 if (medium != null) |
| 99 | 72 medium.Reject(err2.InnerException);*/ |
| 72 | 73 } catch (Exception err2) { |
| 74 if (medium != null) | |
| 75 medium.Reject(err2); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
76 } |
| 72 | 77 } else if (medium != null) |
| 78 medium.Reject(err); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
79 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
80 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
81 public void Cancel() { |
| 72 | 82 if (cancellHandler != null) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
83 try { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
84 cancellHandler(); |
| 72 | 85 } catch (Exception err) { |
| 86 Reject(err); | |
| 87 return; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
88 } |
| 72 | 89 } |
| 90 if (medium != null) | |
| 91 medium.Cancel(); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
92 } |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
93 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
94 |
| 71 | 95 const int UNRESOLVED_SATE = 0; |
| 96 const int TRANSITIONAL_STATE = 1; | |
| 97 const int SUCCEEDED_STATE = 2; | |
| 98 const int REJECTED_STATE = 3; | |
| 99 const int CANCELLED_STATE = 4; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
100 |
| 101 | 101 int m_childrenCount; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
102 int m_state; |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
103 T m_result; |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
104 Exception m_error; |
| 9 | 105 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
106 readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
107 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
108 public Promise() { |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
109 } |
| 2 | 110 |
| 101 | 111 public Promise(IPromise parent) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
112 if (parent != null) |
| 76 | 113 AddHandler( |
| 114 null, | |
| 115 null, | |
| 116 () => { | |
| 117 if (parent.IsExclusive) | |
| 118 parent.Cancel(); | |
| 119 }, | |
| 104 | 120 null, |
| 121 false | |
| 76 | 122 ); |
| 10 | 123 } |
| 2 | 124 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
125 bool BeginTransit() { |
| 71 | 126 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
|
127 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
128 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
129 void CompleteTransit(int state) { |
| 71 | 130 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
|
131 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
|
132 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
133 |
| 25 | 134 void WaitTransition() { |
| 71 | 135 while (m_state == TRANSITIONAL_STATE) { |
| 80 | 136 Thread.MemoryBarrier(); |
| 25 | 137 } |
| 138 } | |
| 139 | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
140 public bool IsResolved { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
141 get { |
| 80 | 142 Thread.MemoryBarrier(); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
143 return m_state > 1; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
144 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
145 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
146 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
147 public bool IsCancelled { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
148 get { |
| 80 | 149 Thread.MemoryBarrier(); |
| 71 | 150 return m_state == CANCELLED_STATE; |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
151 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
152 } |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
153 |
| 29 | 154 public Type PromiseType { |
| 155 get { return typeof(T); } | |
| 156 } | |
| 157 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
158 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
159 /// Выполняет обещание, сообщая об успешном выполнении. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
160 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
161 /// <param name="result">Результат выполнения.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
162 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
163 public void Resolve(T result) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
164 if (BeginTransit()) { |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
165 m_result = result; |
| 71 | 166 CompleteTransit(SUCCEEDED_STATE); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
167 OnStateChanged(); |
| 25 | 168 } else { |
| 169 WaitTransition(); | |
| 71 | 170 if (m_state != CANCELLED_STATE) |
| 25 | 171 throw new InvalidOperationException("The promise is already resolved"); |
| 172 } | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
173 } |
| 2 | 174 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
175 /// <summary> |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
176 /// Выполняет обещание, сообщая об успешном выполнении. Результатом выполнения будет пустое значения. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
177 /// </summary> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
178 /// <remarks> |
|
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 /// </remarks> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
181 public void Resolve() { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
182 Resolve(default(T)); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
183 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
184 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
185 /// <summary> |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
186 /// Выполняет обещание, сообщая об ошибке |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
187 /// </summary> |
| 16 | 188 /// <remarks> |
| 189 /// Поскольку обещание должно работать в многопточной среде, при его выполнении сразу несколько потоков | |
| 190 /// могу вернуть ошибку, при этом только первая будет использована в качестве результата, остальные | |
| 191 /// будут проигнорированы. | |
| 192 /// </remarks> | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
193 /// <param name="error">Исключение возникшее при выполнении операции</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
194 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
195 public void Reject(Exception error) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
196 if (BeginTransit()) { |
| 99 | 197 m_error = error is TransientPromiseException ? error.InnerException : error; |
| 71 | 198 CompleteTransit(REJECTED_STATE); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
199 OnStateChanged(); |
| 25 | 200 } else { |
| 201 WaitTransition(); | |
| 71 | 202 if (m_state == SUCCEEDED_STATE) |
| 25 | 203 throw new InvalidOperationException("The promise is already resolved"); |
| 204 } | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
205 } |
| 2 | 206 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
207 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
208 /// Отменяет операцию, если это возможно. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
209 /// </summary> |
| 76 | 210 /// <remarks>Для определения была ли операция отменена следует использовать свойство <see cref="IsCancelled"/>.</remarks> |
| 211 public void Cancel() { | |
| 101 | 212 if (BeginTransit()) { |
| 71 | 213 CompleteTransit(CANCELLED_STATE); |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
214 OnStateChanged(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
215 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
216 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
217 |
| 101 | 218 public IPromise<T> Then(Action<T> success, Func<Exception,T> error, Action cancel) { |
| 75 | 219 if (success == null && error == null && cancel == null) |
| 220 return this; | |
| 221 | |
| 101 | 222 var medium = new Promise<T>(this); |
| 75 | 223 |
| 104 | 224 AddHandler(success, error, cancel, medium, true); |
| 75 | 225 |
| 226 return medium; | |
| 227 } | |
| 228 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
229 /// <summary> |
| 11 | 230 /// Adds new handlers to this promise. |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
231 /// </summary> |
| 11 | 232 /// <param name="success">The handler of the successfully completed operation. |
| 233 /// This handler will recieve an operation result as a parameter.</param> | |
| 234 /// <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> | |
| 235 /// <returns>The new promise chained to this one.</returns> | |
| 101 | 236 public IPromise<T> Then(Action<T> success, Func<Exception,T> error) { |
| 11 | 237 if (success == null && error == null) |
| 238 return this; | |
| 239 | |
| 101 | 240 var medium = new Promise<T>(this); |
| 11 | 241 |
| 104 | 242 AddHandler(success, error, null, medium, true); |
| 11 | 243 |
| 244 return medium; | |
| 245 } | |
| 246 | |
| 76 | 247 |
| 72 | 248 |
| 11 | 249 |
| 101 | 250 public IPromise<T> Then(Action<T> success) { |
| 11 | 251 if (success == null) |
| 252 return this; | |
| 253 | |
| 101 | 254 var medium = new Promise<T>(this); |
| 11 | 255 |
| 104 | 256 AddHandler(success, null, null, medium, true); |
| 11 | 257 |
| 258 return medium; | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
259 } |
| 2 | 260 |
| 76 | 261 /// <summary> |
| 262 /// Последний обработчик в цепочки обещаний. | |
| 263 /// </summary> | |
| 264 /// <param name="success"></param> | |
| 265 /// <param name="error"></param> | |
| 266 /// <param name="cancel"></param> | |
| 267 /// <remarks> | |
| 268 /// <para> | |
| 269 /// Данный метод не создает связанного с текущим обещания и предназначен для окончания | |
| 270 /// фсинхронной цепочки. | |
| 271 /// </para> | |
| 272 /// <para> | |
| 273 /// Если данный метод вызвать несколько раз, либо добавить другие обработчики, то цепочка | |
| 274 /// не будет одиночной <see cref="IsExclusive"/> и, как следствие, будет невозможна отмена | |
| 275 /// всей цепи обещаний снизу (с самого последнего обещания). | |
| 276 /// </para> | |
| 277 /// </remarks> | |
| 104 | 278 public void On(Action<T> success, Action<Exception> error, Action cancel) { |
| 75 | 279 if (success == null && error == null && cancel == null) |
| 280 return; | |
| 281 | |
| 101 | 282 Func<Exception,T> errorHandler = null; |
| 75 | 283 if (error != null) |
| 284 errorHandler = err => { | |
| 285 error(err); | |
| 286 return default(T); | |
| 287 }; | |
| 104 | 288 AddHandler(success, errorHandler, cancel, null, false); |
| 289 } | |
| 290 | |
| 291 public void On(Action<T> success, Action<Exception> error) { | |
| 292 On(success, error, null); | |
| 293 } | |
| 294 | |
| 295 public void On(Action<T> success) { | |
| 296 On(success, null, null); | |
| 75 | 297 } |
| 298 | |
| 104 | 299 public void On(Action handler, PromiseEventType events) { |
| 300 Safe.ArgumentNotNull(handler, "handler"); | |
| 75 | 301 |
| 104 | 302 Action<T> success = events.HasFlag(PromiseEventType.Success) ? new Action<T>(x => handler()) : null; |
| 303 Func<Exception,T> error = events.HasFlag(PromiseEventType.Error) ? new Func<Exception,T>(e => { | |
| 304 handler(); | |
| 305 return default(T); | |
| 306 }) : null; | |
| 307 Action cancel = events.HasFlag(PromiseEventType.Cancelled) ? handler : null; | |
| 308 | |
| 309 AddHandler(success, error, cancel, null, false); | |
| 75 | 310 } |
| 311 | |
| 101 | 312 public IPromise Error(Action<Exception> error) { |
| 72 | 313 if (error == null) |
| 314 return this; | |
| 315 | |
| 101 | 316 var medium = new Promise<T>(this); |
| 72 | 317 |
| 318 AddHandler( | |
| 319 null, | |
| 320 e => { | |
| 321 error(e); | |
| 322 return default(T); | |
| 323 }, | |
| 324 null, | |
| 104 | 325 medium, |
| 326 true | |
| 72 | 327 ); |
| 328 | |
| 329 return medium; | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
330 } |
| 2 | 331 |
| 11 | 332 /// <summary> |
| 333 /// Handles error and allows to keep the promise. | |
| 334 /// </summary> | |
| 335 /// <remarks> | |
| 336 /// If the specified handler throws an exception, this exception will be used to reject the promise. | |
| 337 /// </remarks> | |
| 338 /// <param name="handler">The error handler which returns the result of the promise.</param> | |
| 339 /// <returns>New promise.</returns> | |
| 101 | 340 public IPromise<T> Error(Func<Exception,T> handler) { |
| 11 | 341 if (handler == null) |
| 342 return this; | |
| 343 | |
| 101 | 344 var medium = new Promise<T>(this); |
| 11 | 345 |
| 104 | 346 AddHandler(null, handler, null, medium, true); |
| 11 | 347 |
| 348 return medium; | |
| 349 } | |
| 350 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
351 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
352 /// Позволяет преобразовать результат выполения операции к новому типу. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
353 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
354 /// <typeparam name="TNew">Новый тип результата.</typeparam> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
355 /// <param name="mapper">Преобразование результата к новому типу.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
356 /// <param name="error">Обработчик ошибки. Данный обработчик получит |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
357 /// исключение возникшее при выполнении операции.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
358 /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns> |
| 96 | 359 /// <param name = "cancel"></param> |
| 101 | 360 public IPromise<TNew> Then<TNew>(Func<T, TNew> mapper, Func<Exception,TNew> error, Action cancel) { |
| 76 | 361 Safe.ArgumentNotNull(mapper, "mapper"); |
| 362 | |
| 363 // создаем прицепленное обещание | |
| 101 | 364 var medium = new Promise<TNew>(this); |
| 2 | 365 |
| 101 | 366 Action<T> resultHandler = result => medium.Resolve(mapper(result)); |
| 367 Func<Exception,T> errorHandler; | |
| 72 | 368 if (error != null) |
| 369 errorHandler = e => { | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
370 try { |
| 76 | 371 medium.Resolve(error(e)); |
| 72 | 372 } catch (Exception e2) { |
| 373 // в случае ошибки нужно передать исключение дальше по цепочке | |
| 76 | 374 medium.Reject(e2); |
| 72 | 375 } |
| 376 return default(T); | |
| 377 }; | |
| 378 else | |
| 379 errorHandler = e => { | |
| 76 | 380 medium.Reject(e); |
| 72 | 381 return default(T); |
| 382 }; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
383 |
| 76 | 384 Action cancelHandler; |
| 385 if (cancel != null) | |
| 386 cancelHandler = () => { | |
| 387 cancel(); | |
| 388 medium.Cancel(); | |
| 389 }; | |
| 390 else | |
| 391 cancelHandler = medium.Cancel; | |
| 392 | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
393 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
394 AddHandler( |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
395 resultHandler, |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
396 errorHandler, |
| 76 | 397 cancelHandler, |
| 104 | 398 null, |
| 399 true | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
400 ); |
| 2 | 401 |
| 76 | 402 return medium; |
| 403 } | |
| 404 | |
| 101 | 405 public IPromise<TNew> Then<TNew>(Func<T, TNew> mapper, Func<Exception,TNew> error) { |
| 76 | 406 return Then(mapper, error, null); |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
407 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
408 |
| 101 | 409 public IPromise<TNew> Then<TNew>(Func<T, TNew> mapper) { |
| 76 | 410 return Then(mapper, null, null); |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
411 } |
| 2 | 412 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
413 /// <summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
414 /// Сцепляет несколько аснхронных операций. Указанная асинхронная операция будет вызвана после |
|
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 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
418 /// <typeparam name="TNew">Тип результата указанной асинхронной операции.</typeparam> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
419 /// <param name="chained">Асинхронная операция, которая должна будет начаться после выполнения текущей.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
420 /// <param name="error">Обработчик ошибки. Данный обработчик получит |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
421 /// исключение возникшее при выполнении текуещй операции.</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
422 /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns> |
| 96 | 423 /// <param name = "cancel"></param> |
| 101 | 424 public IPromise<TNew> Chain<TNew>(Func<T, IPromise<TNew>> chained, Func<Exception,IPromise<TNew>> error, Action cancel) { |
| 76 | 425 |
| 426 Safe.ArgumentNotNull(chained, "chained"); | |
|
6
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 // создать посредника, к которому будут подвызяваться следующие обработчики. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
430 // когда будет выполнена реальная асинхронная операция, она обратиться к посреднику, чтобы |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
431 // передать через него результаты работы. |
| 101 | 432 var medium = new Promise<TNew>(this); |
| 2 | 433 |
| 101 | 434 Action<T> resultHandler = delegate(T result) { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
435 if (medium.IsCancelled) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
436 return; |
| 10 | 437 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
438 var promise = chained(result); |
| 10 | 439 |
| 104 | 440 promise.On( |
| 72 | 441 medium.Resolve, |
| 76 | 442 medium.Reject, |
| 443 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка | |
|
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 |
| 104 | 449 medium.On( |
| 101 | 450 null, |
| 451 null, | |
| 452 () => { | |
| 453 if (promise.IsExclusive) | |
| 454 promise.Cancel(); | |
| 455 } | |
| 456 ); | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
457 }; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
458 |
| 101 | 459 Func<Exception,T> errorHandler; |
| 76 | 460 |
| 461 if (error != null) | |
| 462 errorHandler = delegate(Exception e) { | |
| 72 | 463 try { |
| 76 | 464 var promise = error(e); |
| 465 | |
| 104 | 466 promise.On( |
| 76 | 467 medium.Resolve, |
| 468 medium.Reject, | |
| 469 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка | |
| 470 ); | |
| 471 | |
| 472 // notify chained operation that it's not needed anymore | |
| 473 // порядок вызова Then, Cancelled важен, поскольку от этого | |
| 474 // зависит IsExclusive | |
| 475 medium.Cancelled(() => { | |
| 476 if (promise.IsExclusive) | |
| 477 promise.Cancel(); | |
| 478 }); | |
| 72 | 479 } catch (Exception e2) { |
| 480 medium.Reject(e2); | |
| 481 } | |
| 76 | 482 return default(T); |
| 483 }; | |
| 484 else | |
| 485 errorHandler = err => { | |
| 486 medium.Reject(err); | |
| 487 return default(T); | |
| 488 }; | |
| 489 | |
| 490 | |
| 491 Action cancelHandler; | |
| 492 if (cancel != null) | |
| 493 cancelHandler = () => { | |
| 494 if (cancel != null) | |
| 495 cancel(); | |
| 496 medium.Cancel(); | |
| 497 }; | |
| 498 else | |
| 499 cancelHandler = medium.Cancel; | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
500 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
501 AddHandler( |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
502 resultHandler, |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
503 errorHandler, |
| 76 | 504 cancelHandler, |
| 104 | 505 null, |
| 506 true | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
507 ); |
| 2 | 508 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
509 return medium; |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
510 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
511 |
| 101 | 512 public IPromise<TNew> Chain<TNew>(Func<T, IPromise<TNew>> chained, Func<Exception,IPromise<TNew>> error) { |
| 76 | 513 return Chain(chained, error, null); |
| 514 } | |
| 515 | |
| 101 | 516 public IPromise<TNew> Chain<TNew>(Func<T, IPromise<TNew>> chained) { |
| 76 | 517 return Chain(chained, null, null); |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
518 } |
| 2 | 519 |
| 26 | 520 public IPromise<T> Cancelled(Action handler) { |
| 101 | 521 var medium = new Promise<T>(this); |
| 104 | 522 AddHandler(null, null, handler, medium, false); |
| 74 | 523 return medium; |
| 10 | 524 } |
| 525 | |
| 25 | 526 /// <summary> |
| 527 /// Adds the specified handler for all cases (success, error, cancel) | |
| 528 /// </summary> | |
| 529 /// <param name="handler">The handler that will be called anyway</param> | |
| 530 /// <returns>self</returns> | |
| 76 | 531 public IPromise<T> Anyway(Action handler) { |
| 532 Safe.ArgumentNotNull(handler, "handler"); | |
| 104 | 533 |
| 534 var medium = new Promise<T>(this); | |
| 535 | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
536 AddHandler( |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
537 x => handler(), |
| 72 | 538 e => { |
| 539 handler(); | |
| 540 throw new TransientPromiseException(e); | |
| 541 }, | |
| 542 handler, | |
| 104 | 543 medium, |
| 544 true | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
545 ); |
| 104 | 546 |
| 547 return medium; | |
| 10 | 548 } |
| 549 | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
550 /// <summary> |
| 29 | 551 /// Преобразует результат обещания к нужному типу |
| 552 /// </summary> | |
| 553 /// <typeparam name="T2"></typeparam> | |
| 554 /// <returns></returns> | |
| 555 public IPromise<T2> Cast<T2>() { | |
| 75 | 556 return Then(x => (T2)(object)x, null); |
| 29 | 557 } |
| 558 | |
| 559 /// <summary> | |
|
6
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 /// его, результат, в противном случае бросает исключение. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
562 /// </summary> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
563 /// <remarks> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
564 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
565 /// Если ожидание обещания было прервано по таймауту, это не значит, |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
566 /// что обещание было отменено или что-то в этом роде, это только |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
567 /// означает, что мы его не дождались, однако все зарегистрированные |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
568 /// обработчики, как были так остались и они будут вызваны, когда |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
569 /// обещание будет выполнено. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
570 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
571 /// <para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
572 /// Такое поведение вполне оправдано поскольку таймаут может истечь |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
573 /// в тот момент, когда началась обработка цепочки обработчиков, и |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
574 /// к тому же текущее обещание может стоять в цепочке обещаний и его |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
575 /// отклонение может привести к непрогнозируемому результату. |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
576 /// </para> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
577 /// </remarks> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
578 /// <param name="timeout">Время ожидания</param> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
579 /// <returns>Результат выполнения обещания</returns> |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
580 public T Join(int timeout) { |
| 10 | 581 var evt = new ManualResetEvent(false); |
| 76 | 582 Anyway(() => evt.Set()); |
| 2 | 583 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
584 if (!evt.WaitOne(timeout, true)) |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
585 throw new TimeoutException(); |
| 2 | 586 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
587 switch (m_state) { |
| 71 | 588 case SUCCEEDED_STATE: |
| 10 | 589 return m_result; |
| 71 | 590 case CANCELLED_STATE: |
| 10 | 591 throw new OperationCanceledException(); |
| 71 | 592 case REJECTED_STATE: |
| 10 | 593 throw new TargetInvocationException(m_error); |
| 594 default: | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
595 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); |
| 10 | 596 } |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
597 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
598 |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
599 public T Join() { |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
600 return Join(Timeout.Infinite); |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
601 } |
| 2 | 602 |
| 104 | 603 void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium, bool inc) { |
| 604 if (inc) | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
605 Interlocked.Increment(ref m_childrenCount); |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
606 |
| 72 | 607 var handler = new HandlerDescriptor { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
608 resultHandler = success, |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
609 errorHandler = error, |
| 72 | 610 cancellHandler = cancel, |
| 611 medium = medium | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
612 }; |
| 2 | 613 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
614 bool queued; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
615 |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
616 if (!IsResolved) { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
617 m_handlers.Enqueue(handler); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
618 queued = true; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
619 } else { |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
620 // 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
|
621 queued = false; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
622 InvokeHandler(handler); |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
623 } |
| 2 | 624 |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
625 if (queued && IsResolved && m_handlers.TryDequeue(out handler)) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
626 // 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
|
627 // 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
|
628 // therefore we will fetch a handler from the queue and execute it |
| 27 | 629 // note that fetched handler may be not the one that we have added |
| 630 // even we can fetch no handlers at all :) | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
631 InvokeHandler(handler); |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
632 } |
| 2 | 633 |
| 27 | 634 protected virtual void InvokeHandler(HandlerDescriptor handler) { |
| 10 | 635 switch (m_state) { |
| 71 | 636 case SUCCEEDED_STATE: |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
637 handler.Resolve(m_result); |
| 10 | 638 break; |
| 71 | 639 case REJECTED_STATE: |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
640 handler.Reject(m_error); |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
641 break; |
| 71 | 642 case CANCELLED_STATE: |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
643 handler.Cancel(); |
| 10 | 644 break; |
| 645 default: | |
| 646 // do nothing | |
| 647 return; | |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
648 } |
|
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
649 } |
| 2 | 650 |
| 65 | 651 void OnStateChanged() { |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
652 HandlerDescriptor handler; |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
653 while (m_handlers.TryDequeue(out handler)) |
|
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
654 InvokeHandler(handler); |
| 11 | 655 } |
| 656 | |
| 9 | 657 public bool IsExclusive { |
| 658 get { | |
|
19
e3935fdf59a2
Promise is rewritten to use interlocked operations instead of locks
cin
parents:
16
diff
changeset
|
659 return m_childrenCount <= 1; |
| 9 | 660 } |
| 661 } | |
| 662 | |
| 25 | 663 /// <summary> |
| 664 /// Объединяет несколько обещаний в одно, результатом которого является массив результатов других обещаний. | |
| 665 /// Если хотябы одно из переданных обещаний не будет выполнено, то новое обещение тоже не будет выполнено. | |
| 666 /// При отмене нового обещания, переданные обещания также будут отменены, если никто больше на них не подписан. | |
| 667 /// </summary> | |
| 668 /// <param name="promises">Список обещаний. Если список пустой, то результирующее обещание возвращается уже выполненным.</param> | |
| 669 /// <returns>Обещание объединяющее в себе результат переданных обещаний.</returns> | |
| 670 /// <exception cref="ArgumentNullException"><paramref name="promises"/> не может быть null</exception> | |
| 30 | 671 public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) { |
| 25 | 672 if (promises == null) |
| 673 throw new ArgumentNullException(); | |
| 674 | |
| 675 // создаем аккумулятор для результатов и результирующее обещание | |
| 676 var result = new T[promises.Count]; | |
| 677 var promise = new Promise<T[]>(); | |
| 678 | |
| 679 // special case | |
| 680 if (promises.Count == 0) { | |
| 681 promise.Resolve(result); | |
| 682 return promise; | |
| 683 } | |
| 684 | |
| 685 int pending = promises.Count; | |
| 686 | |
| 687 for (int i = 0; i < promises.Count; i++) { | |
| 688 var dest = i; | |
| 689 | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
690 if (promises[i] != null) { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
691 promises[i].Then( |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
692 x => { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
693 result[dest] = x; |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
694 if (Interlocked.Decrement(ref pending) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
695 promise.Resolve(result); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
696 }, |
| 72 | 697 e => { |
| 698 promise.Reject(e); | |
| 699 return default(T); | |
| 700 } | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
701 ); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
702 } else { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
703 if (Interlocked.Decrement(ref pending) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
704 promise.Resolve(result); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
705 } |
| 25 | 706 } |
| 707 | |
| 708 promise.Cancelled( | |
| 709 () => { | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
710 foreach (var d in promises) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
711 if (d != null && d.IsExclusive) |
| 25 | 712 d.Cancel(); |
| 713 } | |
| 714 ); | |
| 715 | |
| 716 return promise; | |
| 717 } | |
| 718 | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
719 /// <summary> |
|
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 /// выполнении всех указанных обещаний. При этом возвращаемые значения первичных обещаний |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
722 /// игнорируются. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
723 /// </summary> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
724 /// <param name="promises">Коллекция первичных обещаний, которые будут объеденены в одно.</param> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
725 /// <returns>Новое обещание, объединяющее в себе переданные.</returns> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
726 /// <remarks> |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
727 /// Если в коллекции встречаюься <c>null</c>, то они воспринимаются как выполненные обещания. |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
728 /// </remarks> |
| 66 | 729 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
|
730 if (promises == null) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
731 throw new ArgumentNullException(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
732 if (promises.Count == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
733 return Promise<object>.ResultToPromise(null); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
734 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
735 int countdown = promises.Count; |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
736 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
737 var result = new Promise<object>(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
738 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
739 foreach (var d in promises) { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
740 if (d == null) { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
741 if (Interlocked.Decrement(ref countdown) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
742 result.Resolve(null); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
743 } else { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
744 d.Then(() => { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
745 if (Interlocked.Decrement(ref countdown) == 0) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
746 result.Resolve(null); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
747 }); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
748 } |
|
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 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
751 result.Cancelled(() => { |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
752 foreach (var d in promises) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
753 if (d != null && d.IsExclusive) |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
754 d.Cancel(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
755 }); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
756 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
757 return result; |
|
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 |
| 25 | 760 public static Promise<T> ResultToPromise(T result) { |
| 761 var p = new Promise<T>(); | |
| 762 p.Resolve(result); | |
| 763 return p; | |
| 764 } | |
| 765 | |
| 766 public static Promise<T> ExceptionToPromise(Exception error) { | |
| 767 if (error == null) | |
| 768 throw new ArgumentNullException(); | |
| 769 | |
| 770 var p = new Promise<T>(); | |
| 771 p.Reject(error); | |
| 772 return p; | |
| 773 } | |
| 774 | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
775 #region IPromiseBase explicit implementation |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
776 |
| 101 | 777 IPromise IPromise.Then(Action success, Action<Exception> error, Action cancel) { |
| 76 | 778 return Then( |
| 101 | 779 success != null ? new Action<T>(x => success()) : null, |
| 780 error != null ? new Func<Exception,T>(e => { | |
| 76 | 781 error(e); |
| 782 return default(T); | |
| 96 | 783 }) : null, |
| 76 | 784 cancel |
| 785 ); | |
| 786 } | |
| 787 | |
| 101 | 788 IPromise IPromise.Then(Action success, Action<Exception> error) { |
| 76 | 789 return Then( |
| 101 | 790 success != null ? new Action<T>(x => success()) : null, |
| 791 error != null ? new Func<Exception,T>(e => { | |
| 76 | 792 error(e); |
| 793 return default(T); | |
| 96 | 794 }) : null |
| 76 | 795 ); |
| 796 } | |
| 797 | |
| 798 IPromise IPromise.Then(Action success) { | |
| 96 | 799 Safe.ArgumentNotNull(success, "success"); |
| 76 | 800 return Then(x => success()); |
| 801 } | |
| 802 | |
| 101 | 803 IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) { |
| 96 | 804 return ChainNoResult(chained, error, cancel); |
| 805 } | |
| 806 | |
| 101 | 807 IPromise ChainNoResult(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) { |
| 104 | 808 Safe.ArgumentNotNull(chained, "chained"); |
| 96 | 809 |
| 101 | 810 var medium = new Promise<object>(this); |
| 96 | 811 |
| 101 | 812 Action<T> resultHandler = delegate { |
| 104 | 813 if (medium.IsCancelled) |
| 814 return; | |
| 96 | 815 |
| 104 | 816 var promise = chained(); |
| 96 | 817 |
| 104 | 818 promise.On( |
| 819 medium.Resolve, | |
| 820 medium.Reject, | |
| 821 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка | |
| 822 ); | |
| 96 | 823 |
| 104 | 824 // notify chained operation that it's not needed anymore |
| 825 // порядок вызова Then, Cancelled важен, поскольку от этого | |
| 826 // зависит IsExclusive | |
| 827 medium.Cancelled(() => { | |
| 828 if (promise.IsExclusive) | |
| 829 promise.Cancel(); | |
| 830 }); | |
| 831 }; | |
| 96 | 832 |
| 101 | 833 Func<Exception,T> errorHandler; |
| 96 | 834 |
| 104 | 835 if (error != null) |
| 836 errorHandler = delegate(Exception e) { | |
| 96 | 837 try { |
| 838 var promise = error(e); | |
| 839 | |
| 104 | 840 promise.On( |
| 96 | 841 medium.Resolve, |
| 842 medium.Reject, | |
| 843 () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка | |
| 844 ); | |
| 845 | |
| 846 // notify chained operation that it's not needed anymore | |
| 847 // порядок вызова Then, Cancelled важен, поскольку от этого | |
| 848 // зависит IsExclusive | |
| 849 medium.Cancelled(() => { | |
| 850 if (promise.IsExclusive) | |
| 851 promise.Cancel(); | |
| 852 }); | |
| 853 } catch (Exception e2) { | |
| 854 medium.Reject(e2); | |
| 855 } | |
| 856 return default(T); | |
| 857 }; | |
| 104 | 858 else |
| 859 errorHandler = err => { | |
| 96 | 860 medium.Reject(err); |
| 861 return default(T); | |
| 862 }; | |
| 863 | |
| 864 | |
| 104 | 865 Action cancelHandler; |
| 866 if (cancel != null) | |
| 867 cancelHandler = () => { | |
| 96 | 868 if (cancel != null) |
| 869 cancel(); | |
| 870 medium.Cancel(); | |
| 871 }; | |
| 104 | 872 else |
| 873 cancelHandler = medium.Cancel; | |
| 96 | 874 |
| 104 | 875 AddHandler( |
| 876 resultHandler, | |
| 877 errorHandler, | |
| 878 cancelHandler, | |
| 879 null, | |
| 880 true | |
| 881 ); | |
| 96 | 882 |
| 104 | 883 return medium; |
| 96 | 884 } |
| 104 | 885 |
| 101 | 886 IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error) { |
| 96 | 887 return ChainNoResult(chained, error, null); |
| 888 } | |
| 104 | 889 |
| 96 | 890 IPromise IPromise.Chain(Func<IPromise> chained) { |
| 891 return ChainNoResult(chained, null, null); | |
| 104 | 892 } |
| 96 | 893 |
| 894 | |
| 104 | 895 void IPromise.On(Action success, Action<Exception> error, Action cancel) { |
| 105 | 896 On(success != null ? new Action<T>(x => success()) : null, error, cancel); |
| 76 | 897 } |
| 898 | |
| 104 | 899 void IPromise.On(Action success, Action<Exception> error) { |
| 900 On(x => success(), error, null); | |
| 76 | 901 } |
| 902 | |
| 104 | 903 void IPromise.On(Action success) { |
| 904 On(x => success(), null, null); | |
| 76 | 905 } |
| 906 | |
| 101 | 907 IPromise IPromise.Error(Action<Exception> error) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
908 return Error(error); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
909 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
910 |
| 76 | 911 IPromise IPromise.Anyway(Action handler) { |
| 912 return Anyway(handler); | |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
913 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
914 |
| 66 | 915 IPromise IPromise.Cancelled(Action handler) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
916 return Cancelled(handler); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
917 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
918 |
| 66 | 919 void IPromise.Join() { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
920 Join(); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
921 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
922 |
| 66 | 923 void IPromise.Join(int timeout) { |
|
33
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
924 Join(timeout); |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
925 } |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
926 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
927 #endregion |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
928 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
929 |
|
b255e4aeef17
removed the reference to the parent from the promise object this allows
cin
parents:
32
diff
changeset
|
930 |
|
6
dfa21d507bc5
*refactoring: Promise.Then now returns a new chained promise
cin
parents:
2
diff
changeset
|
931 } |
| 2 | 932 } |
