comparison Implab/Deferred`1.cs @ 246:5aa9cfbe56c3 v3

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