##// END OF EJS Templates
working on promises
cin -
r245:b904e0a3ba72 v3
parent child
Show More
@@ -1,52 +1,52
1 1 using System;
2 2 using System.Diagnostics;
3 3
4 4 namespace Implab {
5 5 /// <summary>
6 6 /// This class is responsible for the promise resolution, dispatching and chaining
7 7 /// </summary>
8 8 public class Deferred : IResolvable {
9 9
10 10 readonly AbstractPromise m_promise;
11 11 readonly IDispatcher m_dispatcher;
12 12
13 13 internal Deferred(AbstractPromise promise, IDispatcher dispatcher) {
14 14 Debug.Assert(promise != null);
15 15 m_promise = promise;
16 16 m_dispatcher = dispatcher;
17 17 }
18 18
19 19 public IPromise Promise {
20 20 get { return m_promise; }
21 21 }
22 22
23 23 public void Reject(Exception error) {
24 24 m_promise.Reject(error);
25 25 }
26 26
27 27 public void Resolve() {
28 28 m_promise.Resolve();
29 29 }
30 30
31 31 public void Resolve(IPromise thenable) {
32 32 if (thenable == null)
33 33 Reject(new Exception("The promise or task are expected"));
34 34 if (thenable == m_promise)
35 35 Reject(new Exception("The promise cannot be resolved with oneself"));
36 36
37 37 else if (m_dispatcher != null)
38 38 // dispatch (see ecma-262/6.0: 25.4.1.3.2 Promise Resolve Functions)
39 m_dispatcher.Enqueue(() => thenable.Then(this));
39 m_dispatcher.Enqueue(() => Chain(thenable));
40 40 else
41 thenable.Then(this);
41 Chain(thenable);
42 42 }
43 43
44 44 void Chain(IPromise thenable) {
45 45 try {
46 46 thenable.Then(this);
47 47 } catch (Exception err) {
48 48 Reject(err);
49 49 }
50 50 }
51 51 }
52 52 } No newline at end of file
@@ -1,26 +1,24
1 1 using System;
2 2
3 3 namespace Implab {
4 4 /// <summary>
5 5 /// Deferred result, usually used by asynchronous services as the service part of the promise.
6 6 /// </summary>
7 7 public interface IResolvable {
8 8
9 9 void Resolve();
10 10
11 void Resolve(IPromise thenable);
12
13 11 /// <summary>
14 12 /// Reject the promise with the specified error.
15 13 /// </summary>
16 14 /// <param name="error">The reason why the promise is rejected.</param>
17 15 /// <remarks>
18 16 /// Some exceptions are treated in a special case:
19 17 /// <see cref="OperationCanceledException"/> is interpreted as call to <see cref="Cancel()"/> method,
20 18 /// and <see cref="PromiseTransientException"/> is always unwrapped and its
21 19 /// <see cref="PromiseTransientException.InnerException"> is used as the reason to reject promise.
22 20 /// </remarks>
23 21 void Reject(Exception error);
24 22 }
25 23 }
26 24
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now