annotate Implab/Deferred.cs @ 244:eee3e49dd1ff v3

working on promises
author cin
date Thu, 25 Jan 2018 19:09:16 +0300
parents
children b904e0a3ba72
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
1 using System;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
2 using System.Diagnostics;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
3
eee3e49dd1ff working on promises
cin
parents:
diff changeset
4 namespace Implab {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
5 /// <summary>
eee3e49dd1ff working on promises
cin
parents:
diff changeset
6 /// This class is responsible for the promise resolution, dispatching and chaining
eee3e49dd1ff working on promises
cin
parents:
diff changeset
7 /// </summary>
eee3e49dd1ff working on promises
cin
parents:
diff changeset
8 public class Deferred : IResolvable {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
9
eee3e49dd1ff working on promises
cin
parents:
diff changeset
10 readonly AbstractPromise m_promise;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
11 readonly IDispatcher m_dispatcher;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
12
eee3e49dd1ff working on promises
cin
parents:
diff changeset
13 internal Deferred(AbstractPromise promise, IDispatcher dispatcher) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
14 Debug.Assert(promise != null);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
15 m_promise = promise;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
16 m_dispatcher = dispatcher;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
17 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
18
eee3e49dd1ff working on promises
cin
parents:
diff changeset
19 public IPromise Promise {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
20 get { return m_promise; }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
21 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
22
eee3e49dd1ff working on promises
cin
parents:
diff changeset
23 public void Reject(Exception error) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
24 m_promise.Reject(error);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
25 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
26
eee3e49dd1ff working on promises
cin
parents:
diff changeset
27 public void Resolve() {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
28 m_promise.Resolve();
eee3e49dd1ff working on promises
cin
parents:
diff changeset
29 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
30
eee3e49dd1ff working on promises
cin
parents:
diff changeset
31 public void Resolve(IPromise thenable) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
32 if (thenable == null)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
33 Reject(new Exception("The promise or task are expected"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
34 if (thenable == m_promise)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
35 Reject(new Exception("The promise cannot be resolved with oneself"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
36
eee3e49dd1ff working on promises
cin
parents:
diff changeset
37 else if (m_dispatcher != null)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
38 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
39 m_dispatcher.Enqueue(() => thenable.Then(this));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
40 else
eee3e49dd1ff working on promises
cin
parents:
diff changeset
41 thenable.Then(this);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
42 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
43
eee3e49dd1ff working on promises
cin
parents:
diff changeset
44 void Chain(IPromise thenable) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
45 try {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
46 thenable.Then(this);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
47 } catch (Exception err) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
48 Reject(err);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
49 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
50 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
51 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
52 }