##// END OF EJS Templates
fixed TransientPromiseException handling
cin -
r99:8ddf1648eca4 v2
parent child
Show More
@@ -1,86 +1,86
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: ICancellable {
8 8 /// <summary>
9 9 /// Check whereather the promise has no more than one dependent promise.
10 10 /// </summary>
11 11 bool IsExclusive {
12 12 get;
13 13 }
14 14
15 15 /// <summary>
16 16 /// Π’ΠΈΠΏ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π°, ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌΠΎΠ³ΠΎ Ρ‡Π΅Ρ€Π΅Π· Π΄Π°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅.
17 17 /// </summary>
18 18 Type PromiseType { get; }
19 19
20 20 /// <summary>
21 /// ΠžΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ являСтся Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Ρ‹ΠΌ, Π»ΠΈΠ±ΠΎ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, Π»ΠΈΠ±ΠΎ с ошибкой.
21 /// ΠžΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ являСтся Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Ρ‹ΠΌ, Π»ΠΈΠ±ΠΎ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, Π»ΠΈΠ±ΠΎ с ошибкой, Π»ΠΈΠ±ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½ΠΎ.
22 22 /// </summary>
23 23 bool IsResolved { get; }
24 24
25 25 /// <summary>
26 26 /// ΠžΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±Ρ‹Π»ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½ΠΎ.
27 27 /// </summary>
28 28 bool IsCancelled { get; }
29 29
30 30 IPromise Then(Action success, ErrorHandler error, Action cancel);
31 31 IPromise Then(Action success, ErrorHandler error);
32 32 IPromise Then(Action success);
33 33
34 34 IPromise Chain(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel);
35 35 IPromise Chain(Func<IPromise> chained, ErrorHandler<IPromise> error);
36 36 IPromise Chain(Func<IPromise> chained);
37 37
38 38 /// <summary>
39 39 /// ДобавляСт послСднй ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΡƒ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ, Π½Π΅ создаСт ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.
40 40 /// </summary>
41 41 /// <param name="success">Success.</param>
42 42 /// <param name="error">Error.</param>
43 43 /// <param name="cancel">Cancel.</param>
44 44 void Last(Action success, ErrorHandler error, Action cancel);
45 45 void Last(Action success, ErrorHandler error);
46 46 void Last(Action success);
47 47
48 48 IPromise Error(ErrorHandler error);
49 49 /// <summary>
50 50 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π»ΠΈΠ±ΠΎ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Π»ΠΈΠ±ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚. Π‘ΠΎΠ±Ρ‹Ρ‚ΠΈΠ΅ ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ Π½Π΅ обрабатываСтся.
51 51 /// </summary>
52 52 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ.</param>
53 53 /// <remarks>ПослС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ΅ ошибки, ΠΎΠ½Π° пСрСдаСтся дальшС.</remarks>
54 54 /// <summary>
55 55 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π»ΠΈΠ±ΠΎ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Π»ΠΈΠ±ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, Π»ΠΈΠ±ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Ρƒ обСщания.
56 56 /// </summary>
57 57 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ.</param>
58 58 /// <remarks>ПослС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ΅ ошибки, ΠΎΠ½Π° пСрСдаСтся дальшС.</remarks>
59 59 IPromise Anyway(Action handler);
60 60 /// <summary>
61 61 /// ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ для рСгистрации ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ обСщания, событиС ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½ΠΎ.
62 62 /// </summary>
63 63 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, связанноС с Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ.</returns>
64 64 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ события.</param>
65 65 /// <remarks>Если ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π²Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅, Ρ‚ΠΎ ΠΎΠ½ΠΎ пСрСдаСтся ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΡƒ ошибки, Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ€Π°Π±ΠΎΡ‚Ρ‹
66 66 /// ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½ связанному ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΡŽ</remarks>
67 67 IPromise Cancelled(Action handler);
68 68
69 69 /// <summary>
70 70 /// ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ обСщания ΠΊ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅.
71 71 /// </summary>
72 72 IPromise<T> Cast<T>();
73 73
74 74 /// <summary>
75 75 /// Π‘ΠΈΠ½Ρ…Ρ€ΠΎΠ½ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΏΠΎΡ‚ΠΎΠΊ с ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ΠΌ.
76 76 /// </summary>
77 77 void Join();
78 78 /// <summary>
79 79 /// Π‘ΠΈΠ½Ρ…Ρ€ΠΎΠ½ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΏΠΎΡ‚ΠΎΠΊ с ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ΠΌ.
80 80 /// </summary>
81 81 /// <param name="timeout">ВрСмя оТидания, ΠΏΠΎ Π΅Π³ΠΎ ΠΈΡΡ‚Π΅Ρ‡Π΅Π½ΠΈΡŽ Π²ΠΎΠ·Π½ΠΈΠΊΠ½Π΅Ρ‚ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅.</param>
82 82 /// <exception cref="TimeoutException">ΠŸΡ€Π΅Π²Ρ‹ΡˆΠ΅Π½ΠΎ врСмя оТидания.</exception>
83 83 void Join(int timeout);
84 84
85 85 }
86 86 }
@@ -1,914 +1,914
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Reflection;
4 4 using System.Diagnostics;
5 5 using System.Threading;
6 6 using Implab.Parallels;
7 7
8 8 namespace Implab {
9 9
10 10 public delegate void ErrorHandler(Exception e);
11 11 public delegate T ErrorHandler<out T>(Exception e);
12 12 public delegate void ResultHandler<in T>(T result);
13 13 public delegate TNew ResultMapper<in TSrc,out TNew>(TSrc result);
14 14
15 15 /// <summary>
16 16 /// Класс для асинхронного получСния Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ². Π’Π°ΠΊ Π½Π°Π·Ρ‹Π²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅".
17 17 /// </summary>
18 18 /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌΠΎΠ³ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π°</typeparam>
19 19 /// <remarks>
20 20 /// <para>БСрвис ΠΏΡ€ΠΈ ΠΎΠ±Ρ€Π°Ρ‰Π΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρƒ Π΄Π°Π΅Ρ‚ ΠΎΠ±Π΅Ρ‰Π°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ,
21 21 /// ΠΊΠ»ΠΈΠ΅Π½Ρ‚ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ² Ρ‚Π°ΠΊΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ ряд ΠΎΠ±Ρ€Π°Ρ‚Π½Ρ‹Ρ… Π²Ρ‹Π·ΠΎΠ²ΠΎ для получСния
22 22 /// событий выполнСния обСщания, Ρ‚ΠΎΠ΅ΡΡ‚ΡŒ Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈ прСдоставлСнии Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ².</para>
23 23 /// <para>
24 24 /// ΠžΠ±Π΅Ρ‰Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΠ°ΠΊ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ, Ρ‚Π°ΠΊ ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ с ошибкой. Для подписки Π½Π°
25 25 /// Π΄Π°Π½Π½Ρ‹Π΅ события ΠΊΠ»ΠΈΠ΅Π½Ρ‚ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ <c>Then</c>.
26 26 /// </para>
27 27 /// <para>
28 28 /// БСрвис, Π² свою ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ, ΠΏΠΎ ΠΎΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΡŽ выполнСния ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ с ошибкой),
29 29 /// ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> для оповСщСния ΠΊΠ»ΠΈΠ΅Ρ‚Π½Π° ΠΎ
30 30 /// Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ обСщания.
31 31 /// </para>
32 32 /// <para>
33 33 /// Если сСрвСр успСл Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π΅Ρ‰Π΅ Π΄ΠΎ Ρ‚ΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ‚ Π½Π° Π½Π΅Π³ΠΎ подписался,
34 34 /// Ρ‚ΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ подписки ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° Π±ΡƒΠ΄ΡƒΡ‚ Π²Ρ‹Π·Π²Π°Π½Ρ‹ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΠ²ΡƒΡŽΡ‰ΠΈΠ΅ события Π² синхронном
35 35 /// Ρ€Π΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ‚ Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΠΏΠΎΠ²Π΅Ρ‰Π΅Π½ Π² любом случаС. Π˜Π½Π°Ρ‡Π΅, ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡŽΡ‚ΡΡ Π²
36 36 /// список Π² порядкС подписания ΠΈ Π² этом ΠΆΠ΅ порядкС ΠΎΠ½ΠΈ Π±ΡƒΠ΄ΡƒΡ‚ Π²Ρ‹Π·Π²Π°Π½Ρ‹ ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ
37 37 /// обСщания.
38 38 /// </para>
39 39 /// <para>
40 40 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Ρ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ обСщания ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°Ρ‚ΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡ†ΠΈΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ
41 41 /// связанныС асинхронныС ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Ρ‚Π°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°ΡŽΡ‚ обСщания. Для этого слСдуСт
42 42 /// ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΡƒΡŽ Ρ„ΠΎΡ€ΠΌΡƒ ΠΌΠ΅Ρ‚ΠΎΠ΄Π΅ <c>Then</c>.
43 43 /// </para>
44 44 /// <para>
45 45 /// Π’Π°ΠΊΠΆΠ΅ Ρ…ΠΎΡ€ΠΎΡˆΠΈΠΌ ΠΏΡ€Π°Π²ΠΈΠ»ΠΎΠΌ являСтся Ρ‚ΠΎ, Ρ‡Ρ‚ΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ
46 46 /// Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΈΠ½ΠΈΡ†ΠΈΠ°Ρ‚ΠΎΡ€ обСщания ΠΈΠ½Π°Ρ‡Π΅ ΠΌΠΎΠ³ΡƒΡ‚ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡƒΡ‚ΡŒ противорСчия.
47 47 /// </para>
48 48 /// </remarks>
49 49 public class Promise<T> : IPromise<T> {
50 50
51 51 protected struct HandlerDescriptor {
52 52 public ResultHandler<T> resultHandler;
53 53 public ErrorHandler<T> errorHandler;
54 54 public Action cancellHandler;
55 55 public Promise<T> medium;
56 56
57 57 public void Resolve(T result) {
58 58 if (resultHandler != null) {
59 59 try {
60 60 resultHandler(result);
61 61 } catch (Exception e) {
62 62 Reject(e);
63 63 return;
64 64 }
65 65 }
66 66 if (medium != null)
67 67 medium.Resolve(result);
68 68 }
69 69
70 70 public void Reject(Exception err) {
71 71 if (errorHandler != null) {
72 72 try {
73 73 var res = errorHandler(err);
74 74 if (medium != null)
75 75 medium.Resolve(res);
76 } catch (TransientPromiseException err2) {
76 /*} catch (TransientPromiseException err2) {
77 77 if (medium != null)
78 medium.Reject(err2.InnerException);
78 medium.Reject(err2.InnerException);*/
79 79 } catch (Exception err2) {
80 80 if (medium != null)
81 81 medium.Reject(err2);
82 82 }
83 83 } else if (medium != null)
84 84 medium.Reject(err);
85 85 }
86 86
87 87 public void Cancel() {
88 88 if (cancellHandler != null) {
89 89 try {
90 90 cancellHandler();
91 91 } catch (Exception err) {
92 92 Reject(err);
93 93 return;
94 94 }
95 95 }
96 96 if (medium != null)
97 97 medium.Cancel();
98 98 }
99 99 }
100 100
101 101 const int UNRESOLVED_SATE = 0;
102 102 const int TRANSITIONAL_STATE = 1;
103 103 const int SUCCEEDED_STATE = 2;
104 104 const int REJECTED_STATE = 3;
105 105 const int CANCELLED_STATE = 4;
106 106
107 107 readonly bool m_cancellable;
108 108
109 109 int m_childrenCount = 0;
110 110 int m_state;
111 111 T m_result;
112 112 Exception m_error;
113 113
114 114 readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>();
115 115
116 116 public Promise() {
117 117 m_cancellable = true;
118 118 }
119 119
120 120 public Promise(IPromise parent, bool cancellable) {
121 121 m_cancellable = cancellable;
122 122 if (parent != null)
123 123 AddHandler(
124 124 null,
125 125 null,
126 126 () => {
127 127 if (parent.IsExclusive)
128 128 parent.Cancel();
129 129 },
130 130 null
131 131 );
132 132 }
133 133
134 134 bool BeginTransit() {
135 135 return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE);
136 136 }
137 137
138 138 void CompleteTransit(int state) {
139 139 if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE))
140 140 throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
141 141 }
142 142
143 143 void WaitTransition() {
144 144 while (m_state == TRANSITIONAL_STATE) {
145 145 Thread.MemoryBarrier();
146 146 }
147 147 }
148 148
149 149 public bool IsResolved {
150 150 get {
151 151 Thread.MemoryBarrier();
152 152 return m_state > 1;
153 153 }
154 154 }
155 155
156 156 public bool IsCancelled {
157 157 get {
158 158 Thread.MemoryBarrier();
159 159 return m_state == CANCELLED_STATE;
160 160 }
161 161 }
162 162
163 163 public Type PromiseType {
164 164 get { return typeof(T); }
165 165 }
166 166
167 167 /// <summary>
168 168 /// ВыполняСт ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, сообщая ΠΎΠ± ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎΠΌ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ.
169 169 /// </summary>
170 170 /// <param name="result">Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния.</param>
171 171 /// <exception cref="InvalidOperationException">Π”Π°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΡƒΠΆΠ΅ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
172 172 public void Resolve(T result) {
173 173 if (BeginTransit()) {
174 174 m_result = result;
175 175 CompleteTransit(SUCCEEDED_STATE);
176 176 OnStateChanged();
177 177 } else {
178 178 WaitTransition();
179 179 if (m_state != CANCELLED_STATE)
180 180 throw new InvalidOperationException("The promise is already resolved");
181 181 }
182 182 }
183 183
184 184 /// <summary>
185 185 /// ВыполняСт ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, сообщая ΠΎΠ± ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎΠΌ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠΌ выполнСния Π±ΡƒΠ΄Π΅Ρ‚ пустоС значСния.
186 186 /// </summary>
187 187 /// <remarks>
188 188 /// Π”Π°Π½Π½Ρ‹ΠΉ Π²Π°Ρ€ΠΈΠ°Π½Ρ‚ ΡƒΠ΄ΠΎΠ±Π΅Π½ Π² случаях, ΠΊΠΎΠ³Π΄Π° интСрСсСн Ρ„Π°ΠΊΡ‚ выполнСния ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ, Π½Π΅ΠΆΠ΅Π»ΠΈ ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅.
189 189 /// </remarks>
190 190 public void Resolve() {
191 191 Resolve(default(T));
192 192 }
193 193
194 194 /// <summary>
195 195 /// ВыполняСт ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, сообщая ΠΎΠ± ошибкС
196 196 /// </summary>
197 197 /// <remarks>
198 198 /// ΠŸΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡ‚ΠΎΡ‡Π½ΠΎΠΉ срСдС, ΠΏΡ€ΠΈ Π΅Π³ΠΎ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ сразу нСсколько ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ²
199 199 /// ΠΌΠΎΠ³Ρƒ Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ ΠΎΡˆΠΈΠ±ΠΊΡƒ, ΠΏΡ€ΠΈ этом Ρ‚ΠΎΠ»ΡŒΠΊΠΎ пСрвая Π±ΡƒΠ΄Π΅Ρ‚ использована Π² качСствС Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π°, ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹Π΅
200 200 /// Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΡ€ΠΎΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΠΎΠ²Π°Π½Ρ‹.
201 201 /// </remarks>
202 202 /// <param name="error">Π˜ΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ возникшСС ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ</param>
203 203 /// <exception cref="InvalidOperationException">Π”Π°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΡƒΠΆΠ΅ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception>
204 204 public void Reject(Exception error) {
205 205 if (BeginTransit()) {
206 m_error = error;
206 m_error = error is TransientPromiseException ? error.InnerException : error;
207 207 CompleteTransit(REJECTED_STATE);
208 208 OnStateChanged();
209 209 } else {
210 210 WaitTransition();
211 211 if (m_state == SUCCEEDED_STATE)
212 212 throw new InvalidOperationException("The promise is already resolved");
213 213 }
214 214 }
215 215
216 216 /// <summary>
217 217 /// ΠžΡ‚ΠΌΠ΅Π½ΡΠ΅Ρ‚ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΡŽ, Ссли это Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.
218 218 /// </summary>
219 219 /// <remarks>Для опрСдСлСния Π±Ρ‹Π»Π° Π»ΠΈ опСрация ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½Π° слСдуСт ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ свойство <see cref="IsCancelled"/>.</remarks>
220 220 public void Cancel() {
221 221 if (m_cancellable && BeginTransit()) {
222 222 CompleteTransit(CANCELLED_STATE);
223 223 OnStateChanged();
224 224 }
225 225 }
226 226
227 227 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel) {
228 228 if (success == null && error == null && cancel == null)
229 229 return this;
230 230
231 231 var medium = new Promise<T>(this, true);
232 232
233 233 AddHandler(success, error, cancel, medium);
234 234
235 235 return medium;
236 236 }
237 237
238 238 /// <summary>
239 239 /// Adds new handlers to this promise.
240 240 /// </summary>
241 241 /// <param name="success">The handler of the successfully completed operation.
242 242 /// This handler will recieve an operation result as a parameter.</param>
243 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>
244 244 /// <returns>The new promise chained to this one.</returns>
245 245 public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) {
246 246 if (success == null && error == null)
247 247 return this;
248 248
249 249 var medium = new Promise<T>(this, true);
250 250
251 251 AddHandler(success, error, null, medium);
252 252
253 253 return medium;
254 254 }
255 255
256 256
257 257
258 258
259 259 public IPromise<T> Then(ResultHandler<T> success) {
260 260 if (success == null)
261 261 return this;
262 262
263 263 var medium = new Promise<T>(this, true);
264 264
265 265 AddHandler(success, null, null, medium);
266 266
267 267 return medium;
268 268 }
269 269
270 270 /// <summary>
271 271 /// ПослСдний ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠΈ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.
272 272 /// </summary>
273 273 /// <param name="success"></param>
274 274 /// <param name="error"></param>
275 275 /// <param name="cancel"></param>
276 276 /// <remarks>
277 277 /// <para>
278 278 /// Π”Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ Π½Π΅ создаСт связанного с Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ обСщания ΠΈ ΠΏΡ€Π΅Π΄Π½Π°Π·Π½Π°Ρ‡Π΅Π½ для окончания
279 279 /// фсинхронной Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠΈ.
280 280 /// </para>
281 281 /// <para>
282 282 /// Если Π΄Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ нСсколько Ρ€Π°Π·, Π»ΠΈΠ±ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ, Ρ‚ΠΎ Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠ°
283 283 /// Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΎΠ΄ΠΈΠ½ΠΎΡ‡Π½ΠΎΠΉ <see cref="IsExclusive"/> ΠΈ, ΠΊΠ°ΠΊ слСдствиС, Π±ΡƒΠ΄Π΅Ρ‚ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡ‚ΠΌΠ΅Π½Π°
284 284 /// всСй Ρ†Π΅ΠΏΠΈ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ снизу (с самого послСднСго обСщания).
285 285 /// </para>
286 286 /// </remarks>
287 287 public void Last(ResultHandler<T> success, ErrorHandler error, Action cancel) {
288 288 if (success == null && error == null && cancel == null)
289 289 return;
290 290
291 291 ErrorHandler<T> errorHandler = null;
292 292 if (error != null)
293 293 errorHandler = err => {
294 294 error(err);
295 295 return default(T);
296 296 };
297 297 AddHandler(success, errorHandler, cancel, null);
298 298 }
299 299
300 300 public void Last(ResultHandler<T> success, ErrorHandler error) {
301 301 Last(success, error, null);
302 302 }
303 303
304 304 public void Last(ResultHandler<T> success) {
305 305 Last(success, null, null);
306 306 }
307 307
308 308 public IPromise Error(ErrorHandler error) {
309 309 if (error == null)
310 310 return this;
311 311
312 312 var medium = new Promise<T>(this, true);
313 313
314 314 AddHandler(
315 315 null,
316 316 e => {
317 317 error(e);
318 318 return default(T);
319 319 },
320 320 null,
321 321 medium
322 322 );
323 323
324 324 return medium;
325 325 }
326 326
327 327 /// <summary>
328 328 /// Handles error and allows to keep the promise.
329 329 /// </summary>
330 330 /// <remarks>
331 331 /// If the specified handler throws an exception, this exception will be used to reject the promise.
332 332 /// </remarks>
333 333 /// <param name="handler">The error handler which returns the result of the promise.</param>
334 334 /// <returns>New promise.</returns>
335 335 public IPromise<T> Error(ErrorHandler<T> handler) {
336 336 if (handler == null)
337 337 return this;
338 338
339 339 var medium = new Promise<T>(this, true);
340 340
341 341 AddHandler(null, handler, null, medium);
342 342
343 343 return medium;
344 344 }
345 345
346 346 /// <summary>
347 347 /// ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Ρ‚ΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполСния ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ.
348 348 /// </summary>
349 349 /// <typeparam name="TNew">Новый Ρ‚ΠΈΠΏ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π°.</typeparam>
350 350 /// <param name="mapper">ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ.</param>
351 351 /// <param name="error">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибки. Π”Π°Π½Π½Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚
352 352 /// ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ возникшСС ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</param>
353 353 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ исходного обСщания.</returns>
354 354 /// <param name = "cancel"></param>
355 355 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error, Action cancel) {
356 356 Safe.ArgumentNotNull(mapper, "mapper");
357 357
358 358 // создаСм ΠΏΡ€ΠΈΡ†Π΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅
359 359 var medium = new Promise<TNew>(this, true);
360 360
361 361 ResultHandler<T> resultHandler = result => medium.Resolve(mapper(result));
362 362 ErrorHandler<T> errorHandler;
363 363 if (error != null)
364 364 errorHandler = e => {
365 365 try {
366 366 medium.Resolve(error(e));
367 367 } catch (Exception e2) {
368 368 // Π² случаС ошибки Π½ΡƒΠΆΠ½ΠΎ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ дальшС ΠΏΠΎ Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠ΅
369 369 medium.Reject(e2);
370 370 }
371 371 return default(T);
372 372 };
373 373 else
374 374 errorHandler = e => {
375 375 medium.Reject(e);
376 376 return default(T);
377 377 };
378 378
379 379 Action cancelHandler;
380 380 if (cancel != null)
381 381 cancelHandler = () => {
382 382 cancel();
383 383 medium.Cancel();
384 384 };
385 385 else
386 386 cancelHandler = medium.Cancel;
387 387
388 388
389 389 AddHandler(
390 390 resultHandler,
391 391 errorHandler,
392 392 cancelHandler,
393 393 null
394 394 );
395 395
396 396 return medium;
397 397 }
398 398
399 399 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error) {
400 400 return Then(mapper, error, null);
401 401 }
402 402
403 403 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) {
404 404 return Then(mapper, null, null);
405 405 }
406 406
407 407 /// <summary>
408 408 /// БцСпляСт нСсколько аснхронных ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ. Указанная асинхронная опСрация Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½Π° послС
409 409 /// выполнСния Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ, Π° Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ использован для ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ
410 410 /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.
411 411 /// </summary>
412 412 /// <typeparam name="TNew">Π’ΠΈΠΏ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ асинхронной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</typeparam>
413 413 /// <param name="chained">Асинхронная опСрация, которая Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡƒΠ΄Π΅Ρ‚ Π½Π°Ρ‡Π°Ρ‚ΡŒΡΡ послС выполнСния Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ.</param>
414 414 /// <param name="error">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибки. Π”Π°Π½Π½Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚
415 415 /// ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ возникшСС ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Ρ‚Π΅ΠΊΡƒΠ΅Ρ‰ΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</param>
416 416 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΡŽ ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ аснхронной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</returns>
417 417 /// <param name = "cancel"></param>
418 418 public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error, Action cancel) {
419 419
420 420 Safe.ArgumentNotNull(chained, "chained");
421 421
422 422 // ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° Π² Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ‚ связывания Π΅Ρ‰Π΅ Π½Π΅ Π½Π°Ρ‡Π°Ρ‚Π° асинхронная опСрация, поэтому Π½ΡƒΠΆΠ½ΠΎ
423 423 // ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ посрСдника, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΠΎΠ΄Π²Ρ‹Π·ΡΠ²Π°Ρ‚ΡŒΡΡ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ.
424 424 // ΠΊΠΎΠ³Π΄Π° Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π° Ρ€Π΅Π°Π»ΡŒΠ½Π°Ρ асинхронная опСрация, ΠΎΠ½Π° ΠΎΠ±Ρ€Π°Ρ‚ΠΈΡ‚ΡŒΡΡ ΠΊ посрСднику, Ρ‡Ρ‚ΠΎΠ±Ρ‹
425 425 // ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ Ρ‡Π΅Ρ€Π΅Π· Π½Π΅Π³ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Ρ€Π°Π±ΠΎΡ‚Ρ‹.
426 426 var medium = new Promise<TNew>(this, true);
427 427
428 428 ResultHandler<T> resultHandler = delegate(T result) {
429 429 if (medium.IsCancelled)
430 430 return;
431 431
432 432 var promise = chained(result);
433 433
434 434 promise.Last(
435 435 medium.Resolve,
436 436 medium.Reject,
437 437 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
438 438 );
439 439
440 440 // notify chained operation that it's not needed anymore
441 441 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
442 442 // зависит IsExclusive
443 443 medium.Cancelled(() => {
444 444 if (promise.IsExclusive)
445 445 promise.Cancel();
446 446 });
447 447 };
448 448
449 449 ErrorHandler<T> errorHandler;
450 450
451 451 if (error != null)
452 452 errorHandler = delegate(Exception e) {
453 453 try {
454 454 var promise = error(e);
455 455
456 456 promise.Last(
457 457 medium.Resolve,
458 458 medium.Reject,
459 459 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
460 460 );
461 461
462 462 // notify chained operation that it's not needed anymore
463 463 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
464 464 // зависит IsExclusive
465 465 medium.Cancelled(() => {
466 466 if (promise.IsExclusive)
467 467 promise.Cancel();
468 468 });
469 469 } catch (Exception e2) {
470 470 medium.Reject(e2);
471 471 }
472 472 return default(T);
473 473 };
474 474 else
475 475 errorHandler = err => {
476 476 medium.Reject(err);
477 477 return default(T);
478 478 };
479 479
480 480
481 481 Action cancelHandler;
482 482 if (cancel != null)
483 483 cancelHandler = () => {
484 484 if (cancel != null)
485 485 cancel();
486 486 medium.Cancel();
487 487 };
488 488 else
489 489 cancelHandler = medium.Cancel;
490 490
491 491 AddHandler(
492 492 resultHandler,
493 493 errorHandler,
494 494 cancelHandler,
495 495 null
496 496 );
497 497
498 498 return medium;
499 499 }
500 500
501 501 public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error) {
502 502 return Chain(chained, error, null);
503 503 }
504 504
505 505 public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained) {
506 506 return Chain(chained, null, null);
507 507 }
508 508
509 509 public IPromise<T> Cancelled(Action handler) {
510 510 var medium = new Promise<T>(this,true);
511 511 AddHandler(null, null, handler, medium);
512 512 return medium;
513 513 }
514 514
515 515 /// <summary>
516 516 /// Adds the specified handler for all cases (success, error, cancel)
517 517 /// </summary>
518 518 /// <param name="handler">The handler that will be called anyway</param>
519 519 /// <returns>self</returns>
520 520 public IPromise<T> Anyway(Action handler) {
521 521 Safe.ArgumentNotNull(handler, "handler");
522 522
523 523 AddHandler(
524 524 x => handler(),
525 525 e => {
526 526 handler();
527 527 throw new TransientPromiseException(e);
528 528 },
529 529 handler,
530 530 null
531 531 );
532 532 return this;
533 533 }
534 534
535 535 /// <summary>
536 536 /// ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ обСщания ΠΊ Π½ΡƒΠΆΠ½ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ
537 537 /// </summary>
538 538 /// <typeparam name="T2"></typeparam>
539 539 /// <returns></returns>
540 540 public IPromise<T2> Cast<T2>() {
541 541 return Then(x => (T2)(object)x, null);
542 542 }
543 543
544 544 /// <summary>
545 545 /// ДоТидаСтся ΠΎΡ‚Π»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ обСщания ΠΈ Π² случаС успСха, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚
546 546 /// Π΅Π³ΠΎ, Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, Π² ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠΌ случаС бросаСт ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅.
547 547 /// </summary>
548 548 /// <remarks>
549 549 /// <para>
550 550 /// Если ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ обСщания Π±Ρ‹Π»ΠΎ ΠΏΡ€Π΅Ρ€Π²Π°Π½ΠΎ ΠΏΠΎ Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚Ρƒ, это Π½Π΅ Π·Π½Π°Ρ‡ΠΈΡ‚,
551 551 /// Ρ‡Ρ‚ΠΎ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±Ρ‹Π»ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ Π² этом Ρ€ΠΎΠ΄Π΅, это Ρ‚ΠΎΠ»ΡŒΠΊΠΎ
552 552 /// ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ ΠΌΡ‹ Π΅Π³ΠΎ Π½Π΅ доТдались, ΠΎΠ΄Π½Π°ΠΊΠΎ всС зарСгистрированныС
553 553 /// ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±Ρ‹Π»ΠΈ Ρ‚Π°ΠΊ ΠΎΡΡ‚Π°Π»ΠΈΡΡŒ ΠΈ ΠΎΠ½ΠΈ Π±ΡƒΠ΄ΡƒΡ‚ Π²Ρ‹Π·Π²Π°Π½Ρ‹, ΠΊΠΎΠ³Π΄Π°
554 554 /// ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
555 555 /// </para>
556 556 /// <para>
557 557 /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡ€Π°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΈΡΡ‚Π΅Ρ‡ΡŒ
558 558 /// Π² Ρ‚ΠΎΡ‚ ΠΌΠΎΠΌΠ΅Π½Ρ‚, ΠΊΠΎΠ³Π΄Π° Π½Π°Ρ‡Π°Π»Π°ΡΡŒ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠΈ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ², ΠΈ
559 559 /// ΠΊ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΡΡ‚ΠΎΡΡ‚ΡŒ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ
560 560 /// ΠΎΡ‚ΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ привСсти ΠΊ Π½Π΅ΠΏΡ€ΠΎΠ³Π½ΠΎΠ·ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρƒ.
561 561 /// </para>
562 562 /// </remarks>
563 563 /// <param name="timeout">ВрСмя оТидания</param>
564 564 /// <returns>Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния обСщания</returns>
565 565 public T Join(int timeout) {
566 566 var evt = new ManualResetEvent(false);
567 567 Anyway(() => evt.Set());
568 568
569 569 if (!evt.WaitOne(timeout, true))
570 570 throw new TimeoutException();
571 571
572 572 switch (m_state) {
573 573 case SUCCEEDED_STATE:
574 574 return m_result;
575 575 case CANCELLED_STATE:
576 576 throw new OperationCanceledException();
577 577 case REJECTED_STATE:
578 578 throw new TargetInvocationException(m_error);
579 579 default:
580 580 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
581 581 }
582 582 }
583 583
584 584 public T Join() {
585 585 return Join(Timeout.Infinite);
586 586 }
587 587
588 588 void AddHandler(ResultHandler<T> success, ErrorHandler<T> error, Action cancel, Promise<T> medium) {
589 589 if (success != null || error != null)
590 590 Interlocked.Increment(ref m_childrenCount);
591 591
592 592 var handler = new HandlerDescriptor {
593 593 resultHandler = success,
594 594 errorHandler = error,
595 595 cancellHandler = cancel,
596 596 medium = medium
597 597 };
598 598
599 599 bool queued;
600 600
601 601 if (!IsResolved) {
602 602 m_handlers.Enqueue(handler);
603 603 queued = true;
604 604 } else {
605 605 // the promise is in resolved state, just invoke the handled with minimum overhead
606 606 queued = false;
607 607 InvokeHandler(handler);
608 608 }
609 609
610 610 if (queued && IsResolved && m_handlers.TryDequeue(out handler))
611 611 // if the promise have been resolved while we was adding handler to the queue
612 612 // we can't guarantee that someone is still processing it
613 613 // therefore we will fetch a handler from the queue and execute it
614 614 // note that fetched handler may be not the one that we have added
615 615 // even we can fetch no handlers at all :)
616 616 InvokeHandler(handler);
617 617 }
618 618
619 619 protected virtual void InvokeHandler(HandlerDescriptor handler) {
620 620 switch (m_state) {
621 621 case SUCCEEDED_STATE:
622 622 handler.Resolve(m_result);
623 623 break;
624 624 case REJECTED_STATE:
625 625 handler.Reject(m_error);
626 626 break;
627 627 case CANCELLED_STATE:
628 628 handler.Cancel();
629 629 break;
630 630 default:
631 631 // do nothing
632 632 return;
633 633 }
634 634 }
635 635
636 636 void OnStateChanged() {
637 637 HandlerDescriptor handler;
638 638 while (m_handlers.TryDequeue(out handler))
639 639 InvokeHandler(handler);
640 640 }
641 641
642 642 public bool IsExclusive {
643 643 get {
644 644 return m_childrenCount <= 1;
645 645 }
646 646 }
647 647
648 648 /// <summary>
649 649 /// ΠžΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ‚ нСсколько ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠΌ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ являСтся массив Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ² Π΄Ρ€ΡƒΠ³ΠΈΡ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.
650 650 /// Если хотябы ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ, Ρ‚ΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π΅Π½ΠΈΠ΅ Ρ‚ΠΎΠΆΠ΅ Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
651 651 /// ΠŸΡ€ΠΈ ΠΎΡ‚ΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ обСщания, ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Π΅ обСщания Ρ‚Π°ΠΊΠΆΠ΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½Ρ‹, Ссли Π½ΠΈΠΊΡ‚ΠΎ большС Π½Π° Π½ΠΈΡ… Π½Π΅ подписан.
652 652 /// </summary>
653 653 /// <param name="promises">Бписок ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ. Если список пустой, Ρ‚ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ возвращаСтся ΡƒΠΆΠ΅ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π½Ρ‹ΠΌ.</param>
654 654 /// <returns>ΠžΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΡŽΡ‰Π΅Π΅ Π² сСбС Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.</returns>
655 655 /// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ null</exception>
656 656 public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) {
657 657 if (promises == null)
658 658 throw new ArgumentNullException();
659 659
660 660 // создаСм аккумулятор для Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ² ΠΈ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅
661 661 var result = new T[promises.Count];
662 662 var promise = new Promise<T[]>();
663 663
664 664 // special case
665 665 if (promises.Count == 0) {
666 666 promise.Resolve(result);
667 667 return promise;
668 668 }
669 669
670 670 int pending = promises.Count;
671 671
672 672 for (int i = 0; i < promises.Count; i++) {
673 673 var dest = i;
674 674
675 675 if (promises[i] != null) {
676 676 promises[i].Then(
677 677 x => {
678 678 result[dest] = x;
679 679 if (Interlocked.Decrement(ref pending) == 0)
680 680 promise.Resolve(result);
681 681 },
682 682 e => {
683 683 promise.Reject(e);
684 684 return default(T);
685 685 }
686 686 );
687 687 } else {
688 688 if (Interlocked.Decrement(ref pending) == 0)
689 689 promise.Resolve(result);
690 690 }
691 691 }
692 692
693 693 promise.Cancelled(
694 694 () => {
695 695 foreach (var d in promises)
696 696 if (d != null && d.IsExclusive)
697 697 d.Cancel();
698 698 }
699 699 );
700 700
701 701 return promise;
702 702 }
703 703
704 704 /// <summary>
705 705 /// ΠžΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ‚ нСсколько ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ. Π Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡ€ΠΈ
706 706 /// Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ всСх ΡƒΠΊΠ°Π·Π°Π½Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ. ΠŸΡ€ΠΈ этом Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ
707 707 /// ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΡŽΡ‚ΡΡ.
708 708 /// </summary>
709 709 /// <param name="promises">ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΎΠ±ΡŠΠ΅Π΄Π΅Π½Π΅Π½Ρ‹ Π² ΠΎΠ΄Π½ΠΎ.</param>
710 710 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΡŽΡ‰Π΅Π΅ Π² сСбС ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Π΅.</returns>
711 711 /// <remarks>
712 712 /// Если Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π²ΡΡ‚Ρ€Π΅Ρ‡Π°ΡŽΡŒΡΡ <c>null</c>, Ρ‚ΠΎ ΠΎΠ½ΠΈ Π²ΠΎΡΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°ΡŽΡ‚ΡΡ ΠΊΠ°ΠΊ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π½Ρ‹Π΅ обСщания.
713 713 /// </remarks>
714 714 public static IPromise CreateComposite(ICollection<IPromise> promises) {
715 715 if (promises == null)
716 716 throw new ArgumentNullException();
717 717 if (promises.Count == 0)
718 718 return Promise<object>.ResultToPromise(null);
719 719
720 720 int countdown = promises.Count;
721 721
722 722 var result = new Promise<object>();
723 723
724 724 foreach (var d in promises) {
725 725 if (d == null) {
726 726 if (Interlocked.Decrement(ref countdown) == 0)
727 727 result.Resolve(null);
728 728 } else {
729 729 d.Then(() => {
730 730 if (Interlocked.Decrement(ref countdown) == 0)
731 731 result.Resolve(null);
732 732 });
733 733 }
734 734 }
735 735
736 736 result.Cancelled(() => {
737 737 foreach (var d in promises)
738 738 if (d != null && d.IsExclusive)
739 739 d.Cancel();
740 740 });
741 741
742 742 return result;
743 743 }
744 744
745 745 public static Promise<T> ResultToPromise(T result) {
746 746 var p = new Promise<T>();
747 747 p.Resolve(result);
748 748 return p;
749 749 }
750 750
751 751 public static Promise<T> ExceptionToPromise(Exception error) {
752 752 if (error == null)
753 753 throw new ArgumentNullException();
754 754
755 755 var p = new Promise<T>();
756 756 p.Reject(error);
757 757 return p;
758 758 }
759 759
760 760 #region IPromiseBase explicit implementation
761 761
762 762 IPromise IPromise.Then(Action success, ErrorHandler error, Action cancel) {
763 763 return Then(
764 764 success != null ? new ResultHandler<T>(x => success()) : null,
765 765 error != null ? new ErrorHandler<T>(e => {
766 766 error(e);
767 767 return default(T);
768 768 }) : null,
769 769 cancel
770 770 );
771 771 }
772 772
773 773 IPromise IPromise.Then(Action success, ErrorHandler error) {
774 774 return Then(
775 775 success != null ? new ResultHandler<T>(x => success()) : null,
776 776 error != null ? new ErrorHandler<T>(e => {
777 777 error(e);
778 778 return default(T);
779 779 }) : null
780 780 );
781 781 }
782 782
783 783 IPromise IPromise.Then(Action success) {
784 784 Safe.ArgumentNotNull(success, "success");
785 785 return Then(x => success());
786 786 }
787 787
788 788 IPromise IPromise.Chain(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel) {
789 789 return ChainNoResult(chained, error, cancel);
790 790 }
791 791
792 792 IPromise ChainNoResult(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel) {
793 793 Safe.ArgumentNotNull(chained, "chained");
794 794
795 795 var medium = new Promise<object>(this, true);
796 796
797 797 ResultHandler<T> resultHandler = delegate(T result) {
798 798 if (medium.IsCancelled)
799 799 return;
800 800
801 801 var promise = chained();
802 802
803 803 promise.Last(
804 804 medium.Resolve,
805 805 medium.Reject,
806 806 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
807 807 );
808 808
809 809 // notify chained operation that it's not needed anymore
810 810 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
811 811 // зависит IsExclusive
812 812 medium.Cancelled(() => {
813 813 if (promise.IsExclusive)
814 814 promise.Cancel();
815 815 });
816 816 };
817 817
818 818 ErrorHandler<T> errorHandler;
819 819
820 820 if (error != null)
821 821 errorHandler = delegate(Exception e) {
822 822 try {
823 823 var promise = error(e);
824 824
825 825 promise.Last(
826 826 medium.Resolve,
827 827 medium.Reject,
828 828 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
829 829 );
830 830
831 831 // notify chained operation that it's not needed anymore
832 832 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
833 833 // зависит IsExclusive
834 834 medium.Cancelled(() => {
835 835 if (promise.IsExclusive)
836 836 promise.Cancel();
837 837 });
838 838 } catch (Exception e2) {
839 839 medium.Reject(e2);
840 840 }
841 841 return default(T);
842 842 };
843 843 else
844 844 errorHandler = err => {
845 845 medium.Reject(err);
846 846 return default(T);
847 847 };
848 848
849 849
850 850 Action cancelHandler;
851 851 if (cancel != null)
852 852 cancelHandler = () => {
853 853 if (cancel != null)
854 854 cancel();
855 855 medium.Cancel();
856 856 };
857 857 else
858 858 cancelHandler = medium.Cancel;
859 859
860 860 AddHandler(
861 861 resultHandler,
862 862 errorHandler,
863 863 cancelHandler,
864 864 null
865 865 );
866 866
867 867 return medium;
868 868 }
869 869 IPromise IPromise.Chain(Func<IPromise> chained, ErrorHandler<IPromise> error) {
870 870 return ChainNoResult(chained, error, null);
871 871 }
872 872 IPromise IPromise.Chain(Func<IPromise> chained) {
873 873 return ChainNoResult(chained, null, null);
874 874 }
875 875
876 876
877 877 void IPromise.Last(Action success, ErrorHandler error, Action cancel) {
878 878 Last(x => success(), error, cancel);
879 879 }
880 880
881 881 void IPromise.Last(Action success, ErrorHandler error) {
882 882 Last(x => success(), error, null);
883 883 }
884 884
885 885 void IPromise.Last(Action success) {
886 886 Last(x => success(), null, null);
887 887 }
888 888
889 889 IPromise IPromise.Error(ErrorHandler error) {
890 890 return Error(error);
891 891 }
892 892
893 893 IPromise IPromise.Anyway(Action handler) {
894 894 return Anyway(handler);
895 895 }
896 896
897 897 IPromise IPromise.Cancelled(Action handler) {
898 898 return Cancelled(handler);
899 899 }
900 900
901 901 void IPromise.Join() {
902 902 Join();
903 903 }
904 904
905 905 void IPromise.Join(int timeout) {
906 906 Join(timeout);
907 907 }
908 908
909 909 #endregion
910 910
911 911
912 912
913 913 }
914 914 }
General Comments 0
You need to be logged in to leave comments. Login now