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