comparison Implab/Deferred.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 b904e0a3ba72
children d82909310094
comparison
equal deleted inserted replaced
247:fb70574741a1 248:5cb4826c2c2a
5 /// <summary> 5 /// <summary>
6 /// This class is responsible for the promise resolution, dispatching and chaining 6 /// This class is responsible for the promise resolution, dispatching and chaining
7 /// </summary> 7 /// </summary>
8 public class Deferred : IResolvable { 8 public class Deferred : IResolvable {
9 9
10 readonly AbstractPromise m_promise; 10 readonly Promise m_promise;
11 readonly IDispatcher m_dispatcher; 11 readonly IDispatcher m_dispatcher;
12 12
13 internal Deferred(AbstractPromise promise, IDispatcher dispatcher) { 13 internal Deferred(IDispatcher dispatcher) : this(new Promise(), dispatcher) {
14 }
15
16 internal Deferred(Promise promise, IDispatcher dispatcher) {
14 Debug.Assert(promise != null); 17 Debug.Assert(promise != null);
15 m_promise = promise; 18 m_promise = promise;
16 m_dispatcher = dispatcher; 19 m_dispatcher = dispatcher;
17 } 20 }
18 21
19 public IPromise Promise { 22 public IPromise Promise {
20 get { return m_promise; } 23 get { return m_promise; }
21 } 24 }
22 25
23 public void Reject(Exception error) { 26 public void Reject(Exception error) {
24 m_promise.Reject(error); 27 if (error is PromiseTransientException)
28 error = ((PromiseTransientException)error).InnerException;
29
30 m_promise.RejectPromise(error);
25 } 31 }
26 32
27 public void Resolve() { 33 public void Resolve() {
28 m_promise.Resolve(); 34 m_promise.ResolvePromise();
29 } 35 }
30 36
31 public void Resolve(IPromise thenable) { 37 public void Resolve(IPromise thenable) {
32 if (thenable == null) 38 if (thenable == null)
33 Reject(new Exception("The promise or task are expected")); 39 Reject(new Exception("The promise or task are expected"));
34 if (thenable == m_promise) 40 if (thenable == m_promise)
35 Reject(new Exception("The promise cannot be resolved with oneself")); 41 Reject(new Exception("The promise cannot be resolved with oneself"));
36 42
37 else if (m_dispatcher != null) 43 else if (m_dispatcher != null)
38 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions) 44 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
39 m_dispatcher.Enqueue(() => Chain(thenable)); 45 m_dispatcher.Enqueue(Chain, thenable);
40 else 46 else
41 Chain(thenable); 47 Chain(thenable);
42 } 48 }
43 49
44 void Chain(IPromise thenable) { 50 void Chain(IPromise thenable) {