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