annotate Implab/Deferred`1.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents 5aa9cfbe56c3
children 5cb4826c2c2a
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> {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
6 readonly AbstractPromise<T> m_promise;
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
7 readonly IDispatcher m_dispatcher;
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
8
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
9 internal Deferred(AbstractPromise<T> promise, IDispatcher dispatcher) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
10 Debug.Assert(promise != null);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
11 m_promise = promise;
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
12 m_dispatcher = dispatcher;
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
13 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
14
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
15 public IPromise<T> Promise {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
16 get { return m_promise; }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
17 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
18
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
19 public void Reject(Exception error) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
20 m_promise.Reject(error);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
21 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
22
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
23 public void Resolve(T value) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
24 m_promise.Resolve(value);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
25 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
26
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
27 public void Resolve(IPromise<T> thenable) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
28 if (thenable == null)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
29 Reject(new Exception("The promise or task are expected"));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
30 if (thenable == m_promise)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
31 Reject(new Exception("The promise cannot be resolved with oneself"));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
32
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
33 else if (m_dispatcher != null)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
34 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
35 m_dispatcher.Enqueue(() => Chain(thenable));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
36 else
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
37 Chain(thenable);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
38 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
39
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
40 void Chain(IPromise<T> thenable) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
41 try {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
42 thenable.Then(this);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
43 } catch (Exception err) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
44 Reject(err);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
45 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
46 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
47 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
48 }