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