annotate Implab/Deferred`1.cs @ 259:7d52dc684bbd v3

PollingComponent: implemented correct stopping
author cin
date Fri, 13 Apr 2018 03:57:39 +0300
parents 7c7e9ad6fe4a
children
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;
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
3 using System.Threading.Tasks;
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
4
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
5 namespace Implab {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
6 public class Deferred<T> : IResolvable<T> {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
7 readonly Promise<T> m_promise;
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
8
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
9 internal Deferred() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
10 m_promise = new Promise<T>();
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
11 }
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
12
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
13 protected Deferred(Promise<T> promise) {
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
14 Debug.Assert(promise != null);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
15 m_promise = promise;
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
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
22 public void Cancel() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
23 Reject(new OperationCanceledException());
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
24 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
25
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
26 public virtual void Reject(Exception error) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
27 if (error is PromiseTransientException)
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
28 error = ((PromiseTransientException)error).InnerException;
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
29
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
30 m_promise.RejectPromise(error);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
31 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
32
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
33 public virtual void Resolve(T value) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 246
diff changeset
34 m_promise.ResolvePromise(value);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
35 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
36
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
37 public virtual void Resolve(IPromise<T> thenable) {
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
38 if (thenable == null)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
39 Reject(new Exception("The promise or task are expected"));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
40 if (thenable == m_promise)
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
41 Reject(new Exception("The promise cannot be resolved with oneself"));
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
42
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
43 try {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
44 thenable.Then(this);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
45 } catch (Exception err) {
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
46 Reject(err);
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
47 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
48 }
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
49
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
50 public virtual void Resolve(Task<T> thenable) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
51 if (thenable == null)
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
52 Reject(new Exception("The promise or task are expected"));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
53
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
54 try {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
55 thenable.Then(this);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
56 } catch (Exception err) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
57 Reject(err);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
58 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
59 }
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
60 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
61 }