annotate Implab/Promise.cs @ 138:f75cfa58e3d4 v2

added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
author cin
date Tue, 17 Feb 2015 18:16:26 +0300
parents 2573b562e328
children 8c0b95069066
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;
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
10 readonly Action<Exception> m_cancel;
119
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
13 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel, IDeferred deferred) {
119
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
48 public void SignalCancel(Exception reason) {
119
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 {
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
51 m_cancel(reason);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
52 if (m_deferred != null)
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
53 m_deferred.Resolve();
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
54 } catch (Exception err) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
55 SignalError(err);
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
56 }
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
57 } else if (reason != null && m_error != null) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
58 try {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
59 m_error(new OperationCanceledException("The operation was canceled.", reason));
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
60 if (m_deferred != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
61 m_deferred.Resolve();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
62 } catch (Exception err) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
63 SignalError(err);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
64 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
65 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
66 if (m_deferred != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
67 m_deferred.Cancel(reason);
119
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
70 }
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 public void Resolve() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
73 BeginSetResult();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
74 EndSetResult();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
75 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
76
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
77 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
78 SetError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
79 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
80
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
81 #region implemented abstract members of AbstractPromise
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 SignalSuccess(HandlerDescriptor handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
84 handler.SignalSuccess();
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 SignalError(HandlerDescriptor handler, Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
88 handler.SignalError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
89 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
90
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
91 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) {
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
92 handler.SignalCancel(reason);
119
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 protected override void Listen(PromiseEventType events, Action handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
96 AddHandler(new HandlerDescriptor(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
97 events.HasFlag(PromiseEventType.Success) ? handler : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
98 events.HasFlag(PromiseEventType.Error) ? new Action<Exception>(err => handler()) : null,
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
99 events.HasFlag(PromiseEventType.Cancelled) ? new Action<Exception>(reason => handler()) : null,
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
100 null
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
101 ));
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 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
105
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
106
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
107 public Type PromiseType {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
108 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
109 return typeof(void);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
110 }
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
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
113 public IPromise Then(Action success, Action<Exception> error, Action<Exception> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
114 var promise = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
115 if (success != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
116 promise.On(Cancel, PromiseEventType.Cancelled);
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 AddHandler(new HandlerDescriptor(success, error, cancel, promise));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
120 return promise;
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 Then(Action success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
124 return Then(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
125 }
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 public IPromise Then(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
128 return Then(success, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
129 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
130
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
131 public IPromise On(Action success, Action<Exception> error, Action<Exception> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
132 AddHandler(new HandlerDescriptor(success, error, cancel, null));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
133 return this;
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 success, Action<Exception> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
137 return On(success, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
138 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
139
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
140 public IPromise On(Action success) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
141 return On(success, null, null);
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 On(Action handler, PromiseEventType events) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
145 return On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
146 events.HasFlag(PromiseEventType.Success) ? handler : null,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
147 events.HasFlag(PromiseEventType.Error) ? new Action<Exception>(err => handler()) : null,
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
148 events.HasFlag(PromiseEventType.Cancelled) ? new Action<Exception>(reason => handler()) : null
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
149 );
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
152 public IPromise<T> Cast<T>() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
153 throw new InvalidCastException();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
154 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
155
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
156 public IPromise Chain(Func<IPromise> chained, Func<Exception, IPromise> error, Func<Exception,IPromise> cancel) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
157 var medium = new Promise();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
158
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
159 On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
160 () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
161 if (medium.IsCancelled)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
162 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
163 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
164 ConnectPromise(chained(), medium);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
165 },
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
166 ex => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
167 if (medium.IsCancelled)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
168 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
169 if (error != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
170 try {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
171 ConnectPromise(error(ex), medium);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
172 } catch (Exception ex2) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
173 medium.Reject(ex2);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
174 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
175 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
176 medium.Reject(ex);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
177 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
178 },
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
179 reason => {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
180 if (medium.IsCancelled)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
181 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
182 if (cancel != null)
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
183 ConnectPromise(cancel(reason), medium);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
184 else
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
185 medium.Cancel(reason);
119
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 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
188
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
189 if (chained != null)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
190 medium.On(Cancel, PromiseEventType.Cancelled);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
191
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
192 return medium;
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
195 static void ConnectPromise(IPromise result, Promise medium) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
196 if (result != null) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
197 result.On(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
198 medium.Resolve,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
199 medium.Reject,
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
200 medium.Cancel
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
201 );
138
f75cfa58e3d4 added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
cin
parents: 119
diff changeset
202 medium.On(null,null,result.Cancel);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
203 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
204 medium.Reject(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
205 new NullReferenceException(
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
206 "The chained asynchronous operation returned" +
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
207 " 'null' where the promise instance is expected"
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 );
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
210 }
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 Chain(Func<IPromise> chained, Func<Exception, IPromise> error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
214 return Chain(chained, error, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
215 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
216
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
217 public IPromise Chain(Func<IPromise> chained) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
218 return Chain(chained, null, null);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
219 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
220 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
221 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 112
diff changeset
222