##// END OF EJS Templates
Added the chaining method to the non-generic IPromise
cin -
r96:daffa72a1cec v2
parent child
Show More
@@ -1,82 +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 IPromise Chain(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel);
35 IPromise Chain(Func<IPromise> chained, ErrorHandler<IPromise> error);
36 IPromise Chain(Func<IPromise> chained);
37
34 38 /// <summary>
35 39 /// ДобавляСт послСднй ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΡƒ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ, Π½Π΅ создаСт ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.
36 40 /// </summary>
37 41 /// <param name="success">Success.</param>
38 42 /// <param name="error">Error.</param>
39 43 /// <param name="cancel">Cancel.</param>
40 44 void Last(Action success, ErrorHandler error, Action cancel);
41 45 void Last(Action success, ErrorHandler error);
42 46 void Last(Action success);
43 47
44 48 IPromise Error(ErrorHandler error);
45 49 /// <summary>
46 50 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π»ΠΈΠ±ΠΎ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Π»ΠΈΠ±ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚. Π‘ΠΎΠ±Ρ‹Ρ‚ΠΈΠ΅ ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ Π½Π΅ обрабатываСтся.
47 51 /// </summary>
48 52 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ.</param>
49 53 /// <remarks>ПослС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ΅ ошибки, ΠΎΠ½Π° пСрСдаСтся дальшС.</remarks>
50 54 /// <summary>
51 55 /// ΠžΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅Ρ‚ Π»ΠΈΠ±ΠΎ ΠΎΡˆΠΈΠ±ΠΊΡƒ, Π»ΠΈΠ±ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, Π»ΠΈΠ±ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Ρƒ обСщания.
52 56 /// </summary>
53 57 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ.</param>
54 58 /// <remarks>ПослС ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ΅ ошибки, ΠΎΠ½Π° пСрСдаСтся дальшС.</remarks>
55 59 IPromise Anyway(Action handler);
56 60 /// <summary>
57 61 /// ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ для рСгистрации ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ обСщания, событиС ΠΎΡ‚ΠΌΠ΅Π½Ρ‹ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½ΠΎ.
58 62 /// </summary>
59 63 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, связанноС с Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΌ.</returns>
60 64 /// <param name="handler">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ события.</param>
61 65 /// <remarks>Если ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ Π²Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅, Ρ‚ΠΎ ΠΎΠ½ΠΎ пСрСдаСтся ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΡƒ ошибки, Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ€Π°Π±ΠΎΡ‚Ρ‹
62 66 /// ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½ связанному ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΡŽ</remarks>
63 67 IPromise Cancelled(Action handler);
64 68
65 69 /// <summary>
66 70 /// ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ обСщания ΠΊ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅.
67 71 /// </summary>
68 72 IPromise<T> Cast<T>();
69 73
70 74 /// <summary>
71 75 /// Π‘ΠΈΠ½Ρ…Ρ€ΠΎΠ½ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΏΠΎΡ‚ΠΎΠΊ с ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ΠΌ.
72 76 /// </summary>
73 77 void Join();
74 78 /// <summary>
75 79 /// Π‘ΠΈΠ½Ρ…Ρ€ΠΎΠ½ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΏΠΎΡ‚ΠΎΠΊ с ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ΠΌ.
76 80 /// </summary>
77 81 /// <param name="timeout">ВрСмя оТидания, ΠΏΠΎ Π΅Π³ΠΎ ΠΈΡΡ‚Π΅Ρ‡Π΅Π½ΠΈΡŽ Π²ΠΎΠ·Π½ΠΈΠΊΠ½Π΅Ρ‚ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅.</param>
78 82 /// <exception cref="TimeoutException">ΠŸΡ€Π΅Π²Ρ‹ΡˆΠ΅Π½ΠΎ врСмя оТидания.</exception>
79 83 void Join(int timeout);
80 84
81 85 }
82 86 }
@@ -1,822 +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 76 } catch (TransientPromiseException err2) {
77 77 if (medium != null)
78 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 206 m_error = 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 /// <param name = "cancel"></param>
354 355 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error, Action cancel) {
355 356 Safe.ArgumentNotNull(mapper, "mapper");
356 357
357 358 // создаСм ΠΏΡ€ΠΈΡ†Π΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅
358 359 var medium = new Promise<TNew>(this, true);
359 360
360 361 ResultHandler<T> resultHandler = result => medium.Resolve(mapper(result));
361 362 ErrorHandler<T> errorHandler;
362 363 if (error != null)
363 364 errorHandler = e => {
364 365 try {
365 366 medium.Resolve(error(e));
366 367 } catch (Exception e2) {
367 368 // Π² случаС ошибки Π½ΡƒΠΆΠ½ΠΎ ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ дальшС ΠΏΠΎ Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠ΅
368 369 medium.Reject(e2);
369 370 }
370 371 return default(T);
371 372 };
372 373 else
373 374 errorHandler = e => {
374 375 medium.Reject(e);
375 376 return default(T);
376 377 };
377 378
378 379 Action cancelHandler;
379 380 if (cancel != null)
380 381 cancelHandler = () => {
381 382 cancel();
382 383 medium.Cancel();
383 384 };
384 385 else
385 386 cancelHandler = medium.Cancel;
386 387
387 388
388 389 AddHandler(
389 390 resultHandler,
390 391 errorHandler,
391 392 cancelHandler,
392 393 null
393 394 );
394 395
395 396 return medium;
396 397 }
397 398
398 399 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error) {
399 400 return Then(mapper, error, null);
400 401 }
401 402
402 403 public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) {
403 404 return Then(mapper, null, null);
404 405 }
405 406
406 407 /// <summary>
407 408 /// БцСпляСт нСсколько аснхронных ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΉ. Указанная асинхронная опСрация Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹Π·Π²Π°Π½Π° послС
408 409 /// выполнСния Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ, Π° Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ использован для ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ
409 410 /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.
410 411 /// </summary>
411 412 /// <typeparam name="TNew">Π’ΠΈΠΏ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ асинхронной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</typeparam>
412 413 /// <param name="chained">Асинхронная опСрация, которая Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡƒΠ΄Π΅Ρ‚ Π½Π°Ρ‡Π°Ρ‚ΡŒΡΡ послС выполнСния Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ.</param>
413 414 /// <param name="error">ΠžΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибки. Π”Π°Π½Π½Ρ‹ΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚
414 415 /// ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ возникшСС ΠΏΡ€ΠΈ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Ρ‚Π΅ΠΊΡƒΠ΅Ρ‰ΠΉ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</param>
415 416 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΡŽ ΡƒΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ аснхронной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ.</returns>
417 /// <param name = "cancel"></param>
416 418 public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error, Action cancel) {
417 419
418 420 Safe.ArgumentNotNull(chained, "chained");
419 421
420 422 // ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° Π² Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ‚ связывания Π΅Ρ‰Π΅ Π½Π΅ Π½Π°Ρ‡Π°Ρ‚Π° асинхронная опСрация, поэтому Π½ΡƒΠΆΠ½ΠΎ
421 423 // ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ посрСдника, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄ΡƒΡ‚ ΠΏΠΎΠ΄Π²Ρ‹Π·ΡΠ²Π°Ρ‚ΡŒΡΡ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ.
422 424 // ΠΊΠΎΠ³Π΄Π° Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π° Ρ€Π΅Π°Π»ΡŒΠ½Π°Ρ асинхронная опСрация, ΠΎΠ½Π° ΠΎΠ±Ρ€Π°Ρ‚ΠΈΡ‚ΡŒΡΡ ΠΊ посрСднику, Ρ‡Ρ‚ΠΎΠ±Ρ‹
423 425 // ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‚ΡŒ Ρ‡Π΅Ρ€Π΅Π· Π½Π΅Π³ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Ρ€Π°Π±ΠΎΡ‚Ρ‹.
424 426 var medium = new Promise<TNew>(this, true);
425 427
426 428 ResultHandler<T> resultHandler = delegate(T result) {
427 429 if (medium.IsCancelled)
428 430 return;
429 431
430 432 var promise = chained(result);
431 433
432 434 promise.Last(
433 435 medium.Resolve,
434 436 medium.Reject,
435 437 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
436 438 );
437 439
438 440 // notify chained operation that it's not needed anymore
439 441 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
440 442 // зависит IsExclusive
441 443 medium.Cancelled(() => {
442 444 if (promise.IsExclusive)
443 445 promise.Cancel();
444 446 });
445 447 };
446 448
447 449 ErrorHandler<T> errorHandler;
448 450
449 451 if (error != null)
450 452 errorHandler = delegate(Exception e) {
451 453 try {
452 454 var promise = error(e);
453 455
454 456 promise.Last(
455 457 medium.Resolve,
456 458 medium.Reject,
457 459 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
458 460 );
459 461
460 462 // notify chained operation that it's not needed anymore
461 463 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
462 464 // зависит IsExclusive
463 465 medium.Cancelled(() => {
464 466 if (promise.IsExclusive)
465 467 promise.Cancel();
466 468 });
467 469 } catch (Exception e2) {
468 470 medium.Reject(e2);
469 471 }
470 472 return default(T);
471 473 };
472 474 else
473 475 errorHandler = err => {
474 476 medium.Reject(err);
475 477 return default(T);
476 478 };
477 479
478 480
479 481 Action cancelHandler;
480 482 if (cancel != null)
481 483 cancelHandler = () => {
482 484 if (cancel != null)
483 485 cancel();
484 486 medium.Cancel();
485 487 };
486 488 else
487 489 cancelHandler = medium.Cancel;
488 490
489 491 AddHandler(
490 492 resultHandler,
491 493 errorHandler,
492 494 cancelHandler,
493 495 null
494 496 );
495 497
496 498 return medium;
497 499 }
498 500
499 501 public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error) {
500 502 return Chain(chained, error, null);
501 503 }
502 504
503 505 public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained) {
504 506 return Chain(chained, null, null);
505 507 }
506 508
507 509 public IPromise<T> Cancelled(Action handler) {
508 510 var medium = new Promise<T>(this,true);
509 511 AddHandler(null, null, handler, medium);
510 512 return medium;
511 513 }
512 514
513 515 /// <summary>
514 516 /// Adds the specified handler for all cases (success, error, cancel)
515 517 /// </summary>
516 518 /// <param name="handler">The handler that will be called anyway</param>
517 519 /// <returns>self</returns>
518 520 public IPromise<T> Anyway(Action handler) {
519 521 Safe.ArgumentNotNull(handler, "handler");
520 522
521 523 AddHandler(
522 524 x => handler(),
523 525 e => {
524 526 handler();
525 527 throw new TransientPromiseException(e);
526 528 },
527 529 handler,
528 530 null
529 531 );
530 532 return this;
531 533 }
532 534
533 535 /// <summary>
534 536 /// ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ обСщания ΠΊ Π½ΡƒΠΆΠ½ΠΎΠΌΡƒ Ρ‚ΠΈΠΏΡƒ
535 537 /// </summary>
536 538 /// <typeparam name="T2"></typeparam>
537 539 /// <returns></returns>
538 540 public IPromise<T2> Cast<T2>() {
539 541 return Then(x => (T2)(object)x, null);
540 542 }
541 543
542 544 /// <summary>
543 545 /// ДоТидаСтся ΠΎΡ‚Π»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ обСщания ΠΈ Π² случаС успСха, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚
544 546 /// Π΅Π³ΠΎ, Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚, Π² ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠΌ случаС бросаСт ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅.
545 547 /// </summary>
546 548 /// <remarks>
547 549 /// <para>
548 550 /// Если ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ обСщания Π±Ρ‹Π»ΠΎ ΠΏΡ€Π΅Ρ€Π²Π°Π½ΠΎ ΠΏΠΎ Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚Ρƒ, это Π½Π΅ Π·Π½Π°Ρ‡ΠΈΡ‚,
549 551 /// Ρ‡Ρ‚ΠΎ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±Ρ‹Π»ΠΎ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ Π² этом Ρ€ΠΎΠ΄Π΅, это Ρ‚ΠΎΠ»ΡŒΠΊΠΎ
550 552 /// ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ ΠΌΡ‹ Π΅Π³ΠΎ Π½Π΅ доТдались, ΠΎΠ΄Π½Π°ΠΊΠΎ всС зарСгистрированныС
551 553 /// ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±Ρ‹Π»ΠΈ Ρ‚Π°ΠΊ ΠΎΡΡ‚Π°Π»ΠΈΡΡŒ ΠΈ ΠΎΠ½ΠΈ Π±ΡƒΠ΄ΡƒΡ‚ Π²Ρ‹Π·Π²Π°Π½Ρ‹, ΠΊΠΎΠ³Π΄Π°
552 554 /// ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
553 555 /// </para>
554 556 /// <para>
555 557 /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡ€Π°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΈΡΡ‚Π΅Ρ‡ΡŒ
556 558 /// Π² Ρ‚ΠΎΡ‚ ΠΌΠΎΠΌΠ΅Π½Ρ‚, ΠΊΠΎΠ³Π΄Π° Π½Π°Ρ‡Π°Π»Π°ΡΡŒ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠΈ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ², ΠΈ
557 559 /// ΠΊ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΡΡ‚ΠΎΡΡ‚ΡŒ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠ΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ
558 560 /// ΠΎΡ‚ΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ‚ привСсти ΠΊ Π½Π΅ΠΏΡ€ΠΎΠ³Π½ΠΎΠ·ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρƒ.
559 561 /// </para>
560 562 /// </remarks>
561 563 /// <param name="timeout">ВрСмя оТидания</param>
562 564 /// <returns>Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния обСщания</returns>
563 565 public T Join(int timeout) {
564 566 var evt = new ManualResetEvent(false);
565 567 Anyway(() => evt.Set());
566 568
567 569 if (!evt.WaitOne(timeout, true))
568 570 throw new TimeoutException();
569 571
570 572 switch (m_state) {
571 573 case SUCCEEDED_STATE:
572 574 return m_result;
573 575 case CANCELLED_STATE:
574 576 throw new OperationCanceledException();
575 577 case REJECTED_STATE:
576 578 throw new TargetInvocationException(m_error);
577 579 default:
578 580 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
579 581 }
580 582 }
581 583
582 584 public T Join() {
583 585 return Join(Timeout.Infinite);
584 586 }
585 587
586 588 void AddHandler(ResultHandler<T> success, ErrorHandler<T> error, Action cancel, Promise<T> medium) {
587 589 if (success != null || error != null)
588 590 Interlocked.Increment(ref m_childrenCount);
589 591
590 592 var handler = new HandlerDescriptor {
591 593 resultHandler = success,
592 594 errorHandler = error,
593 595 cancellHandler = cancel,
594 596 medium = medium
595 597 };
596 598
597 599 bool queued;
598 600
599 601 if (!IsResolved) {
600 602 m_handlers.Enqueue(handler);
601 603 queued = true;
602 604 } else {
603 605 // the promise is in resolved state, just invoke the handled with minimum overhead
604 606 queued = false;
605 607 InvokeHandler(handler);
606 608 }
607 609
608 610 if (queued && IsResolved && m_handlers.TryDequeue(out handler))
609 611 // if the promise have been resolved while we was adding handler to the queue
610 612 // we can't guarantee that someone is still processing it
611 613 // therefore we will fetch a handler from the queue and execute it
612 614 // note that fetched handler may be not the one that we have added
613 615 // even we can fetch no handlers at all :)
614 616 InvokeHandler(handler);
615 617 }
616 618
617 619 protected virtual void InvokeHandler(HandlerDescriptor handler) {
618 620 switch (m_state) {
619 621 case SUCCEEDED_STATE:
620 622 handler.Resolve(m_result);
621 623 break;
622 624 case REJECTED_STATE:
623 625 handler.Reject(m_error);
624 626 break;
625 627 case CANCELLED_STATE:
626 628 handler.Cancel();
627 629 break;
628 630 default:
629 631 // do nothing
630 632 return;
631 633 }
632 634 }
633 635
634 636 void OnStateChanged() {
635 637 HandlerDescriptor handler;
636 638 while (m_handlers.TryDequeue(out handler))
637 639 InvokeHandler(handler);
638 640 }
639 641
640 642 public bool IsExclusive {
641 643 get {
642 644 return m_childrenCount <= 1;
643 645 }
644 646 }
645 647
646 648 /// <summary>
647 649 /// ΠžΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ‚ нСсколько ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠΌ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ являСтся массив Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ² Π΄Ρ€ΡƒΠ³ΠΈΡ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.
648 650 /// Если хотябы ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ, Ρ‚ΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅Ρ‰Π΅Π½ΠΈΠ΅ Ρ‚ΠΎΠΆΠ΅ Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ.
649 651 /// ΠŸΡ€ΠΈ ΠΎΡ‚ΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ обСщания, ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Π΅ обСщания Ρ‚Π°ΠΊΠΆΠ΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΎΡ‚ΠΌΠ΅Π½Π΅Π½Ρ‹, Ссли Π½ΠΈΠΊΡ‚ΠΎ большС Π½Π° Π½ΠΈΡ… Π½Π΅ подписан.
650 652 /// </summary>
651 653 /// <param name="promises">Бписок ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ. Если список пустой, Ρ‚ΠΎ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ возвращаСтся ΡƒΠΆΠ΅ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π½Ρ‹ΠΌ.</param>
652 654 /// <returns>ΠžΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΡŽΡ‰Π΅Π΅ Π² сСбС Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ.</returns>
653 655 /// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ null</exception>
654 656 public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) {
655 657 if (promises == null)
656 658 throw new ArgumentNullException();
657 659
658 660 // создаСм аккумулятор для Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠ² ΠΈ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅
659 661 var result = new T[promises.Count];
660 662 var promise = new Promise<T[]>();
661 663
662 664 // special case
663 665 if (promises.Count == 0) {
664 666 promise.Resolve(result);
665 667 return promise;
666 668 }
667 669
668 670 int pending = promises.Count;
669 671
670 672 for (int i = 0; i < promises.Count; i++) {
671 673 var dest = i;
672 674
673 675 if (promises[i] != null) {
674 676 promises[i].Then(
675 677 x => {
676 678 result[dest] = x;
677 679 if (Interlocked.Decrement(ref pending) == 0)
678 680 promise.Resolve(result);
679 681 },
680 682 e => {
681 683 promise.Reject(e);
682 684 return default(T);
683 685 }
684 686 );
685 687 } else {
686 688 if (Interlocked.Decrement(ref pending) == 0)
687 689 promise.Resolve(result);
688 690 }
689 691 }
690 692
691 693 promise.Cancelled(
692 694 () => {
693 695 foreach (var d in promises)
694 696 if (d != null && d.IsExclusive)
695 697 d.Cancel();
696 698 }
697 699 );
698 700
699 701 return promise;
700 702 }
701 703
702 704 /// <summary>
703 705 /// ΠžΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ‚ нСсколько ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ. Π Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅ Π±ΡƒΠ΄Π΅Ρ‚ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡ€ΠΈ
704 706 /// Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ всСх ΡƒΠΊΠ°Π·Π°Π½Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ. ΠŸΡ€ΠΈ этом Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ
705 707 /// ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΡŽΡ‚ΡΡ.
706 708 /// </summary>
707 709 /// <param name="promises">ΠšΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½Ρ‹Ρ… ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΎΠ±ΡŠΠ΅Π΄Π΅Π½Π΅Π½Ρ‹ Π² ΠΎΠ΄Π½ΠΎ.</param>
708 710 /// <returns>НовоС ΠΎΠ±Π΅Ρ‰Π°Π½ΠΈΠ΅, ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΡŽΡ‰Π΅Π΅ Π² сСбС ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Π΅.</returns>
709 711 /// <remarks>
710 712 /// Если Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ Π²ΡΡ‚Ρ€Π΅Ρ‡Π°ΡŽΡŒΡΡ <c>null</c>, Ρ‚ΠΎ ΠΎΠ½ΠΈ Π²ΠΎΡΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°ΡŽΡ‚ΡΡ ΠΊΠ°ΠΊ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π½Ρ‹Π΅ обСщания.
711 713 /// </remarks>
712 714 public static IPromise CreateComposite(ICollection<IPromise> promises) {
713 715 if (promises == null)
714 716 throw new ArgumentNullException();
715 717 if (promises.Count == 0)
716 718 return Promise<object>.ResultToPromise(null);
717 719
718 720 int countdown = promises.Count;
719 721
720 722 var result = new Promise<object>();
721 723
722 724 foreach (var d in promises) {
723 725 if (d == null) {
724 726 if (Interlocked.Decrement(ref countdown) == 0)
725 727 result.Resolve(null);
726 728 } else {
727 729 d.Then(() => {
728 730 if (Interlocked.Decrement(ref countdown) == 0)
729 731 result.Resolve(null);
730 732 });
731 733 }
732 734 }
733 735
734 736 result.Cancelled(() => {
735 737 foreach (var d in promises)
736 738 if (d != null && d.IsExclusive)
737 739 d.Cancel();
738 740 });
739 741
740 742 return result;
741 743 }
742 744
743 745 public static Promise<T> ResultToPromise(T result) {
744 746 var p = new Promise<T>();
745 747 p.Resolve(result);
746 748 return p;
747 749 }
748 750
749 751 public static Promise<T> ExceptionToPromise(Exception error) {
750 752 if (error == null)
751 753 throw new ArgumentNullException();
752 754
753 755 var p = new Promise<T>();
754 756 p.Reject(error);
755 757 return p;
756 758 }
757 759
758 760 #region IPromiseBase explicit implementation
759 761
760 762 IPromise IPromise.Then(Action success, ErrorHandler error, Action cancel) {
761 763 return Then(
762 x => success(),
763 e => {
764 success != null ? new ResultHandler<T>(x => success()) : null,
765 error != null ? new ErrorHandler<T>(e => {
764 766 error(e);
765 767 return default(T);
766 },
768 }) : null,
767 769 cancel
768 770 );
769 771 }
770 772
771 773 IPromise IPromise.Then(Action success, ErrorHandler error) {
772 774 return Then(
773 x => success(),
774 e => {
775 success != null ? new ResultHandler<T>(x => success()) : null,
776 error != null ? new ErrorHandler<T>(e => {
775 777 error(e);
776 778 return default(T);
777 }
779 }) : null
778 780 );
779 781 }
780 782
781 783 IPromise IPromise.Then(Action success) {
784 Safe.ArgumentNotNull(success, "success");
782 785 return Then(x => success());
783 786 }
784 787
788 IPromise IPromise.Chain(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel) {
789 return ChainNoResult(chained, error, cancel);
790 }
791
792 IPromise ChainNoResult(Func<IPromise> chained, ErrorHandler<IPromise> error, Action cancel) {
793 Safe.ArgumentNotNull(chained, "chained");
794
795 var medium = new Promise<object>(this, true);
796
797 ResultHandler<T> resultHandler = delegate(T result) {
798 if (medium.IsCancelled)
799 return;
800
801 var promise = chained();
802
803 promise.Last(
804 medium.Resolve,
805 medium.Reject,
806 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
807 );
808
809 // notify chained operation that it's not needed anymore
810 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
811 // зависит IsExclusive
812 medium.Cancelled(() => {
813 if (promise.IsExclusive)
814 promise.Cancel();
815 });
816 };
817
818 ErrorHandler<T> errorHandler;
819
820 if (error != null)
821 errorHandler = delegate(Exception e) {
822 try {
823 var promise = error(e);
824
825 promise.Last(
826 medium.Resolve,
827 medium.Reject,
828 () => medium.Reject(new OperationCanceledException()) // внСшняя ΠΎΡ‚ΠΌΠ΅Π½Π° связанной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ рассматриваСтся ΠΊΠ°ΠΊ ошибка
829 );
830
831 // notify chained operation that it's not needed anymore
832 // порядок Π²Ρ‹Π·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΎΡ‚ этого
833 // зависит IsExclusive
834 medium.Cancelled(() => {
835 if (promise.IsExclusive)
836 promise.Cancel();
837 });
838 } catch (Exception e2) {
839 medium.Reject(e2);
840 }
841 return default(T);
842 };
843 else
844 errorHandler = err => {
845 medium.Reject(err);
846 return default(T);
847 };
848
849
850 Action cancelHandler;
851 if (cancel != null)
852 cancelHandler = () => {
853 if (cancel != null)
854 cancel();
855 medium.Cancel();
856 };
857 else
858 cancelHandler = medium.Cancel;
859
860 AddHandler(
861 resultHandler,
862 errorHandler,
863 cancelHandler,
864 null
865 );
866
867 return medium;
868 }
869 IPromise IPromise.Chain(Func<IPromise> chained, ErrorHandler<IPromise> error) {
870 return ChainNoResult(chained, error, null);
871 }
872 IPromise IPromise.Chain(Func<IPromise> chained) {
873 return ChainNoResult(chained, null, null);
874 }
875
876
785 877 void IPromise.Last(Action success, ErrorHandler error, Action cancel) {
786 878 Last(x => success(), error, cancel);
787 879 }
788 880
789 881 void IPromise.Last(Action success, ErrorHandler error) {
790 882 Last(x => success(), error, null);
791 883 }
792 884
793 885 void IPromise.Last(Action success) {
794 886 Last(x => success(), null, null);
795 887 }
796 888
797 889 IPromise IPromise.Error(ErrorHandler error) {
798 890 return Error(error);
799 891 }
800 892
801 893 IPromise IPromise.Anyway(Action handler) {
802 894 return Anyway(handler);
803 895 }
804 896
805 897 IPromise IPromise.Cancelled(Action handler) {
806 898 return Cancelled(handler);
807 899 }
808 900
809 901 void IPromise.Join() {
810 902 Join();
811 903 }
812 904
813 905 void IPromise.Join(int timeout) {
814 906 Join(timeout);
815 907 }
816 908
817 909 #endregion
818 910
819 911
820 912
821 913 }
822 914 }
@@ -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 using System.Text.RegularExpressions;
6 6 using System.Diagnostics;
7 7
8 8 namespace Implab
9 9 {
10 10 public static class Safe
11 11 {
12 12 public static void ArgumentMatch(string param, string name, Regex rx) {
13 13 if (rx == null)
14 14 throw new ArgumentNullException("rx");
15 15 if (!rx.IsMatch(param))
16 16 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), name);
17 17 }
18 18
19 19 public static void ArgumentNotEmpty(string param, string name) {
20 20 if (String.IsNullOrEmpty(param))
21 21 throw new ArgumentException("The parameter can't be empty", name);
22 22 }
23 23
24 24 public static void ArgumentNotEmpty<T>(T[] param, string name) {
25 25 if (param == null || param.Length == 0)
26 26 throw new ArgumentException("The array must be not emty");
27 27 }
28 28
29 29 public static void ArgumentNotNull(object param, string name) {
30 30 if (param == null)
31 31 throw new ArgumentNullException(name);
32 32 }
33 33
34 34 public static void ArgumentInRange(int arg, int min, int max, string name) {
35 35 if (arg < min || arg > max)
36 36 throw new ArgumentOutOfRangeException(name);
37 37 }
38 38
39 39 public static void Dispose<T>(T obj) where T : class
40 40 {
41 41 var disp = obj as IDisposable;
42 42 if (disp != null)
43 43 disp.Dispose();
44 44 }
45 45
46 46 [DebuggerStepThrough]
47 47 public static IPromise<T> InvokePromise<T>(Func<T> action) {
48 48 ArgumentNotNull(action, "action");
49 49
50 50 var p = new Promise<T>();
51 51 try {
52 52 p.Resolve(action());
53 53 } catch (Exception err) {
54 54 p.Reject(err);
55 55 }
56 56
57 57 return p;
58 58 }
59 59
60 60 [DebuggerStepThrough]
61 public static IPromise<T> InvokePromise<T>(Action action) {
61 public static IPromise InvokePromise(Action action) {
62 62 ArgumentNotNull(action, "action");
63 63
64 64 var p = new Promise<object>();
65 65 try {
66 66 action();
67 67 p.Resolve();
68 68 } catch (Exception err) {
69 69 p.Reject(err);
70 70 }
71 71
72 72 return p;
73 73 }
74 74
75 75 [DebuggerStepThrough]
76 76 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
77 77 ArgumentNotNull(action, "action");
78 78
79 79 try {
80 80 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null"));
81 81 } catch (Exception err) {
82 82 return Promise<T>.ExceptionToPromise(err);
83 83 }
84 84 }
85 85 }
86 86 }
General Comments 0
You need to be logged in to leave comments. Login now