Mercurial > pub > ImplabNet
annotate Implab/PromiseT.cs @ 133:6c49d02a9a05 v2
sync
| author | cin |
|---|---|
| date | Mon, 09 Feb 2015 00:28:13 +0300 |
| parents | 671f60cd0250 |
| children | 656815cb7147 |
| rev | line source |
|---|---|
| 119 | 1 using System; |
| 2 using System.Diagnostics; | |
| 3 | |
| 4 namespace Implab { | |
| 5 | |
| 6 /// <summary> | |
| 7 /// Класс для асинхронного получения результатов. Так называемое "обещание". | |
| 8 /// </summary> | |
| 9 /// <typeparam name="T">Тип получаемого результата</typeparam> | |
| 10 /// <remarks> | |
| 11 /// <para>Сервис при обращении к его методу дает обещаиние о выполнении операции, | |
| 12 /// клиент получив такое обещание может установить ряд обратных вызово для получения | |
| 13 /// событий выполнения обещания, тоесть завершения операции и предоставлении результатов.</para> | |
| 14 /// <para> | |
| 15 /// Обещение может быть как выполнено, так и выполнено с ошибкой. Для подписки на | |
| 16 /// данные события клиент должен использовать методы <c>Then</c>. | |
| 17 /// </para> | |
| 18 /// <para> | |
| 19 /// Сервис, в свою очередь, по окончанию выполнения операции (возможно с ошибкой), | |
| 20 /// использует методы <c>Resolve</c> либо <c>Reject</c> для оповещения клиетна о | |
| 21 /// выполнении обещания. | |
| 22 /// </para> | |
| 23 /// <para> | |
| 24 /// Если сервер успел выполнить обещание еще до того, как клиент на него подписался, | |
| 25 /// то в момент подписки клиента будут вызваны соответсвующие события в синхронном | |
| 26 /// режиме и клиент будет оповещен в любом случае. Иначе, обработчики добавляются в | |
| 27 /// список в порядке подписания и в этом же порядке они будут вызваны при выполнении | |
| 28 /// обещания. | |
| 29 /// </para> | |
| 30 /// <para> | |
| 31 /// Обрабатывая результаты обещания можно преобразовывать результаты либо инициировать | |
| 32 /// связанные асинхронные операции, которые также возвращают обещания. Для этого следует | |
| 33 /// использовать соответствующую форму методе <c>Then</c>. | |
| 34 /// </para> | |
| 35 /// <para> | |
| 36 /// Также хорошим правилом является то, что <c>Resolve</c> и <c>Reject</c> должен вызывать | |
| 37 /// только инициатор обещания иначе могут возникнуть противоречия. | |
| 38 /// </para> | |
| 39 /// </remarks> | |
| 40 public class Promise<T> : AbstractPromise<IDeferred<T>>, IPromise<T>, IDeferred<T> { | |
| 41 | |
| 42 class StubDeferred : IDeferred<T> { | |
| 43 public static readonly StubDeferred instance = new StubDeferred(); | |
| 44 | |
| 45 StubDeferred() { | |
| 46 } | |
| 47 | |
| 48 #region IDeferred implementation | |
| 49 | |
| 50 public void Resolve(T value) { | |
| 51 } | |
| 52 | |
| 53 public void Reject(Exception error) { | |
| 54 } | |
| 55 | |
| 56 #endregion | |
| 57 | |
| 58 #region ICancellable implementation | |
| 59 | |
| 60 public void Cancel() { | |
| 61 } | |
| 62 | |
| 63 #endregion | |
| 64 | |
| 65 | |
| 66 } | |
| 67 | |
| 68 class RemapDescriptor<T2> : IDeferred<T> { | |
| 69 readonly Func<T,T2> m_remap; | |
| 70 readonly Func<Exception,T2> m_failed; | |
| 71 readonly Func<T2> m_cancel; | |
| 72 readonly IDeferred<T2> m_deferred; | |
| 73 | |
| 74 public RemapDescriptor(Func<T,T2> remap, Func<Exception,T2> failed, Func<T2> cancel, IDeferred<T2> deferred ) { | |
| 75 Debug.Assert(deferred != null); | |
| 76 m_remap = remap; | |
| 77 m_failed = failed; | |
| 78 m_cancel = cancel; | |
| 79 m_deferred = deferred; | |
| 80 } | |
| 81 | |
| 82 | |
| 83 | |
| 84 #region IDeferred implementation | |
| 85 | |
| 86 public void Resolve(T value) { | |
| 87 if (m_remap != null) { | |
| 88 try { | |
| 89 m_deferred.Resolve(m_remap(value)); | |
| 90 } catch (Exception ex) { | |
| 91 Reject(ex); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 public void Reject(Exception error) { | |
| 97 if (m_failed != null) { | |
| 98 try { | |
| 99 m_deferred.Resolve(m_failed(error)); | |
| 100 } catch (Exception ex) { | |
| 101 m_deferred.Reject(ex); | |
| 102 } | |
| 103 } else { | |
| 104 m_deferred.Reject(error); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 | |
| 109 #endregion | |
| 110 | |
| 111 #region ICancellable implementation | |
| 112 | |
| 113 public void Cancel() { | |
| 114 if (m_cancel != null) { | |
| 115 try { | |
| 116 m_deferred.Resolve(m_cancel()); | |
| 117 } catch (Exception ex) { | |
| 118 Reject(ex); | |
| 119 } | |
| 120 } else { | |
| 121 m_deferred.Cancel(); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 #endregion | |
| 126 } | |
| 127 | |
| 128 class ListenerDescriptor : IDeferred<T> { | |
| 129 readonly Action m_handler; | |
| 130 readonly PromiseEventType m_events; | |
| 131 | |
| 132 public ListenerDescriptor(Action handler, PromiseEventType events) { | |
| 133 Debug.Assert(handler != null); | |
| 134 | |
| 135 m_handler = handler; | |
| 136 m_events = events; | |
| 137 } | |
| 138 | |
| 139 #region IDeferred implementation | |
| 140 | |
| 141 public void Resolve(T value) { | |
| 142 if (m_events.HasFlag(PromiseEventType.Success)) { | |
| 143 try { | |
| 144 m_handler(); | |
| 145 // Analysis disable once EmptyGeneralCatchClause | |
| 146 } catch { | |
| 147 } | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 public void Reject(Exception error) { | |
| 152 if (m_events.HasFlag(PromiseEventType.Error)){ | |
| 153 try { | |
| 154 m_handler(); | |
| 155 // Analysis disable once EmptyGeneralCatchClause | |
| 156 } catch { | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 #endregion | |
| 162 | |
| 163 #region ICancellable implementation | |
| 164 | |
| 165 public void Cancel() { | |
| 166 if (m_events.HasFlag(PromiseEventType.Cancelled)){ | |
| 167 try { | |
| 168 m_handler(); | |
| 169 // Analysis disable once EmptyGeneralCatchClause | |
| 170 } catch { | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 #endregion | |
| 176 } | |
| 177 | |
| 178 class ValueEventDescriptor : IDeferred<T> { | |
| 179 readonly Action<T> m_success; | |
| 180 readonly Action<Exception> m_failed; | |
| 181 readonly Action m_cancelled; | |
| 182 readonly IDeferred<T> m_deferred; | |
| 183 | |
| 184 public ValueEventDescriptor(Action<T> success, Action<Exception> failed, Action cancelled, IDeferred<T> deferred) { | |
| 185 Debug.Assert(deferred != null); | |
| 186 | |
| 187 m_success = success; | |
| 188 m_failed = failed; | |
| 189 m_cancelled = cancelled; | |
| 190 m_deferred = deferred; | |
| 191 } | |
| 192 | |
| 193 #region IDeferred implementation | |
| 194 | |
| 195 public void Resolve(T value) { | |
| 196 if (m_success != null) { | |
| 197 try { | |
| 198 m_success(value); | |
| 199 m_deferred.Resolve(value); | |
| 200 } catch (Exception ex) { | |
| 201 Reject(ex); | |
| 202 } | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 public void Reject(Exception error) { | |
| 207 if (m_failed != null) { | |
| 208 try { | |
| 209 m_failed(error); | |
| 210 m_deferred.Resolve(default(T)); | |
| 211 } catch(Exception ex) { | |
| 212 m_deferred.Reject(ex); | |
| 213 } | |
| 214 } else { | |
| 215 m_deferred.Reject(error); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 #endregion | |
| 220 | |
| 221 #region ICancellable implementation | |
| 222 | |
| 223 public void Cancel() { | |
| 224 if (m_cancelled != null) { | |
| 225 try { | |
| 226 m_cancelled(); | |
| 227 m_deferred.Resolve(default(T)); | |
| 228 } catch(Exception ex) { | |
| 229 Reject(ex); | |
| 230 } | |
| 231 } else { | |
| 232 m_deferred.Cancel(); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 #endregion | |
| 237 } | |
| 238 | |
| 239 public class EventDescriptor : IDeferred<T> { | |
| 240 readonly Action m_success; | |
| 241 readonly Action<Exception> m_failed; | |
| 242 readonly Action m_cancelled; | |
| 243 readonly IDeferred<T> m_deferred; | |
| 244 | |
| 245 public EventDescriptor(Action success, Action<Exception> failed, Action cancelled, IDeferred<T> deferred) { | |
| 246 Debug.Assert(deferred != null); | |
| 247 | |
| 248 m_success = success; | |
| 249 m_failed = failed; | |
| 250 m_cancelled = cancelled; | |
| 251 m_deferred = deferred; | |
| 252 } | |
| 253 | |
| 254 #region IDeferred implementation | |
| 255 | |
| 256 public void Resolve(T value) { | |
| 257 if (m_success != null) { | |
| 258 try { | |
| 259 m_success(); | |
| 260 m_deferred.Resolve(value); | |
| 261 } catch (Exception ex) { | |
| 262 Reject(ex); | |
| 263 } | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 public void Reject(Exception error) { | |
| 268 if (m_failed != null) { | |
| 269 try { | |
| 270 m_failed(error); | |
| 271 m_deferred.Resolve(default(T)); | |
| 272 }catch (Exception ex) | |
| 273 { | |
| 274 m_deferred.Reject(ex); | |
| 275 } | |
| 276 } else { | |
| 277 m_deferred.Reject(error); | |
| 278 } | |
| 279 | |
| 280 } | |
| 281 | |
| 282 #endregion | |
| 283 | |
| 284 #region ICancellable implementation | |
| 285 | |
| 286 public void Cancel() { | |
| 287 if (m_cancelled != null) { | |
| 288 try { | |
| 289 m_cancelled(); | |
| 290 m_deferred.Resolve(default(T)); | |
| 291 } catch (Exception ex) { | |
| 292 Reject(ex); | |
| 293 } | |
| 294 } else { | |
| 295 m_deferred.Cancel(); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 #endregion | |
| 300 } | |
| 301 | |
| 302 T m_result; | |
| 303 | |
| 304 public virtual void Resolve(T value) { | |
|
130
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
119
diff
changeset
|
305 if (BeginSetResult()) { |
|
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
119
diff
changeset
|
306 m_result = value; |
|
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
119
diff
changeset
|
307 EndSetResult(); |
|
671f60cd0250
fixed Resove method bug when calling it on already cancelled promise
cin
parents:
119
diff
changeset
|
308 } |
| 119 | 309 } |
| 310 | |
| 311 public void Reject(Exception error) { | |
| 312 SetError(error); | |
| 313 } | |
| 314 | |
| 315 public Type PromiseType { | |
| 316 get { | |
| 317 return typeof(T); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 public new T Join() { | |
| 322 WaitResult(-1); | |
| 323 return m_result; | |
| 324 } | |
| 325 public new T Join(int timeout) { | |
| 326 WaitResult(timeout); | |
| 327 return m_result; | |
| 328 } | |
| 329 | |
| 330 public IPromise<T> On(Action<T> success, Action<Exception> error, Action cancel) { | |
| 331 AddHandler(new ValueEventDescriptor(success, error, cancel, StubDeferred.instance)); | |
| 332 return this; | |
| 333 } | |
| 334 | |
| 335 public IPromise<T> On(Action<T> success, Action<Exception> error) { | |
| 336 AddHandler(new ValueEventDescriptor(success, error, null, StubDeferred.instance)); | |
| 337 return this; | |
| 338 } | |
| 339 | |
| 340 public IPromise<T> On(Action<T> success) { | |
| 341 AddHandler(new ValueEventDescriptor(success, null, null, StubDeferred.instance)); | |
| 342 return this; | |
| 343 } | |
| 344 | |
| 345 public IPromise<T> On(Action handler, PromiseEventType events) { | |
| 346 Listen(events, handler); | |
| 347 return this; | |
| 348 } | |
| 349 | |
| 350 public IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception, T2> error, Func<T2> cancel) { | |
| 351 var promise = new Promise<T2>(); | |
| 352 AddHandler(new RemapDescriptor<T2>(mapper, error, cancel, promise)); | |
| 353 return promise; | |
| 354 } | |
| 355 | |
| 356 public IPromise<T2> Then<T2>(Func<T, T2> mapper, Func<Exception, T2> error) { | |
| 357 var promise = new Promise<T2>(); | |
| 358 AddHandler(new RemapDescriptor<T2>(mapper, error, null, promise)); | |
| 359 return promise; | |
| 360 } | |
| 361 | |
| 362 public IPromise<T2> Then<T2>(Func<T, T2> mapper) { | |
| 363 var promise = new Promise<T2>(); | |
| 364 AddHandler(new RemapDescriptor<T2>(mapper, null, null, promise)); | |
| 365 return promise; | |
| 366 } | |
| 367 | |
| 368 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception, IPromise<T2>> error, Func<IPromise<T2>> cancel) { | |
| 369 // this promise will be resolved when an asyc operation is started | |
| 370 var promise = new Promise<IPromise<T2>>(); | |
| 371 | |
| 372 AddHandler(new RemapDescriptor<IPromise<T2>>( | |
| 373 chained, | |
| 374 error, | |
| 375 cancel, | |
| 376 promise | |
| 377 )); | |
| 378 | |
| 379 var medium = new Promise<T2>(); | |
| 380 | |
| 381 if (chained != null) | |
| 382 medium.On(Cancel, PromiseEventType.Cancelled); | |
| 383 | |
| 384 // we need to connect started async operation with the medium | |
| 385 // if the async operation hasn't been started by the some reason | |
| 386 // report is to the medium | |
| 387 promise.On( | |
| 388 result => ConnectPromise<T2>(result, medium), | |
| 389 medium.Reject, | |
| 390 medium.Cancel | |
| 391 ); | |
| 392 | |
| 393 return medium; | |
| 394 } | |
| 395 | |
| 396 static void ConnectPromise<T2>(IPromise<T2> result, Promise<T2> medium) { | |
| 397 if (result != null) { | |
| 398 result.On( | |
| 399 medium.Resolve, | |
| 400 medium.Reject, | |
| 401 () => medium.Reject(new OperationCanceledException()) | |
| 402 ); | |
| 403 medium.On(result.Cancel, PromiseEventType.Cancelled); | |
| 404 } else { | |
| 405 medium.Reject( | |
| 406 new NullReferenceException( | |
| 407 "The chained asynchronous operation returned" + | |
| 408 " 'null' where the promise instance is expected" | |
| 409 ) | |
| 410 ); | |
| 411 } | |
| 412 } | |
| 413 | |
| 414 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained, Func<Exception, IPromise<T2>> error) { | |
| 415 return Chain(chained, error, null); | |
| 416 } | |
| 417 | |
| 418 public IPromise<T2> Chain<T2>(Func<T, IPromise<T2>> chained) { | |
| 419 return Chain(chained, null, null); | |
| 420 } | |
| 421 | |
| 422 public IPromise<T2> Error<T2>(Func<Exception, T2> error) { | |
| 423 var promise = new Promise<T2>(); | |
| 424 if (error != null) | |
| 425 On( | |
| 426 (Action<T>)null, | |
| 427 ex => { | |
| 428 try { | |
| 429 promise.Resolve(error(ex)); | |
| 430 } catch (Exception ex2) { | |
| 431 promise.Reject(ex2); | |
| 432 } | |
| 433 } | |
| 434 ); | |
| 435 else | |
| 436 Listen(PromiseEventType.Error, () => promise.Resolve(default(T2))); | |
| 437 return promise; | |
| 438 } | |
| 439 | |
| 440 public IPromise<T2> Cancelled<T2>(Func<T2> handler) { | |
| 441 var promise = new Promise<T2>(); | |
| 442 if (handler != null) | |
| 443 On( | |
| 444 (Action<T>)null, | |
| 445 null, | |
| 446 () => { | |
| 447 try { | |
| 448 promise.Resolve(handler()); | |
| 449 } catch (Exception ex) { | |
| 450 promise.Reject(ex); | |
| 451 } | |
| 452 }); | |
| 453 else | |
| 454 Listen(PromiseEventType.Cancelled, () => promise.Resolve(default(T2))); | |
| 455 return promise; | |
| 456 } | |
| 457 | |
| 458 public IPromise Then(Action success, Action<Exception> error, Action cancel) { | |
| 459 var promise = new Promise<T>(); | |
| 460 if (success != null) | |
| 461 promise.On(Cancel, PromiseEventType.Cancelled); | |
| 462 | |
| 463 AddHandler(new EventDescriptor(success, error, cancel, promise)); | |
| 464 | |
| 465 return promise; | |
| 466 } | |
| 467 | |
| 468 public IPromise Then(Action success, Action<Exception> error) { | |
| 469 return Then(success, error, null); | |
| 470 } | |
| 471 | |
| 472 public IPromise Then(Action success) { | |
| 473 return Then(success, null, null); | |
| 474 } | |
| 475 | |
| 476 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error, Func<IPromise> cancel) { | |
| 477 var promise = new Promise<IPromise>(); | |
| 478 | |
| 479 AddHandler( | |
| 480 new RemapDescriptor<IPromise>( | |
| 481 x => chained(), | |
| 482 error, | |
| 483 cancel, | |
| 484 promise | |
| 485 ) | |
| 486 ); | |
| 487 | |
| 488 var medium = new Promise(); | |
| 489 if (chained != null) | |
| 490 medium.On(Cancel, PromiseEventType.Cancelled); | |
| 491 | |
| 492 promise.On( | |
| 493 result => ConnectPromise(result, medium), | |
| 494 medium.Reject, | |
| 495 medium.Cancel | |
| 496 ); | |
| 497 | |
| 498 return medium; | |
| 499 } | |
| 500 | |
| 501 static void ConnectPromise(IPromise result, Promise medium) { | |
| 502 if (result != null) { | |
| 503 result.On( | |
| 504 medium.Resolve, | |
| 505 medium.Reject, | |
| 506 () => medium.Reject(new OperationCanceledException()) | |
| 507 ); | |
| 508 medium.On(result.Cancel, PromiseEventType.Cancelled); | |
| 509 } else { | |
| 510 medium.Reject( | |
| 511 new NullReferenceException( | |
| 512 "The chained asynchronous operation returned" + | |
| 513 " 'null' where the promise instance is expected" | |
| 514 ) | |
| 515 ); | |
| 516 } | |
| 517 } | |
| 518 | |
| 519 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error) { | |
| 520 return Chain(chained, error, null); | |
| 521 } | |
| 522 | |
| 523 public IPromise Chain(Func<IPromise> chained) { | |
| 524 return Chain(chained, null, null); | |
| 525 } | |
| 526 | |
| 527 public IPromise On(Action success, Action<Exception> error, Action cancel) { | |
| 528 AddHandler(new EventDescriptor(success,error,cancel, StubDeferred.instance)); | |
| 529 return this; | |
| 530 } | |
| 531 | |
| 532 public IPromise On(Action success, Action<Exception> error) { | |
| 533 AddHandler(new EventDescriptor(success, error, null, StubDeferred.instance)); | |
| 534 return this; | |
| 535 } | |
| 536 | |
| 537 public IPromise On(Action success) { | |
| 538 Listen(PromiseEventType.Success, success); | |
| 539 return this; | |
| 540 } | |
| 541 | |
| 542 IPromise IPromise.On(Action handler, PromiseEventType events) { | |
| 543 Listen(events,handler); | |
| 544 return this; | |
| 545 } | |
| 546 | |
| 547 public IPromise Error(Action<Exception> error) { | |
| 548 var promise = new Promise(); | |
| 549 if (error != null) | |
| 550 On( | |
| 551 (Action<T>)null, | |
| 552 ex => { | |
| 553 try { | |
| 554 error(ex); | |
| 555 promise.Resolve(); | |
| 556 } catch (Exception ex2) { | |
| 557 promise.Reject(ex2); | |
| 558 } | |
| 559 }); | |
| 560 else | |
| 561 Listen(PromiseEventType.Error, promise.Resolve); | |
| 562 return promise; | |
| 563 } | |
| 564 | |
| 565 public IPromise Cancelled(Action handler) { | |
| 566 var promise = new Promise(); | |
| 567 if (handler != null) | |
| 568 On( | |
| 569 (Action<T>)null, | |
| 570 null, | |
| 571 () => { | |
| 572 try { | |
| 573 handler(); | |
| 574 promise.Resolve(); | |
| 575 } catch (Exception ex) { | |
| 576 promise.Reject(ex); | |
| 577 } | |
| 578 }); | |
| 579 else | |
| 580 Listen(PromiseEventType.Cancelled, promise.Resolve); | |
| 581 return promise; | |
| 582 } | |
| 583 | |
| 584 public IPromise<T2> Cast<T2>() { | |
| 585 return (IPromise<T2>)this; | |
| 586 } | |
| 587 | |
| 588 #region implemented abstract members of AbstractPromise | |
| 589 | |
| 590 protected override void SignalSuccess(IDeferred<T> handler) { | |
| 591 handler.Resolve(m_result); | |
| 592 } | |
| 593 | |
| 594 protected override void SignalError(IDeferred<T> handler, Exception error) { | |
| 595 handler.Reject(error); | |
| 596 } | |
| 597 | |
| 598 protected override void SignalCancelled(IDeferred<T> handler) { | |
| 599 handler.Cancel(); | |
| 600 } | |
| 601 | |
| 602 protected override void Listen(PromiseEventType events, Action handler) { | |
| 603 if (handler != null) | |
| 604 AddHandler(new ListenerDescriptor(handler, events)); | |
| 605 } | |
| 606 | |
| 607 #endregion | |
| 608 | |
| 609 public static IPromise<T> ResultToPromise(T value) { | |
| 610 var p = new Promise<T>(); | |
| 611 p.Resolve(value); | |
| 612 return p; | |
| 613 } | |
| 614 | |
| 615 public static IPromise<T> ExceptionToPromise(Exception error) { | |
| 616 var p = new Promise<T>(); | |
| 617 p.Reject(error); | |
| 618 return p; | |
| 619 } | |
| 620 | |
| 621 } | |
| 622 } |
