@@ -1,33 +1,33 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.Linq; |
|
3 | using System.Linq; | |
4 | using System.Text; |
|
4 | using System.Text; | |
5 |
|
5 | |||
6 | namespace Implab |
|
6 | namespace Implab | |
7 | { |
|
7 | { | |
8 | public interface IPromise |
|
8 | public interface IPromise | |
9 | { |
|
9 | { | |
10 | /// <summary> |
|
10 | /// <summary> | |
11 | /// Check whereather the promise has no more than one dependent promise. |
|
11 | /// Check whereather the promise has no more than one dependent promise. | |
12 | /// </summary> |
|
12 | /// </summary> | |
13 | bool IsExclusive |
|
13 | bool IsExclusive | |
14 | { |
|
14 | { | |
15 | get; |
|
15 | get; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | /// <summary> |
|
18 | /// <summary> | |
19 | /// The current state of the promise. |
|
19 | /// The current state of the promise. | |
20 | /// </summary> |
|
20 | /// </summary> | |
21 | PromiseState State |
|
21 | PromiseState State | |
22 | { |
|
22 | { | |
23 | get; |
|
23 | get; | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | /// <summary> |
|
26 | /// <summary> | |
27 | /// Tries to cancel the promise or the complete chain. |
|
27 | /// Tries to cancel the promise or the complete chain. | |
28 | /// </summary> |
|
28 | /// </summary> | |
29 |
/// <param name="dependencies">Try to cancel the parent promise |
|
29 | /// <param name="dependencies">Try to cancel the whole promise chain, the parent promise will be cancelled only if it has only one promise</param> | |
30 | /// <returns></returns> |
|
30 | /// <returns></returns> | |
31 | bool Cancel(bool dependencies); |
|
31 | bool Cancel(bool dependencies); | |
32 | } |
|
32 | } | |
33 | } |
|
33 | } |
@@ -1,399 +1,443 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.Linq; |
|
3 | using System.Linq; | |
4 | using System.Reflection; |
|
4 | using System.Reflection; | |
5 | using System.Text; |
|
5 | using System.Text; | |
6 | using System.Diagnostics; |
|
6 | using System.Diagnostics; | |
7 | using System.Threading; |
|
7 | using System.Threading; | |
8 |
|
8 | |||
9 | namespace Implab { |
|
9 | namespace Implab { | |
10 |
|
10 | |||
11 | public delegate void ErrorHandler(Exception e); |
|
11 | public delegate void ErrorHandler(Exception e); | |
12 |
|
12 | |||
13 | public delegate void ResultHandler<T>(T result); |
|
13 | public delegate void ResultHandler<T>(T result); | |
14 | public delegate TNew ResultMapper<TSrc, TNew>(TSrc result); |
|
14 | public delegate TNew ResultMapper<TSrc, TNew>(TSrc result); | |
15 | public delegate Promise<TNew> ChainedOperation<TSrc, TNew>(TSrc result); |
|
15 | public delegate Promise<TNew> ChainedOperation<TSrc, TNew>(TSrc result); | |
16 |
|
16 | |||
17 | /// <summary> |
|
17 | /// <summary> | |
18 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". |
|
18 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". | |
19 | /// </summary> |
|
19 | /// </summary> | |
20 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> |
|
20 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> | |
21 | /// <remarks> |
|
21 | /// <remarks> | |
22 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, |
|
22 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, | |
23 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ |
|
23 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ | |
24 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> |
|
24 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> | |
25 | /// <para> |
|
25 | /// <para> | |
26 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° |
|
26 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° | |
27 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. |
|
27 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. | |
28 | /// </para> |
|
28 | /// </para> | |
29 | /// <para> |
|
29 | /// <para> | |
30 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), |
|
30 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), | |
31 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ |
|
31 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ | |
32 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
32 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. | |
33 | /// </para> |
|
33 | /// </para> | |
34 | /// <para> |
|
34 | /// <para> | |
35 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, |
|
35 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, | |
36 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ |
|
36 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ | |
37 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² |
|
37 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² | |
38 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ |
|
38 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ | |
39 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
39 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. | |
40 | /// </para> |
|
40 | /// </para> | |
41 | /// <para> |
|
41 | /// <para> | |
42 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ |
|
42 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ | |
43 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ |
|
43 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ | |
44 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. |
|
44 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. | |
45 | /// </para> |
|
45 | /// </para> | |
46 | /// <para> |
|
46 | /// <para> | |
47 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ |
|
47 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ | |
48 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. |
|
48 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. | |
49 | /// </para> |
|
49 | /// </para> | |
50 | /// </remarks> |
|
50 | /// </remarks> | |
51 | public class Promise<T>: IPromise { |
|
51 | public class Promise<T> : IPromise { | |
52 |
|
52 | |||
53 | struct ResultHandlerInfo { |
|
53 | struct ResultHandlerInfo { | |
54 | public ResultHandler<T> resultHandler; |
|
54 | public ResultHandler<T> resultHandler; | |
55 | public ErrorHandler errorHandler; |
|
55 | public ErrorHandler errorHandler; | |
56 | public Action cancelHandler; |
|
|||
57 | } |
|
56 | } | |
58 |
|
57 | |||
59 | LinkedList<ResultHandlerInfo> m_handlersChain = new LinkedList<ResultHandlerInfo>(); |
|
58 | IPromise m_parent; | |
|
59 | ||||
|
60 | LinkedList<ResultHandlerInfo> m_resultHandlers = new LinkedList<ResultHandlerInfo>(); | |||
|
61 | LinkedList<Action> m_cancelHandlers = new LinkedList<Action>(); | |||
|
62 | ||||
|
63 | object m_lock = new Object(); | |||
|
64 | bool m_cancellable; | |||
|
65 | ||||
60 | PromiseState m_state; |
|
66 | PromiseState m_state; | |
61 | bool m_cancellable; |
|
|||
62 | T m_result; |
|
67 | T m_result; | |
63 | Exception m_error; |
|
68 | Exception m_error; | |
64 | IPromise m_parent; |
|
69 | ||
65 | int m_childrenCount; |
|
70 | int m_childrenCount; | |
66 |
|
71 | |||
67 | public Promise() { |
|
72 | public Promise() { | |
68 | m_cancellable = true; |
|
73 | m_cancellable = true; | |
69 | } |
|
74 | } | |
70 |
|
75 | |||
71 | public Promise(IPromise parent, bool cancellable) |
|
76 | public Promise(IPromise parent, bool cancellable) { | |
72 | { |
|
|||
73 | m_cancellable = cancellable; |
|
77 | m_cancellable = cancellable; | |
74 | m_parent = parent; |
|
78 | m_parent = parent; | |
75 | } |
|
79 | } | |
76 |
|
80 | |||
77 | /// <summary> |
|
81 | /// <summary> | |
78 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅, Π²ΠΎΠ·Π½ΠΈΠΊΠ°ΡΡΠ΅Π΅ ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
82 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅, Π²ΠΎΠ·Π½ΠΈΠΊΠ°ΡΡΠ΅Π΅ ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. | |
79 | /// </summary> |
|
83 | /// </summary> | |
80 | /// <description> |
|
84 | /// <description> | |
81 | /// ΠΠ°ΠΊ ΠΏΡΠ°Π²ΠΈΠ»ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠ°, Π²ΡΠΏΠΎΠ»Π½ΡΡΡΠ΅Π³ΠΎ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎ ΡΠΎΠΌ, ΡΡΠΎ Π΅Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΎΡΠΌΠ΅Π½ΠΈΡΡ. |
|
85 | /// ΠΠ°ΠΊ ΠΏΡΠ°Π²ΠΈΠ»ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠ°, Π²ΡΠΏΠΎΠ»Π½ΡΡΡΠ΅Π³ΠΎ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎ ΡΠΎΠΌ, ΡΡΠΎ Π΅Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΎΡΠΌΠ΅Π½ΠΈΡΡ. | |
82 | /// </description> |
|
86 | /// </description> | |
83 | public event EventHandler Cancelled; |
|
87 | public event EventHandler Cancelled; | |
84 |
|
88 | |||
85 | /// <summary> |
|
89 | /// <summary> | |
86 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. |
|
90 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. | |
87 | /// </summary> |
|
91 | /// </summary> | |
88 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> |
|
92 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> | |
89 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
93 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> | |
90 | public void Resolve(T result) { |
|
94 | public void Resolve(T result) { | |
91 | lock (this) { |
|
95 | lock (this) { | |
92 | if (m_state == PromiseState.Cancelled) |
|
96 | if (m_state == PromiseState.Cancelled) | |
93 | return; |
|
97 | return; | |
94 | if (m_state != PromiseState.Unresolved) |
|
98 | if (m_state != PromiseState.Unresolved) | |
95 | throw new InvalidOperationException("The promise is already resolved"); |
|
99 | throw new InvalidOperationException("The promise is already resolved"); | |
96 | m_result = result; |
|
100 | m_result = result; | |
97 | m_state = PromiseState.Resolved; |
|
101 | m_state = PromiseState.Resolved; | |
98 | } |
|
102 | } | |
99 |
|
103 | |||
100 | ResultHandlerInfo handler; |
|
104 | // state has been changed to rejected new handlers can't be added | |
|
105 | ||||
|
106 | foreach (var handler in m_resultHandlers) | |||
|
107 | InvokeHandler(handler); | |||
|
108 | ||||
|
109 | /* ResultHandlerInfo handler; | |||
101 | while (FetchNextHandler(out handler)) |
|
110 | while (FetchNextHandler(out handler)) | |
102 |
|
|
111 | InvokeHandler(handler); */ | |
103 | } |
|
112 | } | |
104 |
|
113 | |||
105 | /// <summary> |
|
114 | /// <summary> | |
106 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ |
|
115 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ | |
107 | /// </summary> |
|
116 | /// </summary> | |
108 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> |
|
117 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> | |
109 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
118 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> | |
110 | public void Reject(Exception error) { |
|
119 | public void Reject(Exception error) { | |
111 | lock (this) { |
|
120 | lock (this) { | |
112 | if (m_state == PromiseState.Cancelled) |
|
121 | if (m_state == PromiseState.Cancelled) | |
113 | return; |
|
122 | return; | |
114 | if (m_state != PromiseState.Unresolved) |
|
123 | if (m_state != PromiseState.Unresolved) | |
115 | throw new InvalidOperationException("The promise is already resolved"); |
|
124 | throw new InvalidOperationException("The promise is already resolved"); | |
116 | m_error = error; |
|
125 | m_error = error; | |
117 | m_state = PromiseState.Rejected; |
|
126 | m_state = PromiseState.Rejected; | |
118 | } |
|
127 | } | |
119 |
|
128 | |||
120 | ResultHandlerInfo handler; |
|
129 | // state has been changed to rejected new handlers can't be added | |
|
130 | ||||
|
131 | foreach (var handler in m_resultHandlers) | |||
|
132 | InvokeHandler(handler); | |||
|
133 | ||||
|
134 | /*ResultHandlerInfo handler; | |||
121 | while (FetchNextHandler(out handler)) |
|
135 | while (FetchNextHandler(out handler)) | |
122 |
|
|
136 | InvokeHandler(handler);*/ | |
123 | } |
|
137 | } | |
124 |
|
138 | |||
125 | /// <summary> |
|
139 | /// <summary> | |
126 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. |
|
140 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. | |
127 | /// </summary> |
|
141 | /// </summary> | |
128 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> |
|
142 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> | |
129 | public bool Cancel() { |
|
143 | public bool Cancel() { | |
130 | lock (this) { |
|
144 | return Cancel(true); | |
131 | if (m_state == PromiseState.Unresolved && m_cancellable) |
|
|||
132 | { |
|
|||
133 | m_state = PromiseState.Cancelled; |
|
|||
134 | EventHandler temp = Cancelled; |
|
|||
135 |
|
||||
136 | if (temp != null) |
|
|||
137 | temp(this, new EventArgs()); |
|
|||
138 |
|
||||
139 | return true; |
|
|||
140 | } else |
|
|||
141 | return false; |
|
|||
142 | } |
|
|||
143 | } |
|
145 | } | |
144 |
|
146 | |||
145 | /// <summary> |
|
147 | /// <summary> | |
146 | /// ΠΠΎΠ±Π°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
148 | /// ΠΠΎΠ±Π°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. | |
147 | /// </summary> |
|
149 | /// </summary> | |
148 | /// <param name="success">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΡΠΏΠ΅ΡΠ½ΠΎΠ³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
150 | /// <param name="success">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΡΠΏΠ΅ΡΠ½ΠΎΠ³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. | |
149 | /// ΠΠ°Π½Π½ΠΎΠΌΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
151 | /// ΠΠ°Π½Π½ΠΎΠΌΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
150 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
152 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ | |
151 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
153 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
152 | /// <returns>Π‘Π°ΠΌΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅</returns> |
|
154 | /// <returns>Π‘Π°ΠΌΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅</returns> | |
153 | public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) { |
|
155 | public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) { | |
154 | if (success == null && error == null) |
|
156 | if (success == null && error == null) | |
155 | return this; |
|
157 | return this; | |
156 |
|
158 | |||
157 | var medium = new Promise<T>(); |
|
159 | var medium = new Promise<T>(); | |
158 |
|
160 | |||
159 | var handlerInfo = new ResultHandlerInfo(); |
|
161 | var handlerInfo = new ResultHandlerInfo(); | |
160 |
|
162 | |||
161 | if (success != null) |
|
163 | if (success != null) | |
162 | handlerInfo.resultHandler = x => { |
|
164 | handlerInfo.resultHandler = x => { | |
163 | try { |
|
165 | try { | |
164 | success(x); |
|
166 | success(x); | |
165 | medium.Resolve(x); |
|
167 | medium.Resolve(x); | |
166 | } catch (Exception e) { |
|
168 | } catch (Exception e) { | |
167 | medium.Reject(e); |
|
169 | medium.Reject(e); | |
168 | } |
|
170 | } | |
169 | }; |
|
171 | }; | |
170 | else |
|
172 | else | |
171 | handlerInfo.resultHandler = x => medium.Resolve(x); |
|
173 | handlerInfo.resultHandler = x => medium.Resolve(x); | |
172 |
|
174 | |||
173 | if (error != null) |
|
175 | if (error != null) | |
174 | handlerInfo.errorHandler = x => { |
|
176 | handlerInfo.errorHandler = x => { | |
175 | try { |
|
177 | try { | |
176 | error(x); |
|
178 | error(x); | |
177 | } catch { } |
|
179 | } catch { } | |
178 | medium.Reject(x); |
|
180 | medium.Reject(x); | |
179 | }; |
|
181 | }; | |
180 | else |
|
182 | else | |
181 | handlerInfo.errorHandler = x => medium.Reject(x); |
|
183 | handlerInfo.errorHandler = x => medium.Reject(x); | |
182 |
|
184 | |||
183 | AddHandler(handlerInfo); |
|
185 | AddHandler(handlerInfo); | |
184 |
|
186 | |||
185 | return medium; |
|
187 | return medium; | |
186 | } |
|
188 | } | |
187 |
|
189 | |||
188 | public Promise<T> Then(ResultHandler<T> success) { |
|
190 | public Promise<T> Then(ResultHandler<T> success) { | |
189 | return Then(success, null); |
|
191 | return Then(success, null); | |
190 | } |
|
192 | } | |
191 |
|
193 | |||
192 | public Promise<T> Error(ErrorHandler error) { |
|
194 | public Promise<T> Error(ErrorHandler error) { | |
193 | return Then(null, error); |
|
195 | return Then(null, error); | |
194 | } |
|
196 | } | |
195 |
|
197 | |||
196 | public Promise<T> Anyway(Action handler) { |
|
198 | public Promise<T> Anyway(Action handler) { | |
197 | if (handler == null) |
|
199 | if (handler == null) | |
198 | return this; |
|
200 | return this; | |
199 |
|
201 | |||
200 | var medium = new Promise<T>(); |
|
202 | var medium = new Promise<T>(); | |
201 |
|
203 | |||
202 | AddHandler(new ResultHandlerInfo { |
|
204 | AddHandler(new ResultHandlerInfo { | |
203 | resultHandler = x => { |
|
205 | resultHandler = x => { | |
204 | try { |
|
206 | try { | |
205 | handler(); |
|
207 | handler(); | |
206 | medium.Resolve(x); |
|
208 | medium.Resolve(x); | |
207 | } catch (Exception e) { |
|
209 | } catch (Exception e) { | |
208 | medium.Reject(e); |
|
210 | medium.Reject(e); | |
209 | } |
|
211 | } | |
210 | }, |
|
212 | }, | |
211 | errorHandler = x => { |
|
213 | errorHandler = x => { | |
212 | try { |
|
214 | try { | |
213 | handler(); |
|
215 | handler(); | |
214 | } catch { } |
|
216 | } catch { } | |
215 | medium.Reject(x); |
|
217 | medium.Reject(x); | |
216 | } |
|
218 | } | |
217 | }); |
|
219 | }); | |
218 |
|
220 | |||
219 | return medium; |
|
221 | return medium; | |
220 | } |
|
222 | } | |
221 |
|
223 | |||
222 | /// <summary> |
|
224 | /// <summary> | |
223 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. |
|
225 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. | |
224 | /// </summary> |
|
226 | /// </summary> | |
225 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> |
|
227 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> | |
226 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> |
|
228 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> | |
227 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
229 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ | |
228 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
230 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
229 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> |
|
231 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> | |
230 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { |
|
232 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { | |
231 | if (mapper == null) |
|
233 | if (mapper == null) | |
232 | throw new ArgumentNullException("mapper"); |
|
234 | throw new ArgumentNullException("mapper"); | |
233 |
|
235 | |||
234 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
236 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ | |
235 | Promise<TNew> chained = new Promise<TNew>(); |
|
237 | Promise<TNew> chained = new Promise<TNew>(); | |
236 |
|
238 | |||
237 | AddHandler(new ResultHandlerInfo() { |
|
239 | AddHandler(new ResultHandlerInfo() { | |
238 | resultHandler = delegate(T result) { |
|
240 | resultHandler = delegate(T result) { | |
239 | try { |
|
241 | try { | |
240 | // Π΅ΡΠ»ΠΈ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π²ΡΠ΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅, ΡΠΎ ΡΡΠ°Π±ΠΎΡΠ°Π΅Ρ reject ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ³ΠΎ deferred |
|
242 | // Π΅ΡΠ»ΠΈ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π²ΡΠ΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅, ΡΠΎ ΡΡΠ°Π±ΠΎΡΠ°Π΅Ρ reject ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ³ΠΎ deferred | |
241 | chained.Resolve(mapper(result)); |
|
243 | chained.Resolve(mapper(result)); | |
242 | } catch (Exception e) { |
|
244 | } catch (Exception e) { | |
243 | chained.Reject(e); |
|
245 | chained.Reject(e); | |
244 | } |
|
246 | } | |
245 | }, |
|
247 | }, | |
246 | errorHandler = delegate(Exception e) { |
|
248 | errorHandler = delegate(Exception e) { | |
247 | if (error != null) |
|
249 | if (error != null) | |
248 | error(e); |
|
250 | error(e); | |
249 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
251 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ | |
250 | chained.Reject(e); |
|
252 | chained.Reject(e); | |
251 | } |
|
253 | } | |
252 | }); |
|
254 | }); | |
253 |
|
255 | |||
254 | return chained; |
|
256 | return chained; | |
255 | } |
|
257 | } | |
256 |
|
258 | |||
257 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { |
|
259 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { | |
258 | return Map(mapper, null); |
|
260 | return Map(mapper, null); | |
259 | } |
|
261 | } | |
260 |
|
262 | |||
261 | /// <summary> |
|
263 | /// <summary> | |
262 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ |
|
264 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ | |
263 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ |
|
265 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ | |
264 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
266 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. | |
265 | /// </summary> |
|
267 | /// </summary> | |
266 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> |
|
268 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> | |
267 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> |
|
269 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> | |
268 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
270 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ | |
269 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
271 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
270 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> |
|
272 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> | |
271 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { |
|
273 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { | |
272 |
|
274 | |||
273 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ |
|
275 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ | |
274 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. |
|
276 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. | |
275 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ |
|
277 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ | |
276 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. |
|
278 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. | |
277 | Promise<TNew> medium = new Promise<TNew>(); |
|
279 | Promise<TNew> medium = new Promise<TNew>(); | |
278 |
|
280 | |||
279 | AddHandler(new ResultHandlerInfo() { |
|
281 | AddHandler(new ResultHandlerInfo() { | |
280 | resultHandler = delegate(T result) { |
|
282 | resultHandler = delegate(T result) { | |
281 | try { |
|
283 | try { | |
282 | chained(result).Then( |
|
284 | chained(result).Then( | |
283 | x => medium.Resolve(x), |
|
285 | x => medium.Resolve(x), | |
284 | e => medium.Reject(e) |
|
286 | e => medium.Reject(e) | |
285 | ); |
|
287 | ); | |
286 | } catch (Exception e) { |
|
288 | } catch (Exception e) { | |
287 | // Π΅ΡΠ»ΠΈ ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π²ΡΠ΄Π°Π»ΠΎ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΌΠ΅ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΡ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
289 | // Π΅ΡΠ»ΠΈ ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π²ΡΠ΄Π°Π»ΠΎ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΌΠ΅ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΡ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ | |
288 | medium.Reject(e); |
|
290 | medium.Reject(e); | |
289 | } |
|
291 | } | |
290 | }, |
|
292 | }, | |
291 | errorHandler = delegate(Exception e) { |
|
293 | errorHandler = delegate(Exception e) { | |
292 | if (error != null) |
|
294 | if (error != null) | |
293 | error(e); |
|
295 | error(e); | |
294 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
296 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ | |
295 | medium.Reject(e); |
|
297 | medium.Reject(e); | |
296 | } |
|
298 | } | |
297 | }); |
|
299 | }); | |
298 |
|
300 | |||
299 | return medium; |
|
301 | return medium; | |
300 | } |
|
302 | } | |
301 |
|
303 | |||
302 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { |
|
304 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { | |
303 | return Chain(chained, null); |
|
305 | return Chain(chained, null); | |
304 | } |
|
306 | } | |
305 |
|
307 | |||
306 | /// <summary> |
|
308 | /// <summary> | |
307 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ |
|
309 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ | |
308 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. |
|
310 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. | |
309 | /// </summary> |
|
311 | /// </summary> | |
310 | /// <remarks> |
|
312 | /// <remarks> | |
311 | /// <para> |
|
313 | /// <para> | |
312 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, |
|
314 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, | |
313 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ |
|
315 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ | |
314 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ |
|
316 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ | |
315 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° |
|
317 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° | |
316 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
318 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. | |
317 | /// </para> |
|
319 | /// </para> | |
318 | /// <para> |
|
320 | /// <para> | |
319 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ |
|
321 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ | |
320 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ |
|
322 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ | |
321 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ |
|
323 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ | |
322 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. |
|
324 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. | |
323 | /// </para> |
|
325 | /// </para> | |
324 | /// </remarks> |
|
326 | /// </remarks> | |
325 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> |
|
327 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> | |
326 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> |
|
328 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> | |
327 | public T Join(int timeout) { |
|
329 | public T Join(int timeout) { | |
328 | ManualResetEvent evt = new ManualResetEvent(false); |
|
330 | ManualResetEvent evt = new ManualResetEvent(false); | |
329 | Anyway(() => evt.Set()); |
|
331 | Anyway(() => evt.Set()); | |
330 |
|
332 | |||
331 | if (!evt.WaitOne(timeout, true)) |
|
333 | if (!evt.WaitOne(timeout, true)) | |
332 | throw new TimeoutException(); |
|
334 | throw new TimeoutException(); | |
333 |
|
335 | |||
334 | if (m_error != null) |
|
336 | if (m_error != null) | |
335 | throw new TargetInvocationException(m_error); |
|
337 | throw new TargetInvocationException(m_error); | |
336 | else |
|
338 | else | |
337 | return m_result; |
|
339 | return m_result; | |
338 | } |
|
340 | } | |
339 |
|
341 | |||
340 | public T Join() { |
|
342 | public T Join() { | |
341 | return Join(Timeout.Infinite); |
|
343 | return Join(Timeout.Infinite); | |
342 | } |
|
344 | } | |
343 |
|
345 | |||
344 | /// <summary> |
|
346 | /// <summary> | |
345 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΊΠΎΠ³Π΄Π° |
|
347 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΊΠΎΠ³Π΄Π° | |
346 | /// ΠΈΡ Π±ΠΎΠ»ΡΡΠ΅ Π½Π΅ ΠΎΡΡΠ°Π»ΠΎΡΡ - ΡΡΠ°Π²ΠΈΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ "ΡΠ°Π·ΡΠ΅ΡΠ΅Π½ΠΎ". |
|
348 | /// ΠΈΡ Π±ΠΎΠ»ΡΡΠ΅ Π½Π΅ ΠΎΡΡΠ°Π»ΠΎΡΡ - ΡΡΠ°Π²ΠΈΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ "ΡΠ°Π·ΡΠ΅ΡΠ΅Π½ΠΎ". | |
347 | /// </summary> |
|
349 | /// </summary> | |
348 | /// <param name="handler">ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎΠ± ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ΅</param> |
|
350 | /// <param name="handler">ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎΠ± ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ΅</param> | |
349 | /// <returns>ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π΅ΡΠ΅ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π² ΠΎΡΠ΅ΡΠ΅Π΄ΠΈ</returns> |
|
351 | /// <returns>ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π΅ΡΠ΅ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π² ΠΎΡΠ΅ΡΠ΅Π΄ΠΈ</returns> | |
350 | bool FetchNextHandler(out ResultHandlerInfo handler) { |
|
352 | bool FetchNextHandler(out ResultHandlerInfo handler) { | |
351 | handler = default(ResultHandlerInfo); |
|
353 | handler = default(ResultHandlerInfo); | |
352 |
|
354 | |||
353 | lock (this) { |
|
355 | lock (this) { | |
354 | Debug.Assert(m_state != PromiseState.Unresolved); |
|
356 | Debug.Assert(m_state != PromiseState.Unresolved); | |
355 |
|
357 | |||
356 |
if (m_ |
|
358 | if (m_resultHandlers.Count > 0) { | |
357 |
handler = m_ |
|
359 | handler = m_resultHandlers.First.Value; | |
358 |
m_ |
|
360 | m_resultHandlers.RemoveFirst(); | |
359 | return true; |
|
361 | return true; | |
360 | } else { |
|
362 | } else { | |
361 | return false; |
|
363 | return false; | |
362 | } |
|
364 | } | |
363 | } |
|
365 | } | |
364 | } |
|
366 | } | |
365 |
|
367 | |||
366 | void AddHandler(ResultHandlerInfo handler) { |
|
368 | void AddHandler(ResultHandlerInfo handler) { | |
367 | bool invokeRequired = false; |
|
369 | bool invokeRequired = false; | |
368 |
|
370 | |||
369 | lock (this) { |
|
371 | lock (this) { | |
370 | if (m_state == PromiseState.Unresolved) |
|
372 | if (m_state == PromiseState.Unresolved) | |
371 |
m_ |
|
373 | m_resultHandlers.AddLast(handler); | |
372 | else |
|
374 | else | |
373 | invokeRequired = true; |
|
375 | invokeRequired = true; | |
374 | } |
|
376 | } | |
375 |
|
377 | |||
376 | // ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½Ρ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°ΡΡ ΡΠ°ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ |
|
378 | // ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½Ρ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°ΡΡ ΡΠ°ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ | |
377 | if (invokeRequired) |
|
379 | if (invokeRequired) | |
378 | InvokeHandler(handler); |
|
380 | InvokeHandler(handler); | |
379 | } |
|
381 | } | |
380 |
|
382 | |||
381 | void InvokeHandler(ResultHandlerInfo handler) { |
|
383 | void InvokeHandler(ResultHandlerInfo handler) { | |
382 | if (m_error == null) { |
|
384 | if (m_error == null) { | |
383 | try { |
|
385 | try { | |
384 | if (handler.resultHandler != null) |
|
386 | if (handler.resultHandler != null) | |
385 | handler.resultHandler(m_result); |
|
387 | handler.resultHandler(m_result); | |
386 | } catch { } |
|
388 | } catch { } | |
387 | } |
|
389 | } | |
388 |
|
390 | |||
389 | if (m_error != null) { |
|
391 | if (m_error != null) { | |
390 | try { |
|
392 | try { | |
391 | if (handler.errorHandler != null) |
|
393 | if (handler.errorHandler != null) | |
392 | handler.errorHandler(m_error); |
|
394 | handler.errorHandler(m_error); | |
393 | } catch { } |
|
395 | } catch { } | |
394 | } |
|
396 | } | |
395 | } |
|
397 | } | |
396 |
|
398 | |||
397 |
|
399 | |||
|
400 | ||||
|
401 | public bool IsExclusive { | |||
|
402 | get { | |||
|
403 | lock (m_lock) { | |||
|
404 | return m_childrenCount <= 1; | |||
|
405 | } | |||
|
406 | } | |||
|
407 | } | |||
|
408 | ||||
|
409 | public PromiseState State { | |||
|
410 | get { | |||
|
411 | lock (m_lock) { | |||
|
412 | return m_state; | |||
|
413 | } | |||
|
414 | } | |||
|
415 | } | |||
|
416 | ||||
|
417 | public bool Cancel(bool dependencies) { | |||
|
418 | bool result; | |||
|
419 | ||||
|
420 | lock (m_lock) { | |||
|
421 | if (m_state == PromiseState.Unresolved) { | |||
|
422 | m_state = PromiseState.Cancelled; | |||
|
423 | result = true; | |||
|
424 | } else { | |||
|
425 | result = false; | |||
|
426 | } | |||
|
427 | } | |||
|
428 | ||||
|
429 | if (dependencies && m_parent != null && m_parent.IsExclusive) { | |||
|
430 | // TODO syncronize IsExclusive, AddHandler, Cancel (maybe CancelExclusive) | |||
|
431 | m_parent.Cancel(true); | |||
|
432 | } | |||
|
433 | ||||
|
434 | if (result) { | |||
|
435 | // state has been changed to cancelled, new handlers can't be added | |||
|
436 | foreach (var handler in m_cancelHandlers) | |||
|
437 | handler(); | |||
|
438 | } | |||
|
439 | ||||
|
440 | return result; | |||
|
441 | } | |||
398 | } |
|
442 | } | |
399 | } |
|
443 | } |
General Comments 0
You need to be logged in to leave comments.
Login now