Mercurial > pub > ImplabNet
comparison Implab/Promise.cs @ 26:f0bf98e4d22c
refactoring
| author | cin |
|---|---|
| date | Fri, 21 Feb 2014 03:15:28 +0400 |
| parents | 9bf5b23650c9 |
| children | a236cd1f0477 |
comparison
equal
deleted
inserted
replaced
| 25:9bf5b23650c9 | 26:f0bf98e4d22c |
|---|---|
| 189 /// </summary> | 189 /// </summary> |
| 190 /// <param name="success">The handler of the successfully completed operation. | 190 /// <param name="success">The handler of the successfully completed operation. |
| 191 /// This handler will recieve an operation result as a parameter.</param> | 191 /// This handler will recieve an operation result as a parameter.</param> |
| 192 /// <param name="error">Handles an exception that may occur during the operation.</param> | 192 /// <param name="error">Handles an exception that may occur during the operation.</param> |
| 193 /// <returns>The new promise chained to this one.</returns> | 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 if (success == null && error == null) | 195 if (success == null && error == null) |
| 196 return this; | 196 return this; |
| 197 | 197 |
| 198 var medium = new Promise<T>(this, true); | 198 var medium = new Promise<T>(this, true); |
| 199 | 199 |
| 223 AddHandler(resultHandler, errorHandler, medium.InternalCancel); | 223 AddHandler(resultHandler, errorHandler, medium.InternalCancel); |
| 224 | 224 |
| 225 return medium; | 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 /// <summary> | 238 /// <summary> |
| 229 /// Adds new handlers to this promise. | 239 /// Adds new handlers to this promise. |
| 230 /// </summary> | 240 /// </summary> |
| 231 /// <param name="success">The handler of the successfully completed operation. | 241 /// <param name="success">The handler of the successfully completed operation. |
| 232 /// This handler will recieve an operation result as a parameter.</param> | 242 /// This handler will recieve an operation result as a parameter.</param> |
| 233 /// <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> | 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 /// <returns>The new promise chained to this one.</returns> | 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 if (success == null && error == null) | 246 if (success == null && error == null) |
| 237 return this; | 247 return this; |
| 238 | 248 |
| 239 var medium = new Promise<T>(this, true); | 249 var medium = new Promise<T>(this, true); |
| 240 | 250 |
| 264 | 274 |
| 265 return medium; | 275 return medium; |
| 266 } | 276 } |
| 267 | 277 |
| 268 | 278 |
| 269 public Promise<T> Then(ResultHandler<T> success) { | 279 public IPromise<T> Then(ResultHandler<T> success) { |
| 270 if (success == null) | 280 if (success == null) |
| 271 return this; | 281 return this; |
| 272 | 282 |
| 273 var medium = new Promise<T>(this, true); | 283 var medium = new Promise<T>(this, true); |
| 274 | 284 |
| 285 AddHandler(resultHandler, medium.Reject, medium.InternalCancel); | 295 AddHandler(resultHandler, medium.Reject, medium.InternalCancel); |
| 286 | 296 |
| 287 return medium; | 297 return medium; |
| 288 } | 298 } |
| 289 | 299 |
| 290 public Promise<T> Error(ErrorHandler error) { | 300 public IPromise<T> Error(ErrorHandler error) { |
| 291 return Then(null, error); | 301 return Then((ResultHandler<T>)null, error); |
| 292 } | 302 } |
| 293 | 303 |
| 294 /// <summary> | 304 /// <summary> |
| 295 /// Handles error and allows to keep the promise. | 305 /// Handles error and allows to keep the promise. |
| 296 /// </summary> | 306 /// </summary> |
| 297 /// <remarks> | 307 /// <remarks> |
| 298 /// If the specified handler throws an exception, this exception will be used to reject the promise. | 308 /// If the specified handler throws an exception, this exception will be used to reject the promise. |
| 299 /// </remarks> | 309 /// </remarks> |
| 300 /// <param name="handler">The error handler which returns the result of the promise.</param> | 310 /// <param name="handler">The error handler which returns the result of the promise.</param> |
| 301 /// <returns>New promise.</returns> | 311 /// <returns>New promise.</returns> |
| 302 public Promise<T> Error(ErrorHandler<T> handler) { | 312 public IPromise<T> Error(ErrorHandler<T> handler) { |
| 303 if (handler == null) | 313 if (handler == null) |
| 304 return this; | 314 return this; |
| 305 | 315 |
| 306 var medium = new Promise<T>(this, true); | 316 var medium = new Promise<T>(this, true); |
| 307 | 317 |
| 318 ); | 328 ); |
| 319 | 329 |
| 320 return medium; | 330 return medium; |
| 321 } | 331 } |
| 322 | 332 |
| 323 public Promise<T> Anyway(Action handler) { | 333 public IPromise<T> Anyway(Action handler) { |
| 324 if (handler == null) | 334 if (handler == null) |
| 325 return this; | 335 return this; |
| 326 | 336 |
| 327 var medium = new Promise<T>(); | 337 var medium = new Promise<T>(); |
| 328 | 338 |
| 356 /// <typeparam name="TNew">Новый тип результата.</typeparam> | 366 /// <typeparam name="TNew">Новый тип результата.</typeparam> |
| 357 /// <param name="mapper">Преобразование результата к новому типу.</param> | 367 /// <param name="mapper">Преобразование результата к новому типу.</param> |
| 358 /// <param name="error">Обработчик ошибки. Данный обработчик получит | 368 /// <param name="error">Обработчик ошибки. Данный обработчик получит |
| 359 /// исключение возникшее при выполнении операции.</param> | 369 /// исключение возникшее при выполнении операции.</param> |
| 360 /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns> | 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 if (mapper == null) | 372 if (mapper == null) |
| 363 throw new ArgumentNullException("mapper"); | 373 throw new ArgumentNullException("mapper"); |
| 364 | 374 |
| 365 // создаем прицепленное обещание | 375 // создаем прицепленное обещание |
| 366 var chained = new Promise<TNew>(); | 376 var chained = new Promise<TNew>(); |
| 383 ); | 393 ); |
| 384 | 394 |
| 385 return chained; | 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 return Map(mapper, null); | 399 return Map(mapper, null); |
| 390 } | 400 } |
| 391 | 401 |
| 392 /// <summary> | 402 /// <summary> |
| 393 /// Сцепляет несколько аснхронных операций. Указанная асинхронная операция будет вызвана после | 403 /// Сцепляет несколько аснхронных операций. Указанная асинхронная операция будет вызвана после |
| 397 /// <typeparam name="TNew">Тип результата указанной асинхронной операции.</typeparam> | 407 /// <typeparam name="TNew">Тип результата указанной асинхронной операции.</typeparam> |
| 398 /// <param name="chained">Асинхронная операция, которая должна будет начаться после выполнения текущей.</param> | 408 /// <param name="chained">Асинхронная операция, которая должна будет начаться после выполнения текущей.</param> |
| 399 /// <param name="error">Обработчик ошибки. Данный обработчик получит | 409 /// <param name="error">Обработчик ошибки. Данный обработчик получит |
| 400 /// исключение возникшее при выполнении текуещй операции.</param> | 410 /// исключение возникшее при выполнении текуещй операции.</param> |
| 401 /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns> | 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 // создать посредника, к которому будут подвызяваться следующие обработчики. |
| 406 // когда будет выполнена реальная асинхронная операция, она обратиться к посреднику, чтобы | 416 // когда будет выполнена реальная асинхронная операция, она обратиться к посреднику, чтобы |
| 407 // передать через него результаты работы. | 417 // передать через него результаты работы. |
| 435 ); | 445 ); |
| 436 | 446 |
| 437 return medium; | 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 return Chain(chained, null); | 451 return Chain(chained, null); |
| 442 } | 452 } |
| 443 | 453 |
| 444 public Promise<T> Cancelled(Action handler) { | 454 public IPromise<T> Cancelled(Action handler) { |
| 445 AddHandler(null, null, handler); | 455 AddHandler(null, null, handler); |
| 446 return this; | 456 return this; |
| 447 } | 457 } |
| 448 | 458 |
| 449 /// <summary> | 459 /// <summary> |
| 450 /// Adds the specified handler for all cases (success, error, cancel) | 460 /// Adds the specified handler for all cases (success, error, cancel) |
| 451 /// </summary> | 461 /// </summary> |
| 452 /// <param name="handler">The handler that will be called anyway</param> | 462 /// <param name="handler">The handler that will be called anyway</param> |
| 453 /// <returns>self</returns> | 463 /// <returns>self</returns> |
| 454 public Promise<T> Finally(Action handler) { | 464 public IPromise<T> Finally(Action handler) { |
| 455 if (handler == null) | 465 if (handler == null) |
| 456 throw new ArgumentNullException("handler"); | 466 throw new ArgumentNullException("handler"); |
| 457 AddHandler( | 467 AddHandler( |
| 458 x => handler(), | 468 x => handler(), |
| 459 e => handler(), | 469 e => handler(), |
