annotate Implab/Promise.cs @ 130:671f60cd0250 v2

fixed Resove method bug when calling it on already cancelled promise
author cin
date Fri, 30 Jan 2015 17:07:17 +0300
parents 2573b562e328
children f75cfa58e3d4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
1 using System;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
2 using System.Diagnostics;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
3
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
4 namespace Implab {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
5 public class Promise : AbstractPromise<Promise.HandlerDescriptor>, IPromise, IDeferred {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
6
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
7 public struct HandlerDescriptor {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
8 readonly Action m_success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
9 readonly Action<Exception> m_error;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
10 readonly Action m_cancel;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
11 readonly IDeferred m_deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
12
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
13 public HandlerDescriptor(Action success, Action<Exception> error, Action cancel, IDeferred deferred) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
14 m_success = success;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
15 m_error = error;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
16 m_cancel = cancel;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
17 m_deferred = deferred;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
18 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
19
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
20 public void SignalSuccess() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
21 if (m_success != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
22 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
23 m_success();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
24 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
25 m_deferred.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
26 } catch (Exception err) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
27 SignalError(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
28 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
29 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
30 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
31
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
32 public void SignalError(Exception err) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
33 if (m_error != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
34 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
35 m_error(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
36 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
37 m_deferred.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
38 } catch (Exception err2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
39 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
40 m_deferred.Reject(err2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
41 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
42 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
43 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
44 m_deferred.Reject(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
45 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
46 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
47
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
48 public void SignalCancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
49 if (m_cancel != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
50 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
51 m_cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
52 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
53 m_deferred.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
54 } catch (Exception err) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
55 SignalError(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
56 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
57 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
58 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
59 m_deferred.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
60 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
61 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
62 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
63
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
64 public void Resolve() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
65 BeginSetResult();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
66 EndSetResult();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
67 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
68
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
69 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
70 SetError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
71 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
72
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
73 #region implemented abstract members of AbstractPromise
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
74
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
75 protected override void SignalSuccess(HandlerDescriptor handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
76 handler.SignalSuccess();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
77 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
78
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
79 protected override void SignalError(HandlerDescriptor handler, Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
80 handler.SignalError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
81 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
82
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
83 protected override void SignalCancelled(HandlerDescriptor handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
84 handler.SignalCancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
85 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
86
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
87 protected override void Listen(PromiseEventType events, Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
88 AddHandler(new HandlerDescriptor(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
89 events.HasFlag(PromiseEventType.Success) ? handler : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
90 events.HasFlag(PromiseEventType.Error) ? new Action<Exception>(err => handler()) : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
91 events.HasFlag(PromiseEventType.Cancelled) ? handler : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
92 null
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
93 ));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
94 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
95
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
96 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
97
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
98
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
99 public Type PromiseType {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
100 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
101 return typeof(void);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
102 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
103 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
104
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
105 public IPromise Then(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
106 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
107 if (success != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
108 promise.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
109
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
110 AddHandler(new HandlerDescriptor(success, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
111
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
112 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
113 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
114
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
115 public IPromise Then(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
116 return Then(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
117 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
118
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
119 public IPromise Then(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
120 return Then(success, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
121 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
122
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
123 public IPromise On(Action success, Action<Exception> error, Action cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
124 AddHandler(new HandlerDescriptor(success, error, cancel, null));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
125 return this;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
126 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
127
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
128 public IPromise On(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
129 return On(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
130 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
131
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
132 public IPromise On(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
133 return On(success, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
134 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
135
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
136 public IPromise On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
137 return On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
138 events.HasFlag(PromiseEventType.Success) ? handler : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
139 events.HasFlag(PromiseEventType.Error) ? new Action<Exception>(err => handler()) : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
140 events.HasFlag(PromiseEventType.Cancelled) ? handler : null
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
141 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
142 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
143
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
144 public IPromise<T> Cast<T>() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
145 throw new InvalidCastException();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
146 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
147
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
148 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error, Func<IPromise> cancel) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
149 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
150
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
151 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
152 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
153 if (medium.IsCancelled)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
154 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
155 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
156 ConnectPromise(chained(), medium);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
157 },
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
158 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
159 if (medium.IsCancelled)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
160 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
161 if (error != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
162 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
163 ConnectPromise(error(ex), medium);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
164 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
165 medium.Reject(ex2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
166 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
167 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
168 medium.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
169 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
170 },
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
171 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
172 if (medium.IsCancelled)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
173 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
174 if (cancel != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
175 ConnectPromise(cancel(), medium);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
176 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
177 medium.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
178 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
179 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
180
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
181 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
182 medium.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
183
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
184 return medium;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
185 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
186
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
187 static void ConnectPromise(IPromise result, Promise medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
188 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
189 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
190 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
191 medium.Reject,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
192 () => medium.Reject(new OperationCanceledException())
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
193 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
194 medium.On(result.Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
195 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
196 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
197 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
198 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
199 " 'null' where the promise instance is expected"
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
200 )
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
201 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
202 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
203 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
204
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
205 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
206 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
207 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
208
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
209 public IPromise Chain(Func<IPromise> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
210 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
211 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
212
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
213 public IPromise Error(Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
214 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
215 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
216 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
217 err => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
218 if (error != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
219 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
220 error(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
221 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
222 } catch (Exception err2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
223 promise.Reject(err2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
224 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
225 else
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
226 promise.Reject(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
227 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
228 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
229
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
230 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
231 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
232
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
233 public IPromise Cancelled(Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
234 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
235 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
236 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
237 null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
238 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
239 if (handler != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
240 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
241 handler();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
242 promise.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
243 } catch (Exception err) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
244 promise.Reject(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
245 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
246 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
247 promise.Cancel();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
248 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
249 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
250 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
251
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
252 return promise;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
253 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
254
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
255
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
256 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
257 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
258