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