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