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