annotate Implab/Deferred`1.cs @ 249:d82909310094 v3

Implab.Test moved to xunit Complete set of PromiseHelpers (Then, Catch, Finally) Removed obsolete types ICancellable, ICancellationToken
author cin
date Wed, 31 Jan 2018 11:28:38 +0300
parents 5cb4826c2c2a
children 7c7e9ad6fe4a
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
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
8 internal Deferred() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
9 m_promise = new Promise<T>();
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
10 }
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
11
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
12 protected Deferred(Promise<T> promise) {
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 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
16
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
17 public IPromise<T> Promise {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
18 get { return m_promise; }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
19 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
20
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
21 public void Cancel() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
22 Reject(new OperationCanceledException());
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
23 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
24
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
25 public virtual void Reject(Exception error) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
26 if (error is PromiseTransientException)
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
27 error = ((PromiseTransientException)error).InnerException;
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
28
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
29 m_promise.RejectPromise(error);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
30 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
31
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
32 public virtual void Resolve(T value) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
33 m_promise.ResolvePromise(value);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
34 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
35
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
36 public virtual void Resolve(IPromise<T> thenable) {
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
37 if (thenable == null)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
38 Reject(new Exception("The promise or task are expected"));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
39 if (thenable == m_promise)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
40 Reject(new Exception("The promise cannot be resolved with oneself"));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
41
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
42 try {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
43 thenable.Then(this);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
44 } catch (Exception err) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
45 Reject(err);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
46 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
47 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
48 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
49 }