Auto status change to "Under Review"
The requested changes are too big and content was truncated. Show full diff
@@ -0,0 +1,161 | |||
|
1 | using System; | |
|
2 | using System.Diagnostics; | |
|
3 | using System.Reflection; | |
|
4 | using Implab.Parallels; | |
|
5 | ||
|
6 | namespace Implab { | |
|
7 | public class AbstractPromise<T> : AbstractEvent<IResolvable<T>>, IPromise<T> { | |
|
8 | ||
|
9 | class ResolvableSignal : IResolvable<T> { | |
|
10 | public Signal Signal { get; private set; } | |
|
11 | public ResolvableSignal() { | |
|
12 | Signal = new Signal(); | |
|
13 | } | |
|
14 | ||
|
15 | ||
|
16 | public void Reject(Exception error) { | |
|
17 | Signal.Set(); | |
|
18 | } | |
|
19 | ||
|
20 | public void Resolve(T result) { | |
|
21 | Signal.Set(); | |
|
22 | } | |
|
23 | } | |
|
24 | ||
|
25 | class ResolvableWrapper : IResolvable<T> { | |
|
26 | readonly IResolvable m_resolvable; | |
|
27 | public ResolvableWrapper(IResolvable resolvable) { | |
|
28 | ||
|
29 | } | |
|
30 | ||
|
31 | public void Reject(Exception reason) { | |
|
32 | m_resolvable.Reject(reason); | |
|
33 | } | |
|
34 | ||
|
35 | public void Resolve(T value) { | |
|
36 | m_resolvable.Resolve(); | |
|
37 | } | |
|
38 | } | |
|
39 | ||
|
40 | PromiseState m_state; | |
|
41 | ||
|
42 | T m_result; | |
|
43 | ||
|
44 | Exception m_error; | |
|
45 | ||
|
46 | public bool IsRejected { | |
|
47 | get { | |
|
48 | return m_state == PromiseState.Rejected; | |
|
49 | } | |
|
50 | } | |
|
51 | ||
|
52 | public bool IsFulfilled { | |
|
53 | get { | |
|
54 | return m_state == PromiseState.Fulfilled; | |
|
55 | } | |
|
56 | } | |
|
57 | ||
|
58 | public Exception RejectReason { | |
|
59 | get { | |
|
60 | return m_error; | |
|
61 | } | |
|
62 | } | |
|
63 | ||
|
64 | ||
|
65 | internal void Resolve(T result) { | |
|
66 | if (BeginTransit()) | |
|
67 | CompleteResolve(); | |
|
68 | } | |
|
69 | ||
|
70 | internal void Reject(Exception reason) { | |
|
71 | if (BeginTransit()) { | |
|
72 | m_error = reason; | |
|
73 | m_state = PromiseState.Rejected; | |
|
74 | CompleteTransit(); | |
|
75 | } | |
|
76 | } | |
|
77 | ||
|
78 | ||
|
79 | #region implemented abstract members of AbstractPromise | |
|
80 | ||
|
81 | protected override void SignalHandler(IResolvable<T> handler) { | |
|
82 | switch (m_state) { | |
|
83 | case PromiseState.Fulfilled: | |
|
84 | handler.Resolve(m_result); | |
|
85 | break; | |
|
86 | case PromiseState.Rejected: | |
|
87 | handler.Reject(RejectReason); | |
|
88 | break; | |
|
89 | default: | |
|
90 | throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state)); | |
|
91 | } | |
|
92 | } | |
|
93 | ||
|
94 | protected override Signal GetFulfillSignal() { | |
|
95 | var next = new ResolvableSignal(); | |
|
96 | Then(next); | |
|
97 | return next.Signal; | |
|
98 | } | |
|
99 | ||
|
100 | #endregion | |
|
101 | ||
|
102 | protected void CompleteResolve() { | |
|
103 | m_state = PromiseState.Fulfilled; | |
|
104 | CompleteTransit(); | |
|
105 | } | |
|
106 | ||
|
107 | public Type ResultType { | |
|
108 | get { | |
|
109 | return typeof(void); | |
|
110 | } | |
|
111 | } | |
|
112 | ||
|
113 | ||
|
114 | protected void Rethrow() { | |
|
115 | Debug.Assert(m_error != null); | |
|
116 | if (m_error is OperationCanceledException) | |
|
117 | throw new OperationCanceledException("Operation cancelled", m_error); | |
|
118 | else | |
|
119 | throw new TargetInvocationException(m_error); | |
|
120 | } | |
|
121 | ||
|
122 | public void Then(IResolvable<T> next) { | |
|
123 | AddHandler(next); | |
|
124 | } | |
|
125 | ||
|
126 | public void Then(IResolvable next) { | |
|
127 | AddHandler(new ResolvableWrapper(next)); | |
|
128 | } | |
|
129 | ||
|
130 | public IPromise<T2> Cast<T2>() { | |
|
131 | return (IPromise<T2>)this; | |
|
132 | } | |
|
133 | ||
|
134 | void IPromise.Join() { | |
|
135 | WaitResult(-1); | |
|
136 | if (IsRejected) | |
|
137 | Rethrow(); | |
|
138 | } | |
|
139 | ||
|
140 | void IPromise.Join(int timeout) { | |
|
141 | WaitResult(timeout); | |
|
142 | if (IsRejected) | |
|
143 | Rethrow(); | |
|
144 | } | |
|
145 | ||
|
146 | public T Join() { | |
|
147 | WaitResult(-1); | |
|
148 | if (IsRejected) | |
|
149 | Rethrow(); | |
|
150 | return m_result; | |
|
151 | } | |
|
152 | ||
|
153 | public T Join(int timeout) { | |
|
154 | WaitResult(timeout); | |
|
155 | if (IsRejected) | |
|
156 | Rethrow(); | |
|
157 | return m_result; | |
|
158 | } | |
|
159 | } | |
|
160 | } | |
|
161 |
@@ -0,0 +1,88 | |||
|
1 | using System; | |
|
2 | using System.Diagnostics; | |
|
3 | ||
|
4 | namespace Implab { | |
|
5 | class PromiseActionReaction : PromiseReaction { | |
|
6 | readonly Action m_fulfilled; | |
|
7 | ||
|
8 | readonly Action<Exception> m_rejected; | |
|
9 | ||
|
10 | readonly Deferred m_next; | |
|
11 | ||
|
12 | public PromiseActionReaction(Action fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
13 | if (fulfilled != null) | |
|
14 | m_fulfilled = () => { | |
|
15 | fulfilled(); | |
|
16 | next.Resolve(); | |
|
17 | }; | |
|
18 | ||
|
19 | if (rejected != null) | |
|
20 | m_rejected = (x) => { | |
|
21 | rejected(x); | |
|
22 | next.Resolve(); | |
|
23 | }; | |
|
24 | m_next = next; | |
|
25 | } | |
|
26 | ||
|
27 | public PromiseActionReaction(Func<IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
28 | if (fulfilled != null) | |
|
29 | m_fulfilled = () => { next.Resolve(fulfilled()); }; | |
|
30 | if (rejected != null) | |
|
31 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
32 | m_next = next; | |
|
33 | } | |
|
34 | ||
|
35 | public PromiseActionReaction(Action fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
36 | if (fulfilled != null) | |
|
37 | m_fulfilled = () => { | |
|
38 | fulfilled(); | |
|
39 | next.Resolve(); | |
|
40 | }; | |
|
41 | ||
|
42 | if (rejected != null) | |
|
43 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
44 | m_next = next; | |
|
45 | } | |
|
46 | ||
|
47 | public PromiseActionReaction(Func<IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
48 | if (fulfilled != null) | |
|
49 | m_fulfilled = () => { next.Resolve(fulfilled()); }; | |
|
50 | ||
|
51 | if (rejected != null) | |
|
52 | m_rejected = (x) => { | |
|
53 | rejected(x); | |
|
54 | next.Resolve(); | |
|
55 | }; | |
|
56 | m_next = next; | |
|
57 | } | |
|
58 | ||
|
59 | ||
|
60 | protected override bool HasFulfilHandler => m_fulfilled != null; | |
|
61 | ||
|
62 | protected override bool HasRejectHandler => m_rejected != null; | |
|
63 | ||
|
64 | protected override void DefaultReject(Exception reason) { | |
|
65 | m_next.Reject(reason); | |
|
66 | } | |
|
67 | ||
|
68 | protected override void DefaultResolve() { | |
|
69 | m_next.Resolve(); | |
|
70 | } | |
|
71 | ||
|
72 | protected override void RejectImpl(Exception reason) { | |
|
73 | try { | |
|
74 | m_rejected(reason); | |
|
75 | } catch (Exception e){ | |
|
76 | m_next.Reject(e); | |
|
77 | } | |
|
78 | } | |
|
79 | ||
|
80 | protected override void ResolveImpl() { | |
|
81 | try { | |
|
82 | m_fulfilled(); | |
|
83 | } catch (Exception e){ | |
|
84 | m_next.Reject(e); | |
|
85 | } | |
|
86 | } | |
|
87 | } | |
|
88 | } No newline at end of file |
@@ -0,0 +1,87 | |||
|
1 | using System; | |
|
2 | using System.Diagnostics; | |
|
3 | ||
|
4 | namespace Implab { | |
|
5 | class PromiseActionReaction<T> : PromiseReaction<T> { | |
|
6 | readonly Action<T> m_fulfilled; | |
|
7 | ||
|
8 | readonly Action<Exception> m_rejected; | |
|
9 | ||
|
10 | readonly Deferred m_next; | |
|
11 | ||
|
12 | public PromiseActionReaction(Action<T> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
13 | if (fulfilled != null) | |
|
14 | m_fulfilled = (x) => { | |
|
15 | fulfilled(x); | |
|
16 | next.Resolve(); | |
|
17 | }; | |
|
18 | ||
|
19 | if (rejected != null) | |
|
20 | m_rejected = (x) => { | |
|
21 | rejected(x); | |
|
22 | next.Resolve(); | |
|
23 | }; | |
|
24 | m_next = next; | |
|
25 | } | |
|
26 | ||
|
27 | public PromiseActionReaction(Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
28 | if (fulfilled != null) | |
|
29 | m_fulfilled = (x) => { next.Resolve(fulfilled(x)); }; | |
|
30 | if (rejected != null) | |
|
31 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
32 | m_next = next; | |
|
33 | } | |
|
34 | ||
|
35 | public PromiseActionReaction(Action<T> fulfilled, Func<Exception, IPromise> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
36 | if (fulfilled != null) | |
|
37 | m_fulfilled = (x) => { | |
|
38 | fulfilled(x); | |
|
39 | next.Resolve(); | |
|
40 | }; | |
|
41 | ||
|
42 | if (rejected != null) | |
|
43 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
44 | m_next = next; | |
|
45 | } | |
|
46 | ||
|
47 | public PromiseActionReaction(Func<T, IPromise> fulfilled, Action<Exception> rejected, Deferred next, IDispatcher dispatcher) : base(dispatcher) { | |
|
48 | if (fulfilled != null) | |
|
49 | m_fulfilled = (x) => { next.Resolve(fulfilled(x)); }; | |
|
50 | ||
|
51 | if (rejected != null) | |
|
52 | m_rejected = (x) => { | |
|
53 | rejected(x); | |
|
54 | next.Resolve(); | |
|
55 | }; | |
|
56 | m_next = next; | |
|
57 | } | |
|
58 | ||
|
59 | protected override bool HasFulfilHandler => m_fulfilled != null; | |
|
60 | ||
|
61 | protected override bool HasRejectHandler => m_rejected != null; | |
|
62 | ||
|
63 | protected override void DefaultReject(Exception reason) { | |
|
64 | m_next.Reject(reason); | |
|
65 | } | |
|
66 | ||
|
67 | protected override void DefaultResolve(T result) { | |
|
68 | m_next.Resolve(); | |
|
69 | } | |
|
70 | ||
|
71 | protected override void RejectImpl(Exception reason) { | |
|
72 | try { | |
|
73 | m_rejected(reason); | |
|
74 | } catch (Exception e) { | |
|
75 | m_next.Reject(e); | |
|
76 | } | |
|
77 | } | |
|
78 | ||
|
79 | protected override void ResolveImpl(T result) { | |
|
80 | try { | |
|
81 | m_fulfilled(result); | |
|
82 | } catch (Exception e) { | |
|
83 | m_next.Reject(e); | |
|
84 | } | |
|
85 | } | |
|
86 | } | |
|
87 | } No newline at end of file |
@@ -0,0 +1,76 | |||
|
1 | using System; | |
|
2 | using System.Diagnostics; | |
|
3 | ||
|
4 | namespace Implab { | |
|
5 | class PromiseFuncReaction<TRet> : PromiseReaction { | |
|
6 | readonly Action m_fulfilled; | |
|
7 | ||
|
8 | readonly Action<Exception> m_rejected; | |
|
9 | ||
|
10 | readonly Deferred<TRet> m_next; | |
|
11 | ||
|
12 | public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) { | |
|
13 | if (fulfilled != null) | |
|
14 | m_fulfilled = () => { next.Resolve(fulfilled()); }; | |
|
15 | ||
|
16 | if (rejected != null) | |
|
17 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
18 | m_next = next; | |
|
19 | } | |
|
20 | ||
|
21 | public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) { | |
|
22 | if (fulfilled != null) | |
|
23 | m_fulfilled = () => { next.Resolve(fulfilled()); }; | |
|
24 | if (rejected != null) | |
|
25 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
26 | m_next = next; | |
|
27 | } | |
|
28 | ||
|
29 | public PromiseFuncReaction(Func<TRet> fulfilled, Func<Exception, IPromise<TRet>> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) { | |
|
30 | if (fulfilled != null) | |
|
31 | m_fulfilled = () => { next.Resolve(fulfilled()); }; | |
|
32 | if (rejected != null) | |
|
33 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
34 | ||
|
35 | m_next = next; | |
|
36 | } | |
|
37 | ||
|
38 | public PromiseFuncReaction(Func<IPromise<TRet>> fulfilled, Func<Exception, TRet> rejected, Deferred<TRet> next, IDispatcher dispatcher) : base(dispatcher) { | |
|
39 | if (fulfilled != null) | |
|
40 | m_fulfilled = () => { next.Resolve(fulfilled()); }; | |
|
41 | if (rejected != null) | |
|
42 | m_rejected = (e) => { next.Resolve(rejected(e)); }; | |
|
43 | ||
|
44 | m_next = next; | |
|
45 | } | |
|
46 | ||
|
47 | ||
|
48 | protected override bool HasFulfilHandler => m_fulfilled != null; | |
|
49 | ||
|
50 | protected override bool HasRejectHandler => m_rejected != null; | |
|
51 | ||
|
52 | protected override void DefaultReject(Exception reason) { | |
|
53 | m_next.Reject(reason); | |
|
54 | } | |
|
55 | ||
|
56 | protected override void DefaultResolve() { | |
|
57 | throw new NotImplementedException(); | |
|
58 | } | |
|
59 | ||
|
60 | protected override void RejectImpl(Exception reason) { | |
|
61 | try { | |
|
62 | m_rejected(reason); | |
|
63 | } catch (Exception e){ | |
|
64 | m_next.Reject(e); | |
|
65 | } | |
|
66 | } | |
|
67 | ||
|
68 | protected override void ResolveImpl() { | |
|
69 | try { | |
|
70 | m_fulfilled(); | |
|
71 | } catch (Exception e){ | |
|
72 | m_next.Reject(e); | |
|
73 | } | |
|
74 | } | |
|
75 | } | |
|
76 | } No newline at end of file |
@@ -1,124 +1,126 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Diagnostics; |
|
3 | 3 | using System.Reflection; |
|
4 | 4 | using Implab.Parallels; |
|
5 | 5 | |
|
6 | 6 | namespace Implab { |
|
7 | 7 | public class AbstractPromise : AbstractEvent<IResolvable>, IPromise { |
|
8 | 8 | |
|
9 | 9 | class ResolvableSignal : IResolvable { |
|
10 | 10 | public Signal Signal { get; private set; } |
|
11 | 11 | public ResolvableSignal() { |
|
12 | 12 | Signal = new Signal(); |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | public void Reject(Exception error) { |
|
17 | 17 | Signal.Set(); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | public void Resolve() { |
|
21 | 21 | Signal.Set(); |
|
22 | 22 | } |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | PromiseState m_state; |
|
26 | 26 | |
|
27 | 27 | Exception m_error; |
|
28 | 28 | |
|
29 | 29 | public bool IsRejected { |
|
30 | 30 | get { |
|
31 | 31 | return m_state == PromiseState.Rejected; |
|
32 | 32 | } |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | public bool IsFulfilled { |
|
36 | 36 | get { |
|
37 | 37 | return m_state == PromiseState.Fulfilled; |
|
38 | 38 | } |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | public Exception RejectReason { |
|
42 | 42 | get { |
|
43 | 43 | return m_error; |
|
44 | 44 | } |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | internal void Resolve() { |
|
49 | 49 | if (BeginTransit()) |
|
50 | 50 | CompleteResolve(); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | internal void Reject(Exception reason) { |
|
54 | 54 | if (BeginTransit()) { |
|
55 | 55 | m_error = reason; |
|
56 | 56 | m_state = PromiseState.Rejected; |
|
57 | 57 | CompleteTransit(); |
|
58 | 58 | } |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | |
|
62 | 62 | #region implemented abstract members of AbstractPromise |
|
63 | 63 | |
|
64 | 64 | protected override void SignalHandler(IResolvable handler) { |
|
65 | 65 | switch (m_state) { |
|
66 | 66 | case PromiseState.Fulfilled: |
|
67 | 67 | handler.Resolve(); |
|
68 | 68 | break; |
|
69 | 69 | case PromiseState.Rejected: |
|
70 | 70 | handler.Reject(RejectReason); |
|
71 | 71 | break; |
|
72 | 72 | default: |
|
73 | 73 | throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", m_state)); |
|
74 | 74 | } |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | protected override Signal GetFulfillSignal() { |
|
78 | 78 | var next = new ResolvableSignal(); |
|
79 | 79 | Then(next); |
|
80 | 80 | return next.Signal; |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | #endregion |
|
84 | 84 | |
|
85 | 85 | protected void CompleteResolve() { |
|
86 | 86 | m_state = PromiseState.Fulfilled; |
|
87 | 87 | CompleteTransit(); |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | public Type ResultType { |
|
91 | 91 | get { |
|
92 | 92 | return typeof(void); |
|
93 | 93 | } |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | protected void Rethrow() { |
|
98 | 98 | Debug.Assert(m_error != null); |
|
99 | 99 | if (m_error is OperationCanceledException) |
|
100 | 100 | throw new OperationCanceledException("Operation cancelled", m_error); |
|
101 | 101 | else |
|
102 | 102 | throw new TargetInvocationException(m_error); |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | public void Then(IResolvable next) { |
|
106 | 106 | AddHandler(next); |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | public IPromise<T> Cast<T>() { |
|
110 | 110 | throw new InvalidCastException(); |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | public void Join() { |
|
114 | 114 | WaitResult(-1); |
|
115 | 115 | if (IsRejected) |
|
116 | 116 | Rethrow(); |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | public void Join(int timeout) { |
|
120 | 120 | WaitResult(timeout); |
|
121 | if (IsRejected) | |
|
122 | Rethrow(); | |
|
121 | 123 | } |
|
122 | 124 | } |
|
123 | 125 | } |
|
124 | 126 |
@@ -1,41 +1,45 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab { |
|
7 | 7 | public interface IPromise { |
|
8 | 8 | |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// Тип результата, получаемого через данное обещание. |
|
11 | 11 | /// </summary> |
|
12 | 12 | Type ResultType { get; } |
|
13 | 13 | |
|
14 | 14 | /// <summary> |
|
15 | 15 | /// Обещание является выполненым, либо успешно, либо с ошибкой, либо отменено. |
|
16 | 16 | /// </summary> |
|
17 | 17 | bool IsResolved { get; } |
|
18 | 18 | |
|
19 | 19 | bool IsRejected { get; } |
|
20 | 20 | |
|
21 | 21 | bool IsFulfilled { get; } |
|
22 | 22 | |
|
23 | 23 | /// <summary> |
|
24 | 24 | /// Исключение возникшее в результате выполнения обещания, либо причина отмены. |
|
25 | 25 | /// </summary> |
|
26 | 26 | Exception RejectReason { get; } |
|
27 | 27 | |
|
28 | 28 | /// <summary> |
|
29 | 29 | /// Adds specified listeners to the current promise. |
|
30 | 30 | /// </summary> |
|
31 | 31 | /// <param name="success">The handler called on the successful promise completion.</param> |
|
32 | 32 | /// <param name="error">The handler is called if an error while completing the promise occurred.</param> |
|
33 | 33 | /// <returns>The current promise.</returns> |
|
34 | 34 | void Then(IResolvable next); |
|
35 | 35 | |
|
36 | 36 | /// <summary> |
|
37 | 37 | /// Преобразует результат обещания к заданному типу и возвращает новое обещание. |
|
38 | 38 | /// </summary> |
|
39 | 39 | IPromise<T> Cast<T>(); |
|
40 | ||
|
41 | void Join(); | |
|
42 | ||
|
43 | void Join(int timeout); | |
|
40 | 44 | } |
|
41 | 45 | } |
@@ -1,12 +1,12 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab { |
|
4 | 4 | public interface IPromise<out T> : IPromise { |
|
5 | 5 | |
|
6 | void On(Action<T> success, Action<Exception> error); | |
|
6 | void Then(IResolvable<T> next); | |
|
7 | 7 | |
|
8 | 8 | new T Join(); |
|
9 | 9 | |
|
10 | 10 | new T Join(int timeout); |
|
11 | 11 | } |
|
12 | 12 | } |
@@ -1,11 +1,11 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab { |
|
4 | 4 | |
|
5 | public interface IResolvable<T> { | |
|
5 | public interface IResolvable<in T> { | |
|
6 | 6 | void Resolve(T value); |
|
7 | 7 | |
|
8 | 8 | void Reject(Exception reason); |
|
9 | 9 | |
|
10 | 10 | } |
|
11 | 11 | } No newline at end of file |
@@ -1,35 +1,45 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab { |
|
4 |
|
|
|
5 | IDispatcher m_dispatcher; | |
|
4 | abstract class PromiseReaction : IResolvable { | |
|
5 | readonly IDispatcher m_dispatcher; | |
|
6 | ||
|
7 | protected PromiseReaction(IDispatcher dispatcher) { | |
|
8 | m_dispatcher = dispatcher; | |
|
9 | } | |
|
6 | 10 | |
|
7 | Action m_onFulfilledJob; | |
|
11 | protected abstract bool HasFulfilHandler { | |
|
12 | get; | |
|
13 | } | |
|
8 | 14 | |
|
9 | Action<Exception> m_onRejectedJob; | |
|
10 | ||
|
11 | IResolvable m_next; | |
|
15 | protected abstract bool HasRejectHandler { | |
|
16 | get; | |
|
17 | } | |
|
12 | 18 | |
|
13 | 19 | public void Reject(Exception error) { |
|
14 |
if ( |
|
|
15 | if (m_dispatcher != null) | |
|
16 | m_dispatcher.Enqueue(() => m_onRejectedJob(error)); | |
|
20 | if (!HasRejectHandler) | |
|
21 | DefaultReject(error); | |
|
22 | else if (m_dispatcher != null) | |
|
23 | m_dispatcher.Enqueue(() => RejectImpl(error)); | |
|
17 | 24 |
|
|
18 |
|
|
|
19 | } else { | |
|
20 | m_next.Reject(error); | |
|
21 | } | |
|
25 | RejectImpl(error); | |
|
22 | 26 | } |
|
23 | 27 | |
|
24 | 28 | public void Resolve() { |
|
25 | if (m_onRejectedJob != null) { | |
|
26 | if (m_dispatcher != null) | |
|
27 |
|
|
|
29 | if (!HasFulfilHandler) | |
|
30 | DefaultResolve(); | |
|
31 | else if (m_dispatcher != null) | |
|
32 | m_dispatcher.Enqueue(ResolveImpl); | |
|
28 | 33 |
|
|
29 |
|
|
|
30 | } else { | |
|
31 | m_next.Resolve(); | |
|
34 | ResolveImpl(); | |
|
32 | 35 |
|
|
33 | } | |
|
36 | ||
|
37 | protected abstract void ResolveImpl(); | |
|
38 | ||
|
39 | protected abstract void RejectImpl(Exception reason); | |
|
40 | ||
|
41 | protected abstract void DefaultResolve(); | |
|
42 | ||
|
43 | protected abstract void DefaultReject(Exception reason); | |
|
34 | 44 | } |
|
35 | 45 | } No newline at end of file |
@@ -1,25 +1,45 | |||
|
1 | 1 | using System; |
|
2 | 2 | |
|
3 | 3 | namespace Implab { |
|
4 |
|
|
|
5 | IDispatcher m_dispatcher; | |
|
4 | abstract class PromiseReaction<T> : IResolvable<T> { | |
|
5 | readonly IDispatcher m_dispatcher; | |
|
6 | ||
|
7 | protected PromiseReaction(IDispatcher dispatcher) { | |
|
8 | m_dispatcher = dispatcher; | |
|
9 | } | |
|
6 | 10 | |
|
7 | Action<T> m_onFulfilledJob; | |
|
11 | protected abstract bool HasFulfilHandler { | |
|
12 | get; | |
|
13 | } | |
|
8 | 14 | |
|
9 | Action<Exception> m_onRejectedJob; | |
|
15 | protected abstract bool HasRejectHandler { | |
|
16 | get; | |
|
17 | } | |
|
10 | 18 | |
|
11 | 19 | public void Reject(Exception error) { |
|
12 | if (m_dispatcher != null) | |
|
13 |
|
|
|
20 | if (!HasRejectHandler) | |
|
21 | DefaultReject(error); | |
|
22 | else if (m_dispatcher != null) | |
|
23 | m_dispatcher.Enqueue(() => RejectImpl(error)); | |
|
14 | 24 | else |
|
15 |
|
|
|
25 | RejectImpl(error); | |
|
16 | 26 | } |
|
17 | 27 | |
|
18 | 28 | public void Resolve(T result) { |
|
19 | if (m_dispatcher != null) | |
|
20 | m_dispatcher.Enqueue(() => m_onFulfilledJob(result)); | |
|
29 | if (!HasFulfilHandler) | |
|
30 | DefaultResolve(result); | |
|
31 | else if (m_dispatcher != null) | |
|
32 | m_dispatcher.Enqueue(() => ResolveImpl(result)); | |
|
21 | 33 | else |
|
22 |
|
|
|
34 | ResolveImpl(result); | |
|
23 | 35 | } |
|
36 | ||
|
37 | protected abstract void ResolveImpl(T result); | |
|
38 | ||
|
39 | protected abstract void RejectImpl(Exception reason); | |
|
40 | ||
|
41 | protected abstract void DefaultResolve(T result); | |
|
42 | ||
|
43 | protected abstract void DefaultReject(Exception reason); | |
|
24 | 44 | } |
|
25 | 45 | } No newline at end of file |
@@ -1,22 +1,22 | |||
|
1 | Copyright 2012 Sergey Smirnov | |
|
1 | Copyright 2012-2018 Sergey Smirnov | |
|
2 | 2 | |
|
3 | 3 | Redistribution and use in source and binary forms, with or without |
|
4 | 4 | modification, are permitted provided that the following conditions are met: |
|
5 | 5 | |
|
6 | 6 | 1. Redistributions of source code must retain the above copyright notice, this |
|
7 | 7 | list of conditions and the following disclaimer. |
|
8 | 8 | |
|
9 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, |
|
10 | 10 | this list of conditions and the following disclaimer in the documentation |
|
11 | 11 | and/or other materials provided with the distribution. |
|
12 | 12 | |
|
13 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
14 | 14 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
15 | 15 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
16 | 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
|
17 | 17 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
18 | 18 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
19 | 19 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
20 | 20 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
21 | 21 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
22 | 22 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. No newline at end of file |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed, binary diff hidden |
|
1 | NO CONTENT: file was removed | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now