##// END OF EJS Templates
refactoring
cin -
r26:f0bf98e4d22c default
parent child
Show More
@@ -8,9 +8,25 namespace Implab
8 8 public interface IPromise<T>: IPromiseBase
9 9 {
10 10
11 T Join();
11 12
13 T Join(int timeout);
14
15 IPromise<T> Then(ResultHandler<T> success, ErrorHandler error);
16 IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error);
17 IPromise<T> Then(ResultHandler<T> success);
18 IPromise<T> Error(ErrorHandler error);
19 IPromise<T> Error(ErrorHandler<T> error);
12 20
21 IPromise<T2> Map<T2>(ResultMapper<T,T2> mapper, ErrorHandler error);
22 IPromise<T2> Map<T2>(ResultMapper<T, T2> mapper);
13 23
24 IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained, ErrorHandler error);
25 IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained);
26
27 IPromise<T> Cancelled(Action handler);
28 IPromise<T> Finally(Action handler);
29 IPromise<T> Anyway(Action handler);
14 30
15 31 }
16 32 }
@@ -15,5 +15,9 namespace Implab {
15 15 bool IsResolved { get; }
16 16
17 17 bool IsCancelled { get; }
18
19 IPromiseBase Then(Action success,ErrorHandler error);
20 IPromiseBase Then(Action success);
21
18 22 }
19 23 }
@@ -125,7 +125,7 namespace Implab.Parallels {
125 125 return iter.Promise;
126 126 }
127 127
128 public static Promise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) {
128 public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) {
129 129 if (source == null)
130 130 throw new ArgumentNullException("source");
131 131 if (transform == null)
@@ -191,7 +191,7 namespace Implab {
191 191 /// This handler will recieve an operation result as a parameter.</param>
192 192 /// <param name="error">Handles an exception that may occur during the operation.</param>
193 193 /// <returns>The new promise chained to this one.</returns>
194 public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) {
194 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler error) {
195 195 if (success == null && error == null)
196 196 return this;
197 197
@@ -225,6 +225,16 namespace Implab {
225 225 return medium;
226 226 }
227 227
228 public IPromiseBase Then(Action success,ErrorHandler error)
229 {
230 return Then(x => success(), error);
231 }
232
233 public IPromiseBase Then(Action success)
234 {
235 return Then(success);
236 }
237
228 238 /// <summary>
229 239 /// Adds new handlers to this promise.
230 240 /// </summary>
@@ -232,7 +242,7 namespace Implab {
232 242 /// This handler will recieve an operation result as a parameter.</param>
233 243 /// <param name="error">Handles an exception that may occur during the operation and returns the value which will be used as the result of the operation.</param>
234 244 /// <returns>The new promise chained to this one.</returns>
235 public Promise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
245 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
236 246 if (success == null && error == null)
237 247 return this;
238 248
@@ -266,7 +276,7 namespace Implab {
266 276 }
267 277
268 278
269 public Promise<T> Then(ResultHandler<T> success) {
279 public IPromise<T> Then(ResultHandler<T> success) {
270 280 if (success == null)
271 281 return this;
272 282
@@ -287,8 +297,8 namespace Implab {
287 297 return medium;
288 298 }
289 299
290 public Promise<T> Error(ErrorHandler error) {
291 return Then(null, error);
300 public IPromise<T> Error(ErrorHandler error) {
301 return Then((ResultHandler<T>)null, error);
292 302 }
293 303
294 304 /// <summary>
@@ -299,7 +309,7 namespace Implab {
299 309 /// </remarks>
300 310 /// <param name="handler">The error handler which returns the result of the promise.</param>
301 311 /// <returns>New promise.</returns>
302 public Promise<T> Error(ErrorHandler<T> handler) {
312 public IPromise<T> Error(ErrorHandler<T> handler) {
303 313 if (handler == null)
304 314 return this;
305 315
@@ -320,7 +330,7 namespace Implab {
320 330 return medium;
321 331 }
322 332
323 public Promise<T> Anyway(Action handler) {
333 public IPromise<T> Anyway(Action handler) {
324 334 if (handler == null)
325 335 return this;
326 336
@@ -358,7 +368,7 namespace Implab {
358 368 /// <param name="error">Обработчик ошибки. Данный обработчик получит
359 369 /// исключение возникшее при выполнении операции.</param>
360 370 /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns>
361 public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) {
371 public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) {
362 372 if (mapper == null)
363 373 throw new ArgumentNullException("mapper");
364 374
@@ -385,7 +395,7 namespace Implab {
385 395 return chained;
386 396 }
387 397
388 public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
398 public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) {
389 399 return Map(mapper, null);
390 400 }
391 401
@@ -399,7 +409,7 namespace Implab {
399 409 /// <param name="error">Обработчик ошибки. Данный обработчик получит
400 410 /// исключение возникшее при выполнении текуещй операции.</param>
401 411 /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns>
402 public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) {
412 public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) {
403 413
404 414 // проблема в том, что на момент связывания еще не начата асинхронная операция, поэтому нужно
405 415 // создать посредника, к которому будут подвызяваться следующие обработчики.
@@ -437,11 +447,11 namespace Implab {
437 447 return medium;
438 448 }
439 449
440 public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
450 public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) {
441 451 return Chain(chained, null);
442 452 }
443 453
444 public Promise<T> Cancelled(Action handler) {
454 public IPromise<T> Cancelled(Action handler) {
445 455 AddHandler(null, null, handler);
446 456 return this;
447 457 }
@@ -451,7 +461,7 namespace Implab {
451 461 /// </summary>
452 462 /// <param name="handler">The handler that will be called anyway</param>
453 463 /// <returns>self</returns>
454 public Promise<T> Finally(Action handler) {
464 public IPromise<T> Finally(Action handler) {
455 465 if (handler == null)
456 466 throw new ArgumentNullException("handler");
457 467 AddHandler(
General Comments 0
You need to be logged in to leave comments. Login now