@@ -1,656 +1,656 | |||||
1 | using System; |
|
1 | using System; | |
2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
3 | using System.Reflection; |
|
3 | using System.Reflection; | |
4 | using System.Diagnostics; |
|
4 | using System.Diagnostics; | |
5 | using System.Threading; |
|
5 | using System.Threading; | |
6 | using Implab.Parallels; |
|
6 | using Implab.Parallels; | |
7 |
|
7 | |||
8 | namespace Implab { |
|
8 | namespace Implab { | |
9 |
|
9 | |||
10 | public delegate void ErrorHandler(Exception e); |
|
10 | public delegate void ErrorHandler(Exception e); | |
11 | public delegate T ErrorHandler<out T>(Exception e); |
|
11 | public delegate T ErrorHandler<out T>(Exception e); | |
12 | public delegate void ResultHandler<in T>(T result); |
|
12 | public delegate void ResultHandler<in T>(T result); | |
13 | public delegate TNew ResultMapper<in TSrc, out TNew>(TSrc result); |
|
13 | public delegate TNew ResultMapper<in TSrc, out TNew>(TSrc result); | |
14 | public delegate Promise<TNew> ChainedOperation<in TSrc, TNew>(TSrc result); |
|
14 | public delegate Promise<TNew> ChainedOperation<in TSrc, TNew>(TSrc result); | |
15 |
|
15 | |||
16 | /// <summary> |
|
16 | /// <summary> | |
17 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". |
|
17 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". | |
18 | /// </summary> |
|
18 | /// </summary> | |
19 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> |
|
19 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> | |
20 | /// <remarks> |
|
20 | /// <remarks> | |
21 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, |
|
21 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, | |
22 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ |
|
22 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ | |
23 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> |
|
23 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> | |
24 | /// <para> |
|
24 | /// <para> | |
25 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° |
|
25 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° | |
26 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. |
|
26 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. | |
27 | /// </para> |
|
27 | /// </para> | |
28 | /// <para> |
|
28 | /// <para> | |
29 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), |
|
29 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), | |
30 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ |
|
30 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ | |
31 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
31 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. | |
32 | /// </para> |
|
32 | /// </para> | |
33 | /// <para> |
|
33 | /// <para> | |
34 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, |
|
34 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, | |
35 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ |
|
35 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ | |
36 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² |
|
36 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² | |
37 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ |
|
37 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ | |
38 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
38 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. | |
39 | /// </para> |
|
39 | /// </para> | |
40 | /// <para> |
|
40 | /// <para> | |
41 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ |
|
41 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ | |
42 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ |
|
42 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ | |
43 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. |
|
43 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. | |
44 | /// </para> |
|
44 | /// </para> | |
45 | /// <para> |
|
45 | /// <para> | |
46 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ |
|
46 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ | |
47 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. |
|
47 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. | |
48 | /// </para> |
|
48 | /// </para> | |
49 | /// </remarks> |
|
49 | /// </remarks> | |
50 | public class Promise<T> : IPromise<T> { |
|
50 | public class Promise<T> : IPromise<T> { | |
51 |
|
51 | |||
52 | struct HandlerDescriptor { |
|
52 | protected struct HandlerDescriptor { | |
53 | public ResultHandler<T> resultHandler; |
|
53 | public ResultHandler<T> resultHandler; | |
54 | public ErrorHandler errorHandler; |
|
54 | public ErrorHandler errorHandler; | |
55 | public Action cancellHandler; |
|
55 | public Action cancellHandler; | |
56 |
|
56 | |||
57 | public void Resolve(T result) { |
|
57 | public void Resolve(T result) { | |
58 | if (resultHandler != null) |
|
58 | if (resultHandler != null) | |
59 | try { |
|
59 | try { | |
60 | resultHandler(result); |
|
60 | resultHandler(result); | |
61 | } catch (Exception e) { |
|
61 | } catch (Exception e) { | |
62 | Reject(e); |
|
62 | Reject(e); | |
63 | } |
|
63 | } | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | public void Reject(Exception err) { |
|
66 | public void Reject(Exception err) { | |
67 | if (errorHandler != null) |
|
67 | if (errorHandler != null) | |
68 | try { |
|
68 | try { | |
69 | errorHandler(err); |
|
69 | errorHandler(err); | |
70 | } catch { |
|
70 | } catch { | |
71 | } |
|
71 | } | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | public void Cancel() { |
|
74 | public void Cancel() { | |
75 | if (cancellHandler != null) |
|
75 | if (cancellHandler != null) | |
76 | try { |
|
76 | try { | |
77 | cancellHandler(); |
|
77 | cancellHandler(); | |
78 | } catch { |
|
78 | } catch { | |
79 | } |
|
79 | } | |
80 | } |
|
80 | } | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | const int UnresolvedSate = 0; |
|
83 | const int UnresolvedSate = 0; | |
84 | const int TransitionalState = 1; |
|
84 | const int TransitionalState = 1; | |
85 | const int SucceededState = 2; |
|
85 | const int SucceededState = 2; | |
86 | const int RejectedState = 3; |
|
86 | const int RejectedState = 3; | |
87 | const int CancelledState = 4; |
|
87 | const int CancelledState = 4; | |
88 |
|
88 | |||
89 | readonly IPromiseBase m_parent; |
|
89 | readonly IPromiseBase m_parent; | |
90 | readonly bool m_cancellable; |
|
90 | readonly bool m_cancellable; | |
91 |
|
91 | |||
92 | int m_childrenCount = 0; |
|
92 | int m_childrenCount = 0; | |
93 | int m_state; |
|
93 | int m_state; | |
94 | T m_result; |
|
94 | T m_result; | |
95 | Exception m_error; |
|
95 | Exception m_error; | |
96 |
|
96 | |||
97 | readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); |
|
97 | readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); | |
98 |
|
98 | |||
99 | public Promise() { |
|
99 | public Promise() { | |
100 | m_cancellable = true; |
|
100 | m_cancellable = true; | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | public Promise(IPromiseBase parent, bool cancellable) { |
|
103 | public Promise(IPromiseBase parent, bool cancellable) { | |
104 | m_cancellable = cancellable; |
|
104 | m_cancellable = cancellable; | |
105 | m_parent = parent; |
|
105 | m_parent = parent; | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 | void InternalCancel() { |
|
108 | void InternalCancel() { | |
109 | // don't try to cancel parent :) |
|
109 | // don't try to cancel parent :) | |
110 | Cancel(false); |
|
110 | Cancel(false); | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | bool BeginTransit() { |
|
113 | bool BeginTransit() { | |
114 | return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate); |
|
114 | return UnresolvedSate == Interlocked.CompareExchange(ref m_state, TransitionalState, UnresolvedSate); | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | void CompleteTransit(int state) { |
|
117 | void CompleteTransit(int state) { | |
118 | if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState)) |
|
118 | if (TransitionalState != Interlocked.CompareExchange(ref m_state, state, TransitionalState)) | |
119 | throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); |
|
119 | throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); | |
120 | } |
|
120 | } | |
121 |
|
121 | |||
122 | void WaitTransition() { |
|
122 | void WaitTransition() { | |
123 | while (m_state == TransitionalState) { |
|
123 | while (m_state == TransitionalState) { | |
124 | /* noop */ |
|
124 | /* noop */ | |
125 | } |
|
125 | } | |
126 | } |
|
126 | } | |
127 |
|
127 | |||
128 | public bool IsResolved { |
|
128 | public bool IsResolved { | |
129 | get { |
|
129 | get { | |
130 | return m_state > 1; |
|
130 | return m_state > 1; | |
131 | } |
|
131 | } | |
132 | } |
|
132 | } | |
133 |
|
133 | |||
134 | public bool IsCancelled { |
|
134 | public bool IsCancelled { | |
135 | get { |
|
135 | get { | |
136 | return m_state == CancelledState; |
|
136 | return m_state == CancelledState; | |
137 | } |
|
137 | } | |
138 | } |
|
138 | } | |
139 |
|
139 | |||
140 | /// <summary> |
|
140 | /// <summary> | |
141 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. |
|
141 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. | |
142 | /// </summary> |
|
142 | /// </summary> | |
143 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> |
|
143 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> | |
144 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
144 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> | |
145 | public void Resolve(T result) { |
|
145 | public void Resolve(T result) { | |
146 | if (BeginTransit()) { |
|
146 | if (BeginTransit()) { | |
147 | m_result = result; |
|
147 | m_result = result; | |
148 | CompleteTransit(SucceededState); |
|
148 | CompleteTransit(SucceededState); | |
149 | OnStateChanged(); |
|
149 | OnStateChanged(); | |
150 | } else { |
|
150 | } else { | |
151 | WaitTransition(); |
|
151 | WaitTransition(); | |
152 | if (m_state != CancelledState) |
|
152 | if (m_state != CancelledState) | |
153 | throw new InvalidOperationException("The promise is already resolved"); |
|
153 | throw new InvalidOperationException("The promise is already resolved"); | |
154 | } |
|
154 | } | |
155 | } |
|
155 | } | |
156 |
|
156 | |||
157 | /// <summary> |
|
157 | /// <summary> | |
158 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ |
|
158 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ | |
159 | /// </summary> |
|
159 | /// </summary> | |
160 | /// <remarks> |
|
160 | /// <remarks> | |
161 | /// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ² |
|
161 | /// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ² | |
162 | /// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ |
|
162 | /// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ | |
163 | /// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ. |
|
163 | /// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ. | |
164 | /// </remarks> |
|
164 | /// </remarks> | |
165 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> |
|
165 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> | |
166 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
166 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> | |
167 | public void Reject(Exception error) { |
|
167 | public void Reject(Exception error) { | |
168 | if (BeginTransit()) { |
|
168 | if (BeginTransit()) { | |
169 | m_error = error; |
|
169 | m_error = error; | |
170 | CompleteTransit(RejectedState); |
|
170 | CompleteTransit(RejectedState); | |
171 | OnStateChanged(); |
|
171 | OnStateChanged(); | |
172 | } else { |
|
172 | } else { | |
173 | WaitTransition(); |
|
173 | WaitTransition(); | |
174 | if (m_state == SucceededState) |
|
174 | if (m_state == SucceededState) | |
175 | throw new InvalidOperationException("The promise is already resolved"); |
|
175 | throw new InvalidOperationException("The promise is already resolved"); | |
176 | } |
|
176 | } | |
177 | } |
|
177 | } | |
178 |
|
178 | |||
179 | /// <summary> |
|
179 | /// <summary> | |
180 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. |
|
180 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. | |
181 | /// </summary> |
|
181 | /// </summary> | |
182 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> |
|
182 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> | |
183 | public bool Cancel() { |
|
183 | public bool Cancel() { | |
184 | return Cancel(true); |
|
184 | return Cancel(true); | |
185 | } |
|
185 | } | |
186 |
|
186 | |||
187 | /// <summary> |
|
187 | /// <summary> | |
188 | /// Adds new handlers to this promise. |
|
188 | /// Adds new handlers to this promise. | |
189 | /// </summary> |
|
189 | /// </summary> | |
190 | /// <param name="success">The handler of the successfully completed operation. |
|
190 | /// <param name="success">The handler of the successfully completed operation. | |
191 | /// This handler will recieve an operation result as a parameter.</param> |
|
191 | /// This handler will recieve an operation result as a parameter.</param> | |
192 | /// <param name="error">Handles an exception that may occur during the operation.</param> |
|
192 | /// <param name="error">Handles an exception that may occur during the operation.</param> | |
193 | /// <returns>The new promise chained to this one.</returns> |
|
193 | /// <returns>The new promise chained to this one.</returns> | |
194 | public IPromise<T> Then(ResultHandler<T> success, ErrorHandler error) { |
|
194 | public IPromise<T> Then(ResultHandler<T> success, ErrorHandler error) { | |
195 | if (success == null && error == null) |
|
195 | if (success == null && error == null) | |
196 | return this; |
|
196 | return this; | |
197 |
|
197 | |||
198 | var medium = new Promise<T>(this, true); |
|
198 | var medium = new Promise<T>(this, true); | |
199 |
|
199 | |||
200 | ResultHandler<T> resultHandler; |
|
200 | ResultHandler<T> resultHandler; | |
201 | if (success != null) |
|
201 | if (success != null) | |
202 | resultHandler = x => { |
|
202 | resultHandler = x => { | |
203 | success(x); |
|
203 | success(x); | |
204 | medium.Resolve(x); |
|
204 | medium.Resolve(x); | |
205 | }; |
|
205 | }; | |
206 | else |
|
206 | else | |
207 | resultHandler = medium.Resolve; |
|
207 | resultHandler = medium.Resolve; | |
208 |
|
208 | |||
209 | ErrorHandler errorHandler; |
|
209 | ErrorHandler errorHandler; | |
210 | if (error != null) |
|
210 | if (error != null) | |
211 | errorHandler = x => { |
|
211 | errorHandler = x => { | |
212 | // Π½Π΅ΡΠΌΠΎΡΡΡ Π½Π° ΡΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π±Π΅Π·ΠΎΠΏΠ°ΡΠ½ΠΎ, |
|
212 | // Π½Π΅ΡΠΌΠΎΡΡΡ Π½Π° ΡΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π±Π΅Π·ΠΎΠΏΠ°ΡΠ½ΠΎ, | |
213 | // Ρ.Π΅. Π²ΠΎΠ·Π½ΠΈΠΊΡΠΈΠ΅ Π² Π½Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½Ρ, Π½Π°ΠΌ Π½ΡΠΆΠ½ΠΎ |
|
213 | // Ρ.Π΅. Π²ΠΎΠ·Π½ΠΈΠΊΡΠΈΠ΅ Π² Π½Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΠΈ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½Ρ, Π½Π°ΠΌ Π½ΡΠΆΠ½ΠΎ | |
214 | // Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°ΡΡ, ΡΡΠΎ ΠΎΡΠΈΠ±ΠΊΠ° Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π° Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ |
|
214 | // Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°ΡΡ, ΡΡΠΎ ΠΎΡΠΈΠ±ΠΊΠ° Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π° Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ | |
215 | try { |
|
215 | try { | |
216 | error(x); |
|
216 | error(x); | |
217 | } catch { } |
|
217 | } catch { } | |
218 | medium.Reject(x); |
|
218 | medium.Reject(x); | |
219 | }; |
|
219 | }; | |
220 | else |
|
220 | else | |
221 | errorHandler = medium.Reject; |
|
221 | errorHandler = medium.Reject; | |
222 |
|
222 | |||
223 | AddHandler(resultHandler, errorHandler, medium.InternalCancel); |
|
223 | AddHandler(resultHandler, errorHandler, medium.InternalCancel); | |
224 |
|
224 | |||
225 | return medium; |
|
225 | return medium; | |
226 | } |
|
226 | } | |
227 |
|
227 | |||
228 | public IPromiseBase Then(Action success,ErrorHandler error) |
|
228 | public IPromiseBase Then(Action success,ErrorHandler error) | |
229 | { |
|
229 | { | |
230 | return Then(x => success(), error); |
|
230 | return Then(x => success(), error); | |
231 | } |
|
231 | } | |
232 |
|
232 | |||
233 | public IPromiseBase Then(Action success) |
|
233 | public IPromiseBase Then(Action success) | |
234 | { |
|
234 | { | |
235 | return Then(success); |
|
235 | return Then(success); | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 | /// <summary> |
|
238 | /// <summary> | |
239 | /// Adds new handlers to this promise. |
|
239 | /// Adds new handlers to this promise. | |
240 | /// </summary> |
|
240 | /// </summary> | |
241 | /// <param name="success">The handler of the successfully completed operation. |
|
241 | /// <param name="success">The handler of the successfully completed operation. | |
242 | /// This handler will recieve an operation result as a parameter.</param> |
|
242 | /// This handler will recieve an operation result as a parameter.</param> | |
243 | /// <param name="error">Handles an exception that may occur during the operation and returns the value which will be used as the result of the operation.</param> |
|
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 | /// <returns>The new promise chained to this one.</returns> |
|
244 | /// <returns>The new promise chained to this one.</returns> | |
245 | public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { |
|
245 | public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { | |
246 | if (success == null && error == null) |
|
246 | if (success == null && error == null) | |
247 | return this; |
|
247 | return this; | |
248 |
|
248 | |||
249 | var medium = new Promise<T>(this, true); |
|
249 | var medium = new Promise<T>(this, true); | |
250 |
|
250 | |||
251 | ResultHandler<T> resultHandler; |
|
251 | ResultHandler<T> resultHandler; | |
252 | ErrorHandler errorHandler; |
|
252 | ErrorHandler errorHandler; | |
253 |
|
253 | |||
254 | if (success != null) |
|
254 | if (success != null) | |
255 | resultHandler = x => { |
|
255 | resultHandler = x => { | |
256 | success(x); |
|
256 | success(x); | |
257 | medium.Resolve(x); |
|
257 | medium.Resolve(x); | |
258 | }; |
|
258 | }; | |
259 | else |
|
259 | else | |
260 | resultHandler = medium.Resolve; |
|
260 | resultHandler = medium.Resolve; | |
261 |
|
261 | |||
262 | if (error != null) |
|
262 | if (error != null) | |
263 | errorHandler = x => { |
|
263 | errorHandler = x => { | |
264 | try { |
|
264 | try { | |
265 | medium.Resolve(error(x)); |
|
265 | medium.Resolve(error(x)); | |
266 | } catch(Exception e) { |
|
266 | } catch(Exception e) { | |
267 | medium.Reject(e); |
|
267 | medium.Reject(e); | |
268 | } |
|
268 | } | |
269 | }; |
|
269 | }; | |
270 | else |
|
270 | else | |
271 | errorHandler = medium.Reject; |
|
271 | errorHandler = medium.Reject; | |
272 |
|
272 | |||
273 | AddHandler(resultHandler, errorHandler, medium.InternalCancel); |
|
273 | AddHandler(resultHandler, errorHandler, medium.InternalCancel); | |
274 |
|
274 | |||
275 | return medium; |
|
275 | return medium; | |
276 | } |
|
276 | } | |
277 |
|
277 | |||
278 |
|
278 | |||
279 | public IPromise<T> Then(ResultHandler<T> success) { |
|
279 | public IPromise<T> Then(ResultHandler<T> success) { | |
280 | if (success == null) |
|
280 | if (success == null) | |
281 | return this; |
|
281 | return this; | |
282 |
|
282 | |||
283 | var medium = new Promise<T>(this, true); |
|
283 | var medium = new Promise<T>(this, true); | |
284 |
|
284 | |||
285 | ResultHandler<T> resultHandler; |
|
285 | ResultHandler<T> resultHandler; | |
286 |
|
286 | |||
287 | if (success != null) |
|
287 | if (success != null) | |
288 | resultHandler = x => { |
|
288 | resultHandler = x => { | |
289 | success(x); |
|
289 | success(x); | |
290 | medium.Resolve(x); |
|
290 | medium.Resolve(x); | |
291 | }; |
|
291 | }; | |
292 | else |
|
292 | else | |
293 | resultHandler = medium.Resolve; |
|
293 | resultHandler = medium.Resolve; | |
294 |
|
294 | |||
295 | AddHandler(resultHandler, medium.Reject, medium.InternalCancel); |
|
295 | AddHandler(resultHandler, medium.Reject, medium.InternalCancel); | |
296 |
|
296 | |||
297 | return medium; |
|
297 | return medium; | |
298 | } |
|
298 | } | |
299 |
|
299 | |||
300 | public IPromise<T> Error(ErrorHandler error) { |
|
300 | public IPromise<T> Error(ErrorHandler error) { | |
301 | return Then((ResultHandler<T>)null, error); |
|
301 | return Then((ResultHandler<T>)null, error); | |
302 | } |
|
302 | } | |
303 |
|
303 | |||
304 | /// <summary> |
|
304 | /// <summary> | |
305 | /// Handles error and allows to keep the promise. |
|
305 | /// Handles error and allows to keep the promise. | |
306 | /// </summary> |
|
306 | /// </summary> | |
307 | /// <remarks> |
|
307 | /// <remarks> | |
308 | /// If the specified handler throws an exception, this exception will be used to reject the promise. |
|
308 | /// If the specified handler throws an exception, this exception will be used to reject the promise. | |
309 | /// </remarks> |
|
309 | /// </remarks> | |
310 | /// <param name="handler">The error handler which returns the result of the promise.</param> |
|
310 | /// <param name="handler">The error handler which returns the result of the promise.</param> | |
311 | /// <returns>New promise.</returns> |
|
311 | /// <returns>New promise.</returns> | |
312 | public IPromise<T> Error(ErrorHandler<T> handler) { |
|
312 | public IPromise<T> Error(ErrorHandler<T> handler) { | |
313 | if (handler == null) |
|
313 | if (handler == null) | |
314 | return this; |
|
314 | return this; | |
315 |
|
315 | |||
316 | var medium = new Promise<T>(this, true); |
|
316 | var medium = new Promise<T>(this, true); | |
317 |
|
317 | |||
318 | AddHandler( |
|
318 | AddHandler( | |
319 | x => medium.Resolve(x), |
|
319 | x => medium.Resolve(x), | |
320 | e => { |
|
320 | e => { | |
321 | try { |
|
321 | try { | |
322 | medium.Resolve(handler(e)); |
|
322 | medium.Resolve(handler(e)); | |
323 | } catch (Exception e2) { |
|
323 | } catch (Exception e2) { | |
324 | medium.Reject(e2); |
|
324 | medium.Reject(e2); | |
325 | } |
|
325 | } | |
326 | }, |
|
326 | }, | |
327 | medium.InternalCancel |
|
327 | medium.InternalCancel | |
328 | ); |
|
328 | ); | |
329 |
|
329 | |||
330 | return medium; |
|
330 | return medium; | |
331 | } |
|
331 | } | |
332 |
|
332 | |||
333 | public IPromise<T> Anyway(Action handler) { |
|
333 | public IPromise<T> Anyway(Action handler) { | |
334 | if (handler == null) |
|
334 | if (handler == null) | |
335 | return this; |
|
335 | return this; | |
336 |
|
336 | |||
337 | var medium = new Promise<T>(); |
|
337 | var medium = new Promise<T>(); | |
338 |
|
338 | |||
339 | AddHandler( |
|
339 | AddHandler( | |
340 | x => { |
|
340 | x => { | |
341 | // to avoid handler being called multiple times we handle exception by ourselfs |
|
341 | // to avoid handler being called multiple times we handle exception by ourselfs | |
342 | try { |
|
342 | try { | |
343 | handler(); |
|
343 | handler(); | |
344 | medium.Resolve(x); |
|
344 | medium.Resolve(x); | |
345 | } catch (Exception e) { |
|
345 | } catch (Exception e) { | |
346 | medium.Reject(e); |
|
346 | medium.Reject(e); | |
347 | } |
|
347 | } | |
348 | }, |
|
348 | }, | |
349 |
|
349 | |||
350 | e => { |
|
350 | e => { | |
351 | try { |
|
351 | try { | |
352 | handler(); |
|
352 | handler(); | |
353 | } catch { } |
|
353 | } catch { } | |
354 | medium.Reject(e); |
|
354 | medium.Reject(e); | |
355 | }, |
|
355 | }, | |
356 |
|
356 | |||
357 | medium.InternalCancel |
|
357 | medium.InternalCancel | |
358 | ); |
|
358 | ); | |
359 |
|
359 | |||
360 | return medium; |
|
360 | return medium; | |
361 | } |
|
361 | } | |
362 |
|
362 | |||
363 | /// <summary> |
|
363 | /// <summary> | |
364 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. |
|
364 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. | |
365 | /// </summary> |
|
365 | /// </summary> | |
366 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> |
|
366 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> | |
367 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> |
|
367 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> | |
368 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
368 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ | |
369 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
369 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
370 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> |
|
370 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> | |
371 | public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { |
|
371 | public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { | |
372 | if (mapper == null) |
|
372 | if (mapper == null) | |
373 | throw new ArgumentNullException("mapper"); |
|
373 | throw new ArgumentNullException("mapper"); | |
374 |
|
374 | |||
375 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
375 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ | |
376 | var chained = new Promise<TNew>(); |
|
376 | var chained = new Promise<TNew>(); | |
377 |
|
377 | |||
378 | ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result)); |
|
378 | ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result)); | |
379 | ErrorHandler errorHandler = delegate(Exception e) { |
|
379 | ErrorHandler errorHandler = delegate(Exception e) { | |
380 | if (error != null) |
|
380 | if (error != null) | |
381 | try { |
|
381 | try { | |
382 | error(e); |
|
382 | error(e); | |
383 | } catch { } |
|
383 | } catch { } | |
384 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
384 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ | |
385 | chained.Reject(e); |
|
385 | chained.Reject(e); | |
386 | }; |
|
386 | }; | |
387 |
|
387 | |||
388 |
|
388 | |||
389 | AddHandler( |
|
389 | AddHandler( | |
390 | resultHandler, |
|
390 | resultHandler, | |
391 | errorHandler, |
|
391 | errorHandler, | |
392 | chained.InternalCancel |
|
392 | chained.InternalCancel | |
393 | ); |
|
393 | ); | |
394 |
|
394 | |||
395 | return chained; |
|
395 | return chained; | |
396 | } |
|
396 | } | |
397 |
|
397 | |||
398 | public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { |
|
398 | public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { | |
399 | return Map(mapper, null); |
|
399 | return Map(mapper, null); | |
400 | } |
|
400 | } | |
401 |
|
401 | |||
402 | /// <summary> |
|
402 | /// <summary> | |
403 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ |
|
403 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ | |
404 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ |
|
404 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ | |
405 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
405 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. | |
406 | /// </summary> |
|
406 | /// </summary> | |
407 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> |
|
407 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> | |
408 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> |
|
408 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> | |
409 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
409 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ | |
410 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
410 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
411 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> |
|
411 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> | |
412 | public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { |
|
412 | public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { | |
413 |
|
413 | |||
414 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ |
|
414 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ | |
415 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. |
|
415 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. | |
416 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ |
|
416 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ | |
417 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. |
|
417 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. | |
418 | var medium = new Promise<TNew>(this, true); |
|
418 | var medium = new Promise<TNew>(this, true); | |
419 |
|
419 | |||
420 | ResultHandler<T> resultHandler = delegate(T result) { |
|
420 | ResultHandler<T> resultHandler = delegate(T result) { | |
421 | if (medium.IsCancelled) |
|
421 | if (medium.IsCancelled) | |
422 | return; |
|
422 | return; | |
423 |
|
423 | |||
424 | var promise = chained(result); |
|
424 | var promise = chained(result); | |
425 |
|
425 | |||
426 | // notify chained operation that it's not needed |
|
426 | // notify chained operation that it's not needed | |
427 | medium.Cancelled(() => promise.Cancel()); |
|
427 | medium.Cancelled(() => promise.Cancel()); | |
428 | promise.Then( |
|
428 | promise.Then( | |
429 | x => medium.Resolve(x), |
|
429 | x => medium.Resolve(x), | |
430 | e => medium.Reject(e) |
|
430 | e => medium.Reject(e) | |
431 | ); |
|
431 | ); | |
432 | }; |
|
432 | }; | |
433 |
|
433 | |||
434 | ErrorHandler errorHandler = delegate(Exception e) { |
|
434 | ErrorHandler errorHandler = delegate(Exception e) { | |
435 | if (error != null) |
|
435 | if (error != null) | |
436 | error(e); |
|
436 | error(e); | |
437 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
437 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ | |
438 | medium.Reject(e); |
|
438 | medium.Reject(e); | |
439 | }; |
|
439 | }; | |
440 |
|
440 | |||
441 | AddHandler( |
|
441 | AddHandler( | |
442 | resultHandler, |
|
442 | resultHandler, | |
443 | errorHandler, |
|
443 | errorHandler, | |
444 | medium.InternalCancel |
|
444 | medium.InternalCancel | |
445 | ); |
|
445 | ); | |
446 |
|
446 | |||
447 | return medium; |
|
447 | return medium; | |
448 | } |
|
448 | } | |
449 |
|
449 | |||
450 | public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { |
|
450 | public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { | |
451 | return Chain(chained, null); |
|
451 | return Chain(chained, null); | |
452 | } |
|
452 | } | |
453 |
|
453 | |||
454 | public IPromise<T> Cancelled(Action handler) { |
|
454 | public IPromise<T> Cancelled(Action handler) { | |
455 | AddHandler(null, null, handler); |
|
455 | AddHandler(null, null, handler); | |
456 | return this; |
|
456 | return this; | |
457 | } |
|
457 | } | |
458 |
|
458 | |||
459 | /// <summary> |
|
459 | /// <summary> | |
460 | /// Adds the specified handler for all cases (success, error, cancel) |
|
460 | /// Adds the specified handler for all cases (success, error, cancel) | |
461 | /// </summary> |
|
461 | /// </summary> | |
462 | /// <param name="handler">The handler that will be called anyway</param> |
|
462 | /// <param name="handler">The handler that will be called anyway</param> | |
463 | /// <returns>self</returns> |
|
463 | /// <returns>self</returns> | |
464 | public IPromise<T> Finally(Action handler) { |
|
464 | public IPromise<T> Finally(Action handler) { | |
465 | if (handler == null) |
|
465 | if (handler == null) | |
466 | throw new ArgumentNullException("handler"); |
|
466 | throw new ArgumentNullException("handler"); | |
467 | AddHandler( |
|
467 | AddHandler( | |
468 | x => handler(), |
|
468 | x => handler(), | |
469 | e => handler(), |
|
469 | e => handler(), | |
470 | handler |
|
470 | handler | |
471 | ); |
|
471 | ); | |
472 | return this; |
|
472 | return this; | |
473 | } |
|
473 | } | |
474 |
|
474 | |||
475 | /// <summary> |
|
475 | /// <summary> | |
476 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ |
|
476 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ | |
477 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. |
|
477 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. | |
478 | /// </summary> |
|
478 | /// </summary> | |
479 | /// <remarks> |
|
479 | /// <remarks> | |
480 | /// <para> |
|
480 | /// <para> | |
481 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, |
|
481 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, | |
482 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ |
|
482 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ | |
483 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ |
|
483 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ | |
484 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° |
|
484 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° | |
485 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
485 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. | |
486 | /// </para> |
|
486 | /// </para> | |
487 | /// <para> |
|
487 | /// <para> | |
488 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ |
|
488 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ | |
489 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ |
|
489 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ | |
490 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ |
|
490 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ | |
491 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. |
|
491 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. | |
492 | /// </para> |
|
492 | /// </para> | |
493 | /// </remarks> |
|
493 | /// </remarks> | |
494 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> |
|
494 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> | |
495 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> |
|
495 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> | |
496 | public T Join(int timeout) { |
|
496 | public T Join(int timeout) { | |
497 | var evt = new ManualResetEvent(false); |
|
497 | var evt = new ManualResetEvent(false); | |
498 | Anyway(() => evt.Set()); |
|
498 | Anyway(() => evt.Set()); | |
499 | Cancelled(() => evt.Set()); |
|
499 | Cancelled(() => evt.Set()); | |
500 |
|
500 | |||
501 | if (!evt.WaitOne(timeout, true)) |
|
501 | if (!evt.WaitOne(timeout, true)) | |
502 | throw new TimeoutException(); |
|
502 | throw new TimeoutException(); | |
503 |
|
503 | |||
504 | switch (m_state) { |
|
504 | switch (m_state) { | |
505 | case SucceededState: |
|
505 | case SucceededState: | |
506 | return m_result; |
|
506 | return m_result; | |
507 | case CancelledState: |
|
507 | case CancelledState: | |
508 | throw new OperationCanceledException(); |
|
508 | throw new OperationCanceledException(); | |
509 | case RejectedState: |
|
509 | case RejectedState: | |
510 | throw new TargetInvocationException(m_error); |
|
510 | throw new TargetInvocationException(m_error); | |
511 | default: |
|
511 | default: | |
512 | throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); |
|
512 | throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); | |
513 | } |
|
513 | } | |
514 | } |
|
514 | } | |
515 |
|
515 | |||
516 | public T Join() { |
|
516 | public T Join() { | |
517 | return Join(Timeout.Infinite); |
|
517 | return Join(Timeout.Infinite); | |
518 | } |
|
518 | } | |
519 |
|
519 | |||
520 | void AddHandler(ResultHandler<T> success, ErrorHandler error, Action cancel) { |
|
520 | void AddHandler(ResultHandler<T> success, ErrorHandler error, Action cancel) { | |
521 | Interlocked.Increment(ref m_childrenCount); |
|
521 | Interlocked.Increment(ref m_childrenCount); | |
522 |
|
522 | |||
523 | HandlerDescriptor handler = new HandlerDescriptor { |
|
523 | HandlerDescriptor handler = new HandlerDescriptor { | |
524 | resultHandler = success, |
|
524 | resultHandler = success, | |
525 | errorHandler = error, |
|
525 | errorHandler = error, | |
526 | cancellHandler = cancel |
|
526 | cancellHandler = cancel | |
527 | }; |
|
527 | }; | |
528 |
|
528 | |||
529 | bool queued; |
|
529 | bool queued; | |
530 |
|
530 | |||
531 | if (!IsResolved) { |
|
531 | if (!IsResolved) { | |
532 | m_handlers.Enqueue(handler); |
|
532 | m_handlers.Enqueue(handler); | |
533 | queued = true; |
|
533 | queued = true; | |
534 | } else { |
|
534 | } else { | |
535 | // the promise is in resolved state, just invoke the handled with minimum overhead |
|
535 | // the promise is in resolved state, just invoke the handled with minimum overhead | |
536 | queued = false; |
|
536 | queued = false; | |
537 | InvokeHandler(handler); |
|
537 | InvokeHandler(handler); | |
538 | } |
|
538 | } | |
539 |
|
539 | |||
540 | if (queued && IsResolved && m_handlers.TryDequeue(out handler)) |
|
540 | if (queued && IsResolved && m_handlers.TryDequeue(out handler)) | |
541 | // if the promise have been resolved while we was adding handler to the queue |
|
541 | // if the promise have been resolved while we was adding handler to the queue | |
542 | // we can't guarantee that someone is still processing it |
|
542 | // we can't guarantee that someone is still processing it | |
543 | // therefore we will fetch a handler from the queue and execute it |
|
543 | // therefore we will fetch a handler from the queue and execute it | |
544 | // note that fetched handler may be not the one that we have added |
|
544 | // note that fetched handler may be not the one that we have added | |
545 | // even we can fetch no handlers at all :) |
|
545 | // even we can fetch no handlers at all :) | |
546 | InvokeHandler(handler); |
|
546 | InvokeHandler(handler); | |
547 | } |
|
547 | } | |
548 |
|
548 | |||
549 | protected virtual void InvokeHandler(HandlerDescriptor handler) { |
|
549 | protected virtual void InvokeHandler(HandlerDescriptor handler) { | |
550 | switch (m_state) { |
|
550 | switch (m_state) { | |
551 | case SucceededState: |
|
551 | case SucceededState: | |
552 | handler.Resolve(m_result); |
|
552 | handler.Resolve(m_result); | |
553 | break; |
|
553 | break; | |
554 | case RejectedState: |
|
554 | case RejectedState: | |
555 | handler.Reject(m_error); |
|
555 | handler.Reject(m_error); | |
556 | break; |
|
556 | break; | |
557 | case CancelledState: |
|
557 | case CancelledState: | |
558 | handler.Cancel(); |
|
558 | handler.Cancel(); | |
559 | break; |
|
559 | break; | |
560 | default: |
|
560 | default: | |
561 | // do nothing |
|
561 | // do nothing | |
562 | return; |
|
562 | return; | |
563 | } |
|
563 | } | |
564 | } |
|
564 | } | |
565 |
|
565 | |||
566 | protected virtual void OnStateChanged() { |
|
566 | protected virtual void OnStateChanged() { | |
567 | HandlerDescriptor handler; |
|
567 | HandlerDescriptor handler; | |
568 | while (m_handlers.TryDequeue(out handler)) |
|
568 | while (m_handlers.TryDequeue(out handler)) | |
569 | InvokeHandler(handler); |
|
569 | InvokeHandler(handler); | |
570 | } |
|
570 | } | |
571 |
|
571 | |||
572 | public bool IsExclusive { |
|
572 | public bool IsExclusive { | |
573 | get { |
|
573 | get { | |
574 | return m_childrenCount <= 1; |
|
574 | return m_childrenCount <= 1; | |
575 | } |
|
575 | } | |
576 | } |
|
576 | } | |
577 |
|
577 | |||
578 | protected bool Cancel(bool dependencies) { |
|
578 | protected bool Cancel(bool dependencies) { | |
579 | if (BeginTransit()) { |
|
579 | if (BeginTransit()) { | |
580 | CompleteTransit(CancelledState); |
|
580 | CompleteTransit(CancelledState); | |
581 | OnStateChanged(); |
|
581 | OnStateChanged(); | |
582 |
|
582 | |||
583 | if (dependencies && m_parent != null && m_parent.IsExclusive) |
|
583 | if (dependencies && m_parent != null && m_parent.IsExclusive) | |
584 | m_parent.Cancel(); |
|
584 | m_parent.Cancel(); | |
585 |
|
585 | |||
586 | return true; |
|
586 | return true; | |
587 | } else { |
|
587 | } else { | |
588 | return false; |
|
588 | return false; | |
589 | } |
|
589 | } | |
590 | } |
|
590 | } | |
591 |
|
591 | |||
592 | /// <summary> |
|
592 | /// <summary> | |
593 | /// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
593 | /// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. | |
594 | /// ΠΡΠ»ΠΈ Ρ ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
594 | /// ΠΡΠ»ΠΈ Ρ ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. | |
595 | /// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½. |
|
595 | /// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½. | |
596 | /// </summary> |
|
596 | /// </summary> | |
597 | /// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param> |
|
597 | /// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param> | |
598 | /// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns> |
|
598 | /// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns> | |
599 | /// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception> |
|
599 | /// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception> | |
600 | public static Promise<T[]> CreateComposite(IList<Promise<T>> promises) { |
|
600 | public static Promise<T[]> CreateComposite(IList<Promise<T>> promises) { | |
601 | if (promises == null) |
|
601 | if (promises == null) | |
602 | throw new ArgumentNullException(); |
|
602 | throw new ArgumentNullException(); | |
603 |
|
603 | |||
604 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
604 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ | |
605 | var result = new T[promises.Count]; |
|
605 | var result = new T[promises.Count]; | |
606 | var promise = new Promise<T[]>(); |
|
606 | var promise = new Promise<T[]>(); | |
607 |
|
607 | |||
608 | // special case |
|
608 | // special case | |
609 | if (promises.Count == 0) { |
|
609 | if (promises.Count == 0) { | |
610 | promise.Resolve(result); |
|
610 | promise.Resolve(result); | |
611 | return promise; |
|
611 | return promise; | |
612 | } |
|
612 | } | |
613 |
|
613 | |||
614 | int pending = promises.Count; |
|
614 | int pending = promises.Count; | |
615 |
|
615 | |||
616 | for (int i = 0; i < promises.Count; i++) { |
|
616 | for (int i = 0; i < promises.Count; i++) { | |
617 | var dest = i; |
|
617 | var dest = i; | |
618 |
|
618 | |||
619 | promises[i].Then( |
|
619 | promises[i].Then( | |
620 | x => { |
|
620 | x => { | |
621 | result[dest] = x; |
|
621 | result[dest] = x; | |
622 | if(Interlocked.Decrement(ref pending) == 0) |
|
622 | if(Interlocked.Decrement(ref pending) == 0) | |
623 | promise.Resolve(result); |
|
623 | promise.Resolve(result); | |
624 | }, |
|
624 | }, | |
625 | e => promise.Reject(e) |
|
625 | e => promise.Reject(e) | |
626 | ); |
|
626 | ); | |
627 | } |
|
627 | } | |
628 |
|
628 | |||
629 | promise.Cancelled( |
|
629 | promise.Cancelled( | |
630 | () => { |
|
630 | () => { | |
631 | foreach(var d in promises) |
|
631 | foreach(var d in promises) | |
632 | if(d.IsExclusive) |
|
632 | if(d.IsExclusive) | |
633 | d.Cancel(); |
|
633 | d.Cancel(); | |
634 | } |
|
634 | } | |
635 | ); |
|
635 | ); | |
636 |
|
636 | |||
637 | return promise; |
|
637 | return promise; | |
638 | } |
|
638 | } | |
639 |
|
639 | |||
640 | public static Promise<T> ResultToPromise(T result) { |
|
640 | public static Promise<T> ResultToPromise(T result) { | |
641 | var p = new Promise<T>(); |
|
641 | var p = new Promise<T>(); | |
642 | p.Resolve(result); |
|
642 | p.Resolve(result); | |
643 | return p; |
|
643 | return p; | |
644 | } |
|
644 | } | |
645 |
|
645 | |||
646 | public static Promise<T> ExceptionToPromise(Exception error) { |
|
646 | public static Promise<T> ExceptionToPromise(Exception error) { | |
647 | if (error == null) |
|
647 | if (error == null) | |
648 | throw new ArgumentNullException(); |
|
648 | throw new ArgumentNullException(); | |
649 |
|
649 | |||
650 | var p = new Promise<T>(); |
|
650 | var p = new Promise<T>(); | |
651 | p.Reject(error); |
|
651 | p.Reject(error); | |
652 | return p; |
|
652 | return p; | |
653 | } |
|
653 | } | |
654 |
|
654 | |||
655 | } |
|
655 | } | |
656 | } |
|
656 | } |
General Comments 0
You need to be logged in to leave comments.
Login now