244
|
1 using System;
|
|
2 using System.Diagnostics;
|
|
3
|
|
4 namespace Implab {
|
|
5 /// <summary>
|
|
6 /// This class is responsible for the promise resolution, dispatching and chaining
|
|
7 /// </summary>
|
|
8 public class Deferred : IResolvable {
|
|
9
|
248
|
10 readonly Promise m_promise;
|
249
|
11 internal Deferred() {
|
|
12 m_promise = new Promise();
|
248
|
13 }
|
|
14
|
|
15 internal Deferred(Promise promise, IDispatcher dispatcher) {
|
244
|
16 Debug.Assert(promise != null);
|
|
17 m_promise = promise;
|
|
18 }
|
|
19
|
|
20 public IPromise Promise {
|
|
21 get { return m_promise; }
|
|
22 }
|
|
23
|
249
|
24 public void Cancel() {
|
|
25 Reject(new OperationCanceledException());
|
|
26 }
|
|
27
|
|
28 public virtual void Reject(Exception error) {
|
248
|
29 if (error is PromiseTransientException)
|
|
30 error = ((PromiseTransientException)error).InnerException;
|
|
31
|
|
32 m_promise.RejectPromise(error);
|
244
|
33 }
|
|
34
|
249
|
35 public virtual void Resolve() {
|
248
|
36 m_promise.ResolvePromise();
|
244
|
37 }
|
|
38
|
249
|
39 public virtual void Resolve(IPromise thenable) {
|
244
|
40 if (thenable == null)
|
|
41 Reject(new Exception("The promise or task are expected"));
|
|
42 if (thenable == m_promise)
|
|
43 Reject(new Exception("The promise cannot be resolved with oneself"));
|
|
44
|
|
45 try {
|
|
46 thenable.Then(this);
|
|
47 } catch (Exception err) {
|
|
48 Reject(err);
|
|
49 }
|
|
50 }
|
249
|
51
|
244
|
52 }
|
|
53 } |