comparison Implab/Deferred.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
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 Promise m_promise; 10 readonly Promise m_promise;
11 readonly IDispatcher m_dispatcher; 11 internal Deferred() {
12 12 m_promise = new Promise();
13 internal Deferred(IDispatcher dispatcher) : this(new Promise(), dispatcher) {
14 } 13 }
15 14
16 internal Deferred(Promise promise, IDispatcher dispatcher) { 15 internal Deferred(Promise promise, IDispatcher dispatcher) {
17 Debug.Assert(promise != null); 16 Debug.Assert(promise != null);
18 m_promise = promise; 17 m_promise = promise;
19 m_dispatcher = dispatcher;
20 } 18 }
21 19
22 public IPromise Promise { 20 public IPromise Promise {
23 get { return m_promise; } 21 get { return m_promise; }
24 } 22 }
25 23
26 public void Reject(Exception error) { 24 public void Cancel() {
25 Reject(new OperationCanceledException());
26 }
27
28 public virtual void Reject(Exception error) {
27 if (error is PromiseTransientException) 29 if (error is PromiseTransientException)
28 error = ((PromiseTransientException)error).InnerException; 30 error = ((PromiseTransientException)error).InnerException;
29 31
30 m_promise.RejectPromise(error); 32 m_promise.RejectPromise(error);
31 } 33 }
32 34
33 public void Resolve() { 35 public virtual void Resolve() {
34 m_promise.ResolvePromise(); 36 m_promise.ResolvePromise();
35 } 37 }
36 38
37 public void Resolve(IPromise thenable) { 39 public virtual void Resolve(IPromise thenable) {
38 if (thenable == null) 40 if (thenable == null)
39 Reject(new Exception("The promise or task are expected")); 41 Reject(new Exception("The promise or task are expected"));
40 if (thenable == m_promise) 42 if (thenable == m_promise)
41 Reject(new Exception("The promise cannot be resolved with oneself")); 43 Reject(new Exception("The promise cannot be resolved with oneself"));
42 44
43 else if (m_dispatcher != null)
44 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
45 m_dispatcher.Enqueue(Chain, thenable);
46 else
47 Chain(thenable);
48 }
49
50 void Chain(IPromise thenable) {
51 try { 45 try {
52 thenable.Then(this); 46 thenable.Then(this);
53 } catch (Exception err) { 47 } catch (Exception err) {
54 Reject(err); 48 Reject(err);
55 } 49 }
56 } 50 }
51
57 } 52 }
58 } 53 }