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