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