Mercurial > pub > ImplabNet
comparison Implab/Deferred`1.cs @ 248:5cb4826c2c2a v3
Added awaiters to promises
Added static methods to Promise Resolve, Reject, All.
Updated promise helpers
author | cin |
---|---|
date | Tue, 30 Jan 2018 01:37:17 +0300 |
parents | 5aa9cfbe56c3 |
children | d82909310094 |
comparison
equal
deleted
inserted
replaced
247:fb70574741a1 | 248:5cb4826c2c2a |
---|---|
1 using System; | 1 using System; |
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 AbstractPromise<T> m_promise; | 6 readonly Promise<T> m_promise; |
7 readonly IDispatcher m_dispatcher; | 7 readonly IDispatcher m_dispatcher; |
8 | 8 |
9 internal Deferred(AbstractPromise<T> promise, IDispatcher dispatcher) { | 9 internal Deferred(IDispatcher dispatcher) : this(new Promise<T>(), dispatcher) { |
10 } | |
11 | |
12 internal Deferred(Promise<T> promise, IDispatcher dispatcher) { | |
10 Debug.Assert(promise != null); | 13 Debug.Assert(promise != null); |
11 m_promise = promise; | 14 m_promise = promise; |
12 m_dispatcher = dispatcher; | 15 m_dispatcher = dispatcher; |
13 } | 16 } |
14 | 17 |
15 public IPromise<T> Promise { | 18 public IPromise<T> Promise { |
16 get { return m_promise; } | 19 get { return m_promise; } |
17 } | 20 } |
18 | 21 |
19 public void Reject(Exception error) { | 22 public void Reject(Exception error) { |
20 m_promise.Reject(error); | 23 if (error is PromiseTransientException) |
24 error = ((PromiseTransientException)error).InnerException; | |
25 | |
26 m_promise.RejectPromise(error); | |
21 } | 27 } |
22 | 28 |
23 public void Resolve(T value) { | 29 public void Resolve(T value) { |
24 m_promise.Resolve(value); | 30 m_promise.ResolvePromise(value); |
25 } | 31 } |
26 | 32 |
27 public void Resolve(IPromise<T> thenable) { | 33 public void Resolve(IPromise<T> thenable) { |
28 if (thenable == null) | 34 if (thenable == null) |
29 Reject(new Exception("The promise or task are expected")); | 35 Reject(new Exception("The promise or task are expected")); |
30 if (thenable == m_promise) | 36 if (thenable == m_promise) |
31 Reject(new Exception("The promise cannot be resolved with oneself")); | 37 Reject(new Exception("The promise cannot be resolved with oneself")); |
32 | 38 |
33 else if (m_dispatcher != null) | 39 else if (m_dispatcher != null) |
34 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions) | 40 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions) |
35 m_dispatcher.Enqueue(() => Chain(thenable)); | 41 m_dispatcher.Enqueue(Chain, thenable); |
36 else | 42 else |
37 Chain(thenable); | 43 Chain(thenable); |
38 } | 44 } |
39 | 45 |
40 void Chain(IPromise<T> thenable) { | 46 void Chain(IPromise<T> thenable) { |