Mercurial > pub > ImplabNet
comparison Implab/Deferred`1.cs @ 249:d82909310094 v3
Implab.Test moved to xunit
Complete set of PromiseHelpers (Then, Catch, Finally)
Removed obsolete types ICancellable, ICancellationToken
| author | cin |
|---|---|
| date | Wed, 31 Jan 2018 11:28:38 +0300 |
| parents | 5cb4826c2c2a |
| children | 7c7e9ad6fe4a |
comparison
equal
deleted
inserted
replaced
| 248:5cb4826c2c2a | 249:d82909310094 |
|---|---|
| 2 using System.Diagnostics; | 2 using System.Diagnostics; |
| 3 | 3 |
| 4 namespace Implab { | 4 namespace Implab { |
| 5 public class Deferred<T> : IResolvable<T> { | 5 public class Deferred<T> : IResolvable<T> { |
| 6 readonly Promise<T> m_promise; | 6 readonly Promise<T> m_promise; |
| 7 readonly IDispatcher m_dispatcher; | |
| 8 | 7 |
| 9 internal Deferred(IDispatcher dispatcher) : this(new Promise<T>(), dispatcher) { | 8 internal Deferred() { |
| 9 m_promise = new Promise<T>(); | |
| 10 } | 10 } |
| 11 | 11 |
| 12 internal Deferred(Promise<T> promise, IDispatcher dispatcher) { | 12 protected Deferred(Promise<T> promise) { |
| 13 Debug.Assert(promise != null); | 13 Debug.Assert(promise != null); |
| 14 m_promise = promise; | 14 m_promise = promise; |
| 15 m_dispatcher = dispatcher; | |
| 16 } | 15 } |
| 17 | 16 |
| 18 public IPromise<T> Promise { | 17 public IPromise<T> Promise { |
| 19 get { return m_promise; } | 18 get { return m_promise; } |
| 20 } | 19 } |
| 21 | 20 |
| 22 public void Reject(Exception error) { | 21 public void Cancel() { |
| 22 Reject(new OperationCanceledException()); | |
| 23 } | |
| 24 | |
| 25 public virtual void Reject(Exception error) { | |
| 23 if (error is PromiseTransientException) | 26 if (error is PromiseTransientException) |
| 24 error = ((PromiseTransientException)error).InnerException; | 27 error = ((PromiseTransientException)error).InnerException; |
| 25 | 28 |
| 26 m_promise.RejectPromise(error); | 29 m_promise.RejectPromise(error); |
| 27 } | 30 } |
| 28 | 31 |
| 29 public void Resolve(T value) { | 32 public virtual void Resolve(T value) { |
| 30 m_promise.ResolvePromise(value); | 33 m_promise.ResolvePromise(value); |
| 31 } | 34 } |
| 32 | 35 |
| 33 public void Resolve(IPromise<T> thenable) { | 36 public virtual void Resolve(IPromise<T> thenable) { |
| 34 if (thenable == null) | 37 if (thenable == null) |
| 35 Reject(new Exception("The promise or task are expected")); | 38 Reject(new Exception("The promise or task are expected")); |
| 36 if (thenable == m_promise) | 39 if (thenable == m_promise) |
| 37 Reject(new Exception("The promise cannot be resolved with oneself")); | 40 Reject(new Exception("The promise cannot be resolved with oneself")); |
| 38 | 41 |
| 39 else if (m_dispatcher != null) | |
| 40 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions) | |
| 41 m_dispatcher.Enqueue(Chain, thenable); | |
| 42 else | |
| 43 Chain(thenable); | |
| 44 } | |
| 45 | |
| 46 void Chain(IPromise<T> thenable) { | |
| 47 try { | 42 try { |
| 48 thenable.Then(this); | 43 thenable.Then(this); |
| 49 } catch (Exception err) { | 44 } catch (Exception err) { |
| 50 Reject(err); | 45 Reject(err); |
| 51 } | 46 } |
