@@ -1,33 +1,33 | |||
|
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 | { |
|
8 | 8 | public interface IPromise |
|
9 | 9 | { |
|
10 | 10 | /// <summary> |
|
11 | 11 | /// Check whereather the promise has no more than one dependent promise. |
|
12 | 12 | /// </summary> |
|
13 | 13 | bool IsExclusive |
|
14 | 14 | { |
|
15 | 15 | get; |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | /// <summary> |
|
19 | 19 | /// The current state of the promise. |
|
20 | 20 | /// </summary> |
|
21 | 21 | PromiseState State |
|
22 | 22 | { |
|
23 | 23 | get; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | /// <summary> |
|
27 | 27 | /// Tries to cancel the promise or the complete chain. |
|
28 | 28 | /// </summary> |
|
29 |
/// <param name="dependencies">Try to cancel the parent promise |
|
|
29 | /// <param name="dependencies">Try to cancel the whole promise chain, the parent promise will be cancelled only if it has only one promise</param> | |
|
30 | 30 | /// <returns></returns> |
|
31 | 31 | bool Cancel(bool dependencies); |
|
32 | 32 | } |
|
33 | 33 | } |
@@ -1,399 +1,443 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Reflection; |
|
5 | 5 | using System.Text; |
|
6 | 6 | using System.Diagnostics; |
|
7 | 7 | using System.Threading; |
|
8 | 8 | |
|
9 | 9 | namespace Implab { |
|
10 | 10 | |
|
11 | 11 | public delegate void ErrorHandler(Exception e); |
|
12 | 12 | |
|
13 | 13 | public delegate void ResultHandler<T>(T result); |
|
14 | 14 | public delegate TNew ResultMapper<TSrc, TNew>(TSrc result); |
|
15 | 15 | public delegate Promise<TNew> ChainedOperation<TSrc, TNew>(TSrc result); |
|
16 | 16 | |
|
17 | 17 | /// <summary> |
|
18 | 18 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". |
|
19 | 19 | /// </summary> |
|
20 | 20 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> |
|
21 | 21 | /// <remarks> |
|
22 | 22 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, |
|
23 | 23 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ |
|
24 | 24 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> |
|
25 | 25 | /// <para> |
|
26 | 26 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° |
|
27 | 27 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. |
|
28 | 28 | /// </para> |
|
29 | 29 | /// <para> |
|
30 | 30 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), |
|
31 | 31 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ |
|
32 | 32 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
33 | 33 | /// </para> |
|
34 | 34 | /// <para> |
|
35 | 35 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, |
|
36 | 36 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ |
|
37 | 37 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² |
|
38 | 38 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ |
|
39 | 39 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
40 | 40 | /// </para> |
|
41 | 41 | /// <para> |
|
42 | 42 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ |
|
43 | 43 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ |
|
44 | 44 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. |
|
45 | 45 | /// </para> |
|
46 | 46 | /// <para> |
|
47 | 47 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ |
|
48 | 48 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. |
|
49 | 49 | /// </para> |
|
50 | 50 | /// </remarks> |
|
51 | 51 | public class Promise<T>: IPromise { |
|
52 | 52 | |
|
53 | 53 | struct ResultHandlerInfo { |
|
54 | 54 | public ResultHandler<T> resultHandler; |
|
55 | 55 | public ErrorHandler errorHandler; |
|
56 | public Action cancelHandler; | |
|
57 | 56 | } |
|
58 | 57 | |
|
59 | LinkedList<ResultHandlerInfo> m_handlersChain = new LinkedList<ResultHandlerInfo>(); | |
|
58 | IPromise m_parent; | |
|
59 | ||
|
60 | LinkedList<ResultHandlerInfo> m_resultHandlers = new LinkedList<ResultHandlerInfo>(); | |
|
61 | LinkedList<Action> m_cancelHandlers = new LinkedList<Action>(); | |
|
62 | ||
|
63 | object m_lock = new Object(); | |
|
64 | bool m_cancellable; | |
|
65 | ||
|
60 | 66 | PromiseState m_state; |
|
61 | bool m_cancellable; | |
|
62 | 67 | T m_result; |
|
63 | 68 | Exception m_error; |
|
64 | IPromise m_parent; | |
|
69 | ||
|
65 | 70 | int m_childrenCount; |
|
66 | 71 | |
|
67 | 72 | public Promise() { |
|
68 | 73 | m_cancellable = true; |
|
69 | 74 | } |
|
70 | 75 | |
|
71 | public Promise(IPromise parent, bool cancellable) | |
|
72 | { | |
|
76 | public Promise(IPromise parent, bool cancellable) { | |
|
73 | 77 | m_cancellable = cancellable; |
|
74 | 78 | m_parent = parent; |
|
75 | 79 | } |
|
76 | 80 | |
|
77 | 81 | /// <summary> |
|
78 | 82 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅, Π²ΠΎΠ·Π½ΠΈΠΊΠ°ΡΡΠ΅Π΅ ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
79 | 83 | /// </summary> |
|
80 | 84 | /// <description> |
|
81 | 85 | /// ΠΠ°ΠΊ ΠΏΡΠ°Π²ΠΈΠ»ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠ°, Π²ΡΠΏΠΎΠ»Π½ΡΡΡΠ΅Π³ΠΎ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎ ΡΠΎΠΌ, ΡΡΠΎ Π΅Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΎΡΠΌΠ΅Π½ΠΈΡΡ. |
|
82 | 86 | /// </description> |
|
83 | 87 | public event EventHandler Cancelled; |
|
84 | 88 | |
|
85 | 89 | /// <summary> |
|
86 | 90 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. |
|
87 | 91 | /// </summary> |
|
88 | 92 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> |
|
89 | 93 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
90 | 94 | public void Resolve(T result) { |
|
91 | 95 | lock (this) { |
|
92 | 96 | if (m_state == PromiseState.Cancelled) |
|
93 | 97 | return; |
|
94 | 98 | if (m_state != PromiseState.Unresolved) |
|
95 | 99 | throw new InvalidOperationException("The promise is already resolved"); |
|
96 | 100 | m_result = result; |
|
97 | 101 | m_state = PromiseState.Resolved; |
|
98 | 102 | } |
|
99 | 103 | |
|
100 | ResultHandlerInfo handler; | |
|
104 | // state has been changed to rejected new handlers can't be added | |
|
105 | ||
|
106 | foreach (var handler in m_resultHandlers) | |
|
107 | InvokeHandler(handler); | |
|
108 | ||
|
109 | /* ResultHandlerInfo handler; | |
|
101 | 110 | while (FetchNextHandler(out handler)) |
|
102 |
|
|
|
111 | InvokeHandler(handler); */ | |
|
103 | 112 | } |
|
104 | 113 | |
|
105 | 114 | /// <summary> |
|
106 | 115 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ |
|
107 | 116 | /// </summary> |
|
108 | 117 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> |
|
109 | 118 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
110 | 119 | public void Reject(Exception error) { |
|
111 | 120 | lock (this) { |
|
112 | 121 | if (m_state == PromiseState.Cancelled) |
|
113 | 122 | return; |
|
114 | 123 | if (m_state != PromiseState.Unresolved) |
|
115 | 124 | throw new InvalidOperationException("The promise is already resolved"); |
|
116 | 125 | m_error = error; |
|
117 | 126 | m_state = PromiseState.Rejected; |
|
118 | 127 | } |
|
119 | 128 | |
|
120 | ResultHandlerInfo handler; | |
|
129 | // state has been changed to rejected new handlers can't be added | |
|
130 | ||
|
131 | foreach (var handler in m_resultHandlers) | |
|
132 | InvokeHandler(handler); | |
|
133 | ||
|
134 | /*ResultHandlerInfo handler; | |
|
121 | 135 | while (FetchNextHandler(out handler)) |
|
122 |
|
|
|
136 | InvokeHandler(handler);*/ | |
|
123 | 137 | } |
|
124 | 138 | |
|
125 | 139 | /// <summary> |
|
126 | 140 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. |
|
127 | 141 | /// </summary> |
|
128 | 142 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> |
|
129 | 143 | public bool Cancel() { |
|
130 | lock (this) { | |
|
131 | if (m_state == PromiseState.Unresolved && m_cancellable) | |
|
132 | { | |
|
133 | m_state = PromiseState.Cancelled; | |
|
134 | EventHandler temp = Cancelled; | |
|
135 | ||
|
136 | if (temp != null) | |
|
137 | temp(this, new EventArgs()); | |
|
138 | ||
|
139 | return true; | |
|
140 | } else | |
|
141 | return false; | |
|
142 | } | |
|
144 | return Cancel(true); | |
|
143 | 145 | } |
|
144 | 146 | |
|
145 | 147 | /// <summary> |
|
146 | 148 | /// ΠΠΎΠ±Π°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
147 | 149 | /// </summary> |
|
148 | 150 | /// <param name="success">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΡΠΏΠ΅ΡΠ½ΠΎΠ³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
149 | 151 | /// ΠΠ°Π½Π½ΠΎΠΌΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
150 | 152 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
151 | 153 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
152 | 154 | /// <returns>Π‘Π°ΠΌΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅</returns> |
|
153 | 155 | public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) { |
|
154 | 156 | if (success == null && error == null) |
|
155 | 157 | return this; |
|
156 | 158 | |
|
157 | 159 | var medium = new Promise<T>(); |
|
158 | 160 | |
|
159 | 161 | var handlerInfo = new ResultHandlerInfo(); |
|
160 | 162 | |
|
161 | 163 | if (success != null) |
|
162 | 164 | handlerInfo.resultHandler = x => { |
|
163 | 165 | try { |
|
164 | 166 | success(x); |
|
165 | 167 | medium.Resolve(x); |
|
166 | 168 | } catch (Exception e) { |
|
167 | 169 | medium.Reject(e); |
|
168 | 170 | } |
|
169 | 171 | }; |
|
170 | 172 | else |
|
171 | 173 | handlerInfo.resultHandler = x => medium.Resolve(x); |
|
172 | 174 | |
|
173 | 175 | if (error != null) |
|
174 | 176 | handlerInfo.errorHandler = x => { |
|
175 | 177 | try { |
|
176 | 178 | error(x); |
|
177 | 179 | } catch { } |
|
178 | 180 | medium.Reject(x); |
|
179 | 181 | }; |
|
180 | 182 | else |
|
181 | 183 | handlerInfo.errorHandler = x => medium.Reject(x); |
|
182 | 184 | |
|
183 | 185 | AddHandler(handlerInfo); |
|
184 | 186 | |
|
185 | 187 | return medium; |
|
186 | 188 | } |
|
187 | 189 | |
|
188 | 190 | public Promise<T> Then(ResultHandler<T> success) { |
|
189 | 191 | return Then(success, null); |
|
190 | 192 | } |
|
191 | 193 | |
|
192 | 194 | public Promise<T> Error(ErrorHandler error) { |
|
193 | 195 | return Then(null, error); |
|
194 | 196 | } |
|
195 | 197 | |
|
196 | 198 | public Promise<T> Anyway(Action handler) { |
|
197 | 199 | if (handler == null) |
|
198 | 200 | return this; |
|
199 | 201 | |
|
200 | 202 | var medium = new Promise<T>(); |
|
201 | 203 | |
|
202 | 204 | AddHandler(new ResultHandlerInfo { |
|
203 | 205 | resultHandler = x => { |
|
204 | 206 | try { |
|
205 | 207 | handler(); |
|
206 | 208 | medium.Resolve(x); |
|
207 | 209 | } catch (Exception e) { |
|
208 | 210 | medium.Reject(e); |
|
209 | 211 | } |
|
210 | 212 | }, |
|
211 | 213 | errorHandler = x => { |
|
212 | 214 | try { |
|
213 | 215 | handler(); |
|
214 | 216 | } catch { } |
|
215 | 217 | medium.Reject(x); |
|
216 | 218 | } |
|
217 | 219 | }); |
|
218 | 220 | |
|
219 | 221 | return medium; |
|
220 | 222 | } |
|
221 | 223 | |
|
222 | 224 | /// <summary> |
|
223 | 225 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. |
|
224 | 226 | /// </summary> |
|
225 | 227 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> |
|
226 | 228 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> |
|
227 | 229 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
228 | 230 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
229 | 231 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> |
|
230 | 232 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { |
|
231 | 233 | if (mapper == null) |
|
232 | 234 | throw new ArgumentNullException("mapper"); |
|
233 | 235 | |
|
234 | 236 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
235 | 237 | Promise<TNew> chained = new Promise<TNew>(); |
|
236 | 238 | |
|
237 | 239 | AddHandler(new ResultHandlerInfo() { |
|
238 | 240 | resultHandler = delegate(T result) { |
|
239 | 241 | try { |
|
240 | 242 | // Π΅ΡΠ»ΠΈ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ Π²ΡΠ΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅, ΡΠΎ ΡΡΠ°Π±ΠΎΡΠ°Π΅Ρ reject ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ³ΠΎ deferred |
|
241 | 243 | chained.Resolve(mapper(result)); |
|
242 | 244 | } catch (Exception e) { |
|
243 | 245 | chained.Reject(e); |
|
244 | 246 | } |
|
245 | 247 | }, |
|
246 | 248 | errorHandler = delegate(Exception e) { |
|
247 | 249 | if (error != null) |
|
248 | 250 | error(e); |
|
249 | 251 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
250 | 252 | chained.Reject(e); |
|
251 | 253 | } |
|
252 | 254 | }); |
|
253 | 255 | |
|
254 | 256 | return chained; |
|
255 | 257 | } |
|
256 | 258 | |
|
257 | 259 | public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { |
|
258 | 260 | return Map(mapper, null); |
|
259 | 261 | } |
|
260 | 262 | |
|
261 | 263 | /// <summary> |
|
262 | 264 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ |
|
263 | 265 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ |
|
264 | 266 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
265 | 267 | /// </summary> |
|
266 | 268 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> |
|
267 | 269 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> |
|
268 | 270 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
269 | 271 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
270 | 272 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> |
|
271 | 273 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { |
|
272 | 274 | |
|
273 | 275 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ |
|
274 | 276 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. |
|
275 | 277 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ |
|
276 | 278 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. |
|
277 | 279 | Promise<TNew> medium = new Promise<TNew>(); |
|
278 | 280 | |
|
279 | 281 | AddHandler(new ResultHandlerInfo() { |
|
280 | 282 | resultHandler = delegate(T result) { |
|
281 | 283 | try { |
|
282 | 284 | chained(result).Then( |
|
283 | 285 | x => medium.Resolve(x), |
|
284 | 286 | e => medium.Reject(e) |
|
285 | 287 | ); |
|
286 | 288 | } catch (Exception e) { |
|
287 | 289 | // Π΅ΡΠ»ΠΈ ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π²ΡΠ΄Π°Π»ΠΎ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΌΠ΅ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΠΌ ΠΎΡΠΈΠ±ΠΊΡ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
288 | 290 | medium.Reject(e); |
|
289 | 291 | } |
|
290 | 292 | }, |
|
291 | 293 | errorHandler = delegate(Exception e) { |
|
292 | 294 | if (error != null) |
|
293 | 295 | error(e); |
|
294 | 296 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
295 | 297 | medium.Reject(e); |
|
296 | 298 | } |
|
297 | 299 | }); |
|
298 | 300 | |
|
299 | 301 | return medium; |
|
300 | 302 | } |
|
301 | 303 | |
|
302 | 304 | public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { |
|
303 | 305 | return Chain(chained, null); |
|
304 | 306 | } |
|
305 | 307 | |
|
306 | 308 | /// <summary> |
|
307 | 309 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ |
|
308 | 310 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. |
|
309 | 311 | /// </summary> |
|
310 | 312 | /// <remarks> |
|
311 | 313 | /// <para> |
|
312 | 314 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, |
|
313 | 315 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ |
|
314 | 316 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ |
|
315 | 317 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° |
|
316 | 318 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
317 | 319 | /// </para> |
|
318 | 320 | /// <para> |
|
319 | 321 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ |
|
320 | 322 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ |
|
321 | 323 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ |
|
322 | 324 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. |
|
323 | 325 | /// </para> |
|
324 | 326 | /// </remarks> |
|
325 | 327 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> |
|
326 | 328 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> |
|
327 | 329 | public T Join(int timeout) { |
|
328 | 330 | ManualResetEvent evt = new ManualResetEvent(false); |
|
329 | 331 | Anyway(() => evt.Set()); |
|
330 | 332 | |
|
331 | 333 | if (!evt.WaitOne(timeout, true)) |
|
332 | 334 | throw new TimeoutException(); |
|
333 | 335 | |
|
334 | 336 | if (m_error != null) |
|
335 | 337 | throw new TargetInvocationException(m_error); |
|
336 | 338 | else |
|
337 | 339 | return m_result; |
|
338 | 340 | } |
|
339 | 341 | |
|
340 | 342 | public T Join() { |
|
341 | 343 | return Join(Timeout.Infinite); |
|
342 | 344 | } |
|
343 | 345 | |
|
344 | 346 | /// <summary> |
|
345 | 347 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΈΠ·Π²Π»Π΅ΠΊΠ°Π΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΊΠΎΠ³Π΄Π° |
|
346 | 348 | /// ΠΈΡ Π±ΠΎΠ»ΡΡΠ΅ Π½Π΅ ΠΎΡΡΠ°Π»ΠΎΡΡ - ΡΡΠ°Π²ΠΈΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ "ΡΠ°Π·ΡΠ΅ΡΠ΅Π½ΠΎ". |
|
347 | 349 | /// </summary> |
|
348 | 350 | /// <param name="handler">ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎΠ± ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ΅</param> |
|
349 | 351 | /// <returns>ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π΅ΡΠ΅ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π² ΠΎΡΠ΅ΡΠ΅Π΄ΠΈ</returns> |
|
350 | 352 | bool FetchNextHandler(out ResultHandlerInfo handler) { |
|
351 | 353 | handler = default(ResultHandlerInfo); |
|
352 | 354 | |
|
353 | 355 | lock (this) { |
|
354 | 356 | Debug.Assert(m_state != PromiseState.Unresolved); |
|
355 | 357 | |
|
356 |
if (m_ |
|
|
357 |
handler = m_ |
|
|
358 |
m_ |
|
|
358 | if (m_resultHandlers.Count > 0) { | |
|
359 | handler = m_resultHandlers.First.Value; | |
|
360 | m_resultHandlers.RemoveFirst(); | |
|
359 | 361 | return true; |
|
360 | 362 | } else { |
|
361 | 363 | return false; |
|
362 | 364 | } |
|
363 | 365 | } |
|
364 | 366 | } |
|
365 | 367 | |
|
366 | 368 | void AddHandler(ResultHandlerInfo handler) { |
|
367 | 369 | bool invokeRequired = false; |
|
368 | 370 | |
|
369 | 371 | lock (this) { |
|
370 | 372 | if (m_state == PromiseState.Unresolved) |
|
371 |
m_ |
|
|
373 | m_resultHandlers.AddLast(handler); | |
|
372 | 374 | else |
|
373 | 375 | invokeRequired = true; |
|
374 | 376 | } |
|
375 | 377 | |
|
376 | 378 | // ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½Ρ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°ΡΡ ΡΠ°ΠΌ ΠΎΠ±ΡΠ΅ΠΊΡ |
|
377 | 379 | if (invokeRequired) |
|
378 | 380 | InvokeHandler(handler); |
|
379 | 381 | } |
|
380 | 382 | |
|
381 | 383 | void InvokeHandler(ResultHandlerInfo handler) { |
|
382 | 384 | if (m_error == null) { |
|
383 | 385 | try { |
|
384 | 386 | if (handler.resultHandler != null) |
|
385 | 387 | handler.resultHandler(m_result); |
|
386 | 388 | } catch { } |
|
387 | 389 | } |
|
388 | 390 | |
|
389 | 391 | if (m_error != null) { |
|
390 | 392 | try { |
|
391 | 393 | if (handler.errorHandler != null) |
|
392 | 394 | handler.errorHandler(m_error); |
|
393 | 395 | } catch { } |
|
394 | 396 | } |
|
395 | 397 | } |
|
396 | 398 | |
|
397 | 399 | |
|
400 | ||
|
401 | public bool IsExclusive { | |
|
402 | get { | |
|
403 | lock (m_lock) { | |
|
404 | return m_childrenCount <= 1; | |
|
405 | } | |
|
406 | } | |
|
407 | } | |
|
408 | ||
|
409 | public PromiseState State { | |
|
410 | get { | |
|
411 | lock (m_lock) { | |
|
412 | return m_state; | |
|
413 | } | |
|
398 | 414 | } |
|
399 | 415 | } |
|
416 | ||
|
417 | public bool Cancel(bool dependencies) { | |
|
418 | bool result; | |
|
419 | ||
|
420 | lock (m_lock) { | |
|
421 | if (m_state == PromiseState.Unresolved) { | |
|
422 | m_state = PromiseState.Cancelled; | |
|
423 | result = true; | |
|
424 | } else { | |
|
425 | result = false; | |
|
426 | } | |
|
427 | } | |
|
428 | ||
|
429 | if (dependencies && m_parent != null && m_parent.IsExclusive) { | |
|
430 | // TODO syncronize IsExclusive, AddHandler, Cancel (maybe CancelExclusive) | |
|
431 | m_parent.Cancel(true); | |
|
432 | } | |
|
433 | ||
|
434 | if (result) { | |
|
435 | // state has been changed to cancelled, new handlers can't be added | |
|
436 | foreach (var handler in m_cancelHandlers) | |
|
437 | handler(); | |
|
438 | } | |
|
439 | ||
|
440 | return result; | |
|
441 | } | |
|
442 | } | |
|
443 | } |
General Comments 0
You need to be logged in to leave comments.
Login now