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