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