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

PollingComponent: implemented correct stopping
author cin
date Fri, 13 Apr 2018 03:57:39 +0300
parents 7c7e9ad6fe4a
children 547a2fc0d93e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
1 using System;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
2 using System.Diagnostics;
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
3 using System.Threading.Tasks;
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
4
eee3e49dd1ff working on promises
cin
parents:
diff changeset
5 namespace Implab {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
6 /// <summary>
eee3e49dd1ff working on promises
cin
parents:
diff changeset
7 /// This class is responsible for the promise resolution, dispatching and chaining
eee3e49dd1ff working on promises
cin
parents:
diff changeset
8 /// </summary>
eee3e49dd1ff working on promises
cin
parents:
diff changeset
9 public class Deferred : IResolvable {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
10
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
11 readonly Promise m_promise;
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
12 internal Deferred() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
13 m_promise = new Promise();
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
14 }
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
15
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
16 internal Deferred(Promise promise, IDispatcher dispatcher) {
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
17 Debug.Assert(promise != null);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
18 m_promise = promise;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
19 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
20
eee3e49dd1ff working on promises
cin
parents:
diff changeset
21 public IPromise Promise {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
22 get { return m_promise; }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
23 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
24
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
25 public void Cancel() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
26 Reject(new OperationCanceledException());
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
27 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
28
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
29 public virtual void Reject(Exception error) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
30 if (error is PromiseTransientException)
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
31 error = ((PromiseTransientException)error).InnerException;
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
32
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
33 m_promise.RejectPromise(error);
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
34 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
35
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
36 public virtual void Resolve() {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
37 m_promise.ResolvePromise();
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
38 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
39
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
40 public virtual void Resolve(IPromise thenable) {
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
41 if (thenable == null)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
42 Reject(new Exception("The promise or task are expected"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
43 if (thenable == m_promise)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
44 Reject(new Exception("The promise cannot be resolved with oneself"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
45
eee3e49dd1ff working on promises
cin
parents:
diff changeset
46 try {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
47 thenable.Then(this);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
48 } catch (Exception err) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
49 Reject(err);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
50 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
51 }
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
52
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
53 public virtual void Resolve(Task thenable) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
54 if (thenable == null)
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
55 Reject(new Exception("The promise or task are expected"));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
56 try {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
57 thenable.Then(this);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
58 } catch(Exception err) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
59 Reject(err);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
60 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
61 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 249
diff changeset
62
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
63 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
64 }