annotate Implab/Deferred.cs @ 248:5cb4826c2c2a v3

Added awaiters to promises Added static methods to Promise Resolve, Reject, All. Updated promise helpers
author cin
date Tue, 30 Jan 2018 01:37:17 +0300
parents b904e0a3ba72
children d82909310094
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
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
10 readonly Promise m_promise;
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
11 readonly IDispatcher m_dispatcher;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
12
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
13 internal Deferred(IDispatcher dispatcher) : this(new Promise(), dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
14 }
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
15
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
16 internal Deferred(Promise promise, IDispatcher dispatcher) {
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
17 Debug.Assert(promise != null);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
18 m_promise = promise;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
19 m_dispatcher = dispatcher;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
20 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
21
eee3e49dd1ff working on promises
cin
parents:
diff changeset
22 public IPromise Promise {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
23 get { return m_promise; }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
24 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
25
eee3e49dd1ff working on promises
cin
parents:
diff changeset
26 public void Reject(Exception error) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
27 if (error is PromiseTransientException)
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
28 error = ((PromiseTransientException)error).InnerException;
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
29
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
30 m_promise.RejectPromise(error);
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
31 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
32
eee3e49dd1ff working on promises
cin
parents:
diff changeset
33 public void Resolve() {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
34 m_promise.ResolvePromise();
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
35 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
36
eee3e49dd1ff working on promises
cin
parents:
diff changeset
37 public void Resolve(IPromise thenable) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
38 if (thenable == null)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
39 Reject(new Exception("The promise or task are expected"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
40 if (thenable == m_promise)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
41 Reject(new Exception("The promise cannot be resolved with oneself"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
42
eee3e49dd1ff working on promises
cin
parents:
diff changeset
43 else if (m_dispatcher != null)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
44 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
45 m_dispatcher.Enqueue(Chain, thenable);
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
46 else
245
b904e0a3ba72 working on promises
cin
parents: 244
diff changeset
47 Chain(thenable);
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
48 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
49
eee3e49dd1ff working on promises
cin
parents:
diff changeset
50 void Chain(IPromise thenable) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
51 try {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
52 thenable.Then(this);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
53 } catch (Exception err) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
54 Reject(err);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
55 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
56 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
57 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
58 }