annotate Implab/Deferred.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
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
1 using System;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
2 using System.Diagnostics;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
3
eee3e49dd1ff working on promises
cin
parents:
diff changeset
4 namespace Implab {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
5 /// <summary>
eee3e49dd1ff working on promises
cin
parents:
diff changeset
6 /// This class is responsible for the promise resolution, dispatching and chaining
eee3e49dd1ff working on promises
cin
parents:
diff changeset
7 /// </summary>
eee3e49dd1ff working on promises
cin
parents:
diff changeset
8 public class Deferred : IResolvable {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
9
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
10 readonly Promise m_promise;
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
11 internal Deferred() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
12 m_promise = new Promise();
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
13 }
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
14
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
15 internal Deferred(Promise promise, IDispatcher dispatcher) {
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
16 Debug.Assert(promise != null);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
17 m_promise = promise;
eee3e49dd1ff working on promises
cin
parents:
diff changeset
18 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
19
eee3e49dd1ff working on promises
cin
parents:
diff changeset
20 public IPromise Promise {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
21 get { return m_promise; }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
22 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
23
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
24 public void Cancel() {
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
25 Reject(new OperationCanceledException());
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
26 }
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
27
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
28 public virtual void Reject(Exception error) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
29 if (error is PromiseTransientException)
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
30 error = ((PromiseTransientException)error).InnerException;
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
31
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
32 m_promise.RejectPromise(error);
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
33 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
34
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
35 public virtual void Resolve() {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 245
diff changeset
36 m_promise.ResolvePromise();
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
37 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
38
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
39 public virtual void Resolve(IPromise thenable) {
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
40 if (thenable == null)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
41 Reject(new Exception("The promise or task are expected"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
42 if (thenable == m_promise)
eee3e49dd1ff working on promises
cin
parents:
diff changeset
43 Reject(new Exception("The promise cannot be resolved with oneself"));
eee3e49dd1ff working on promises
cin
parents:
diff changeset
44
eee3e49dd1ff working on promises
cin
parents:
diff changeset
45 try {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
46 thenable.Then(this);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
47 } catch (Exception err) {
eee3e49dd1ff working on promises
cin
parents:
diff changeset
48 Reject(err);
eee3e49dd1ff working on promises
cin
parents:
diff changeset
49 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
50 }
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
51
244
eee3e49dd1ff working on promises
cin
parents:
diff changeset
52 }
eee3e49dd1ff working on promises
cin
parents:
diff changeset
53 }