@@ -1,87 +1,41 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Windows.Forms; |
|
3 | 3 | using System.Threading; |
|
4 | 4 | |
|
5 | 5 | namespace Implab.Fx |
|
6 | 6 | { |
|
7 | 7 | public static class PromiseHelpers |
|
8 | 8 | { |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// ΠΠ΅ΡΠ΅Π½Π°ΠΏΡΠ°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π² ΠΏΠΎΡΠΎΠΊ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ. |
|
11 | 11 | /// </summary> |
|
12 | 12 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</typeparam> |
|
13 | 13 | /// <param name="that">ΠΡΡ ΠΎΠ΄Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅</param> |
|
14 | 14 | /// <param name="ctl">ΠΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ</param> |
|
15 | 15 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ Π±ΡΠ΄ΡΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Ρ Π² ΠΏΠΎΡΠΎΠΊΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ.</returns> |
|
16 | 16 | /// <exception cref="ArgumentNullException">ΠΠ°ΡΠ°ΠΌΠ΅ΡΡ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ <c>null</c>.</exception> |
|
17 | 17 | /// <example> |
|
18 | 18 | /// client |
|
19 | 19 | /// .Get("description.txt") // returns a promise |
|
20 |
/// .Di |
|
|
20 | /// .DispatchToControl(m_ctl) // handle the promise in the thread of the control | |
|
21 | 21 | /// .Then( |
|
22 | 22 | /// description => m_ctl.Text = description // now it's safe |
|
23 | 23 | /// ) |
|
24 | 24 | /// </example> |
|
25 | public static Promise<T> DispatchToControl<T>(this Promise<T> that, Control ctl) | |
|
25 | public static IPromise<T> DispatchToControl<T>(this IPromise<T> that, Control ctl) | |
|
26 | 26 | { |
|
27 | if (that == null) | |
|
28 |
|
|
|
29 | if (ctl == null) | |
|
30 | throw new ArgumentNullException("ctl"); | |
|
27 | Safe.ArgumentNotNull(that, "that"); | |
|
28 | Safe.ArgumentNotNull(ctl, "ctl"); | |
|
31 | 29 | |
|
32 | 30 | var directed = new ControlBoundPromise<T>(ctl,that,true); |
|
33 | 31 | |
|
34 |
that. |
|
|
32 | that.Last( | |
|
35 | 33 | directed.Resolve, |
|
36 |
|
|
|
37 |
|
|
|
38 | directed.Reject(err); | |
|
39 | return default(T); | |
|
40 | } | |
|
34 | directed.Reject, | |
|
35 | directed.Cancel | |
|
41 | 36 | ); |
|
42 | 37 | |
|
43 | 38 | return directed; |
|
44 | 39 | } |
|
45 | ||
|
46 | /// <summary> | |
|
47 | /// ΠΠ°ΠΏΡΠ°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π² ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ, Π΅ΡΠ»ΠΈ Ρ Π½Π΅Π³ΠΎ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·Π°ΡΠΈΠΈ. | |
|
48 | /// </summary> | |
|
49 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</typeparam> | |
|
50 | /// <param name="that">ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΊΠΎΡΠΎΡΠΎΠ΅ Π½ΡΠΆΠ½ΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΏΠΎΡΠΎΠΊΠ΅.</param> | |
|
51 | /// <returns>ΠΠ΅ΡΠ΅Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅.</returns> | |
|
52 | public static Promise<T> DispatchToCurrentThread<T>(this Promise<T> that) | |
|
53 | { | |
|
54 | var sync = SynchronizationContext.Current; | |
|
55 | if (sync == null) | |
|
56 | throw new InvalidOperationException("The current thread doesn't have a syncronization context"); | |
|
57 | return DispatchToSyncContext(that, sync); | |
|
58 | } | |
|
59 | ||
|
60 | /// <summary> | |
|
61 | /// ΠΠ°ΠΏΡΠ°Π²Π»ΡΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π² ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·Π°ΡΠΈΠΈ. | |
|
62 | /// </summary> | |
|
63 | /// <typeparam name="T">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</typeparam> | |
|
64 | /// <param name="that">ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·Π°ΡΠΈΠΈ.</param> | |
|
65 | /// <param name="sync">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·Π°ΡΠΈΠΈ Π² ΠΊΠΎΡΠΎΡΡΠΉ Π±ΡΠ΄Π΅Ρ Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅.</param> | |
|
66 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°ΡΡΡΡ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅.</returns> | |
|
67 | public static Promise<T> DispatchToSyncContext<T>(this Promise<T> that, SynchronizationContext sync) | |
|
68 | { | |
|
69 | if (that == null) | |
|
70 | throw new ArgumentNullException("that"); | |
|
71 | if (sync == null) | |
|
72 | throw new ArgumentNullException("sync"); | |
|
73 | ||
|
74 | var d = new Promise<T>(); | |
|
75 | ||
|
76 | that.Then( | |
|
77 | res => sync.Post(state => d.Resolve(res), null), | |
|
78 | err => { | |
|
79 | sync.Post(state => d.Reject(err), null); | |
|
80 | return default(T); | |
|
81 | } | |
|
82 | ); | |
|
83 | ||
|
84 | return d; | |
|
85 | } | |
|
86 | 40 | } |
|
87 | 41 | } |
@@ -1,36 +1,35 | |||
|
1 | 1 | using System.Reflection; |
|
2 | 2 | using System.Runtime.CompilerServices; |
|
3 | 3 | using System.Runtime.InteropServices; |
|
4 | 4 | |
|
5 | 5 | // General Information about an assembly is controlled through the following |
|
6 | 6 | // set of attributes. Change these attribute values to modify the information |
|
7 | 7 | // associated with an assembly. |
|
8 | 8 | [assembly: AssemblyTitle("Implab.Fx")] |
|
9 | 9 | [assembly: AssemblyDescription("")] |
|
10 | 10 | [assembly: AssemblyConfiguration("")] |
|
11 | 11 | [assembly: AssemblyCompany("")] |
|
12 | 12 | [assembly: AssemblyProduct("Implab.Fx")] |
|
13 | 13 | [assembly: AssemblyCopyright("Copyright Β© 2013")] |
|
14 | 14 | [assembly: AssemblyTrademark("")] |
|
15 | 15 | [assembly: AssemblyCulture("")] |
|
16 | 16 | |
|
17 | 17 | // Setting ComVisible to false makes the types in this assembly not visible |
|
18 | 18 | // to COM components. If you need to access a type in this assembly from |
|
19 | 19 | // COM, set the ComVisible attribute to true on that type. |
|
20 | 20 | [assembly: ComVisible(false)] |
|
21 | 21 | |
|
22 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM |
|
23 | 23 | [assembly: Guid("d239c29f-98e2-4942-9569-554a8511d07b")] |
|
24 | 24 | |
|
25 | 25 | // Version information for an assembly consists of the following four values: |
|
26 | 26 | // |
|
27 | 27 | // Major Version |
|
28 | 28 | // Minor Version |
|
29 | 29 | // Build Number |
|
30 | 30 | // Revision |
|
31 | 31 | // |
|
32 | 32 | // You can specify all the values or you can default the Build and Revision Numbers |
|
33 | 33 | // by using the '*' as shown below: |
|
34 | 34 | // [assembly: AssemblyVersion("1.0.*")] |
|
35 |
[assembly: AssemblyVersion(" |
|
|
36 | [assembly: AssemblyFileVersion("1.0.0.0")] | |
|
35 | [assembly: AssemblyVersion("2.0.*")] |
@@ -1,392 +1,423 | |||
|
1 | 1 | using System; |
|
2 | 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
|
3 | 3 | using System.Reflection; |
|
4 | 4 | using System.Threading; |
|
5 | 5 | using Implab.Parallels; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Test { |
|
8 | 8 | [TestClass] |
|
9 | 9 | public class AsyncTests { |
|
10 | 10 | [TestMethod] |
|
11 | 11 | public void ResolveTest() { |
|
12 | 12 | int res = -1; |
|
13 | 13 | var p = new Promise<int>(); |
|
14 | 14 | p.Then(x => res = x); |
|
15 | 15 | p.Resolve(100); |
|
16 | 16 | |
|
17 | 17 | Assert.AreEqual(100, res); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | [TestMethod] |
|
21 | 21 | public void RejectTest() { |
|
22 | 22 | int res = -1; |
|
23 | 23 | Exception err = null; |
|
24 | 24 | |
|
25 | 25 | var p = new Promise<int>(); |
|
26 | 26 | p.Then( |
|
27 | 27 | x => res = x, |
|
28 | 28 | e => { |
|
29 | 29 | err = e; |
|
30 | 30 | return -2; |
|
31 | 31 | } |
|
32 | 32 | ); |
|
33 | 33 | p.Reject(new ApplicationException("error")); |
|
34 | 34 | |
|
35 | 35 | Assert.AreEqual(res, -1); |
|
36 | 36 | Assert.AreEqual(err.Message, "error"); |
|
37 | 37 | |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | [TestMethod] |
|
41 | public void CancelExceptionTest() { | |
|
42 | var p = new Promise<bool>(); | |
|
43 | p.Cancel(); | |
|
44 | ||
|
45 | var p2 = p.Cancelled(() => { | |
|
46 | throw new ApplicationException("CANCELLED"); | |
|
47 | }); | |
|
48 | ||
|
49 | try { | |
|
50 | p2.Join(); | |
|
51 | Assert.Fail(); | |
|
52 | } catch (ApplicationException err) { | |
|
53 | Assert.AreEqual("CANCELLED", err.InnerException.Message); | |
|
54 | } | |
|
55 | ||
|
56 | } | |
|
57 | ||
|
58 | [TestMethod] | |
|
59 | public void ContinueOnCancelTest() { | |
|
60 | var p = new Promise<bool>(); | |
|
61 | p.Cancel(); | |
|
62 | ||
|
63 | var p2 = p | |
|
64 | .Cancelled(() => { | |
|
65 | throw new ApplicationException("CANCELLED"); | |
|
66 | }) | |
|
67 | .Error(e => true); | |
|
68 | ||
|
69 | Assert.AreEqual(true, p2.Join()); | |
|
70 | } | |
|
71 | ||
|
72 | [TestMethod] | |
|
41 | 73 | public void JoinSuccessTest() { |
|
42 | 74 | var p = new Promise<int>(); |
|
43 | 75 | p.Resolve(100); |
|
44 | 76 | Assert.AreEqual(p.Join(), 100); |
|
45 | 77 | } |
|
46 | 78 | |
|
47 | 79 | [TestMethod] |
|
48 | 80 | public void JoinFailTest() { |
|
49 | 81 | var p = new Promise<int>(); |
|
50 | 82 | p.Reject(new ApplicationException("failed")); |
|
51 | 83 | |
|
52 | 84 | try { |
|
53 | 85 | p.Join(); |
|
54 | 86 | throw new ApplicationException("WRONG!"); |
|
55 | 87 | } catch (TargetInvocationException err) { |
|
56 | 88 | Assert.AreEqual(err.InnerException.Message, "failed"); |
|
57 | 89 | } catch { |
|
58 | 90 | Assert.Fail("Got wrong excaption"); |
|
59 | 91 | } |
|
60 | 92 | } |
|
61 | 93 | |
|
62 | 94 | [TestMethod] |
|
63 | 95 | public void MapTest() { |
|
64 | 96 | var p = new Promise<int>(); |
|
65 | 97 | |
|
66 |
var p2 = p. |
|
|
98 | var p2 = p.Then(x => x.ToString()); | |
|
67 | 99 | p.Resolve(100); |
|
68 | 100 | |
|
69 | 101 | Assert.AreEqual(p2.Join(), "100"); |
|
70 | 102 | } |
|
71 | 103 | |
|
72 | 104 | [TestMethod] |
|
73 | 105 | public void FixErrorTest() { |
|
74 | 106 | var p = new Promise<int>(); |
|
75 | 107 | |
|
76 | 108 | var p2 = p.Error(e => 101); |
|
77 | 109 | |
|
78 | 110 | p.Reject(new Exception()); |
|
79 | 111 | |
|
80 | 112 | Assert.AreEqual(p2.Join(), 101); |
|
81 | 113 | } |
|
82 | 114 | |
|
83 | 115 | [TestMethod] |
|
84 | 116 | public void ChainTest() { |
|
85 | 117 | var p1 = new Promise<int>(); |
|
86 | 118 | |
|
87 | 119 | var p3 = p1.Chain(x => { |
|
88 | 120 | var p2 = new Promise<string>(); |
|
89 | 121 | p2.Resolve(x.ToString()); |
|
90 | 122 | return p2; |
|
91 | 123 | }); |
|
92 | 124 | |
|
93 | 125 | p1.Resolve(100); |
|
94 | 126 | |
|
95 | 127 | Assert.AreEqual(p3.Join(), "100"); |
|
96 | 128 | } |
|
97 | 129 | |
|
98 | 130 | [TestMethod] |
|
99 | 131 | public void PoolTest() { |
|
100 | 132 | var pid = Thread.CurrentThread.ManagedThreadId; |
|
101 | 133 | var p = AsyncPool.Invoke(() => Thread.CurrentThread.ManagedThreadId); |
|
102 | 134 | |
|
103 | 135 | Assert.AreNotEqual(pid, p.Join()); |
|
104 | 136 | } |
|
105 | 137 | |
|
106 | 138 | [TestMethod] |
|
107 | 139 | public void WorkerPoolSizeTest() { |
|
108 | 140 | var pool = new WorkerPool(5, 10, 0); |
|
109 | 141 | |
|
110 | 142 | Assert.AreEqual(5, pool.PoolSize); |
|
111 | 143 | |
|
112 | 144 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
113 | 145 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
114 | 146 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
115 | 147 | |
|
116 | 148 | Assert.AreEqual(5, pool.PoolSize); |
|
117 | 149 | |
|
118 | 150 | for (int i = 0; i < 100; i++) |
|
119 | 151 | pool.Invoke(() => { Thread.Sleep(100000000); return 10; }); |
|
120 | 152 | Thread.Sleep(200); |
|
121 | 153 | Assert.AreEqual(10, pool.PoolSize); |
|
122 | 154 | |
|
123 | 155 | pool.Dispose(); |
|
124 | 156 | } |
|
125 | 157 | |
|
126 | 158 | [TestMethod] |
|
127 | 159 | public void WorkerPoolCorrectTest() { |
|
128 | 160 | var pool = new WorkerPool(0,1000,100); |
|
129 | 161 | |
|
130 | 162 | int iterations = 1000; |
|
131 | 163 | int pending = iterations; |
|
132 | 164 | var stop = new ManualResetEvent(false); |
|
133 | 165 | |
|
134 | 166 | var count = 0; |
|
135 | 167 | for (int i = 0; i < iterations; i++) { |
|
136 | 168 | pool |
|
137 | 169 | .Invoke(() => 1) |
|
138 | 170 | .Then(x => Interlocked.Add(ref count, x)) |
|
139 | 171 | .Then(x => Math.Log10(x)) |
|
140 | 172 | .Anyway(() => { |
|
141 | 173 | Interlocked.Decrement(ref pending); |
|
142 | 174 | if (pending == 0) |
|
143 | 175 | stop.Set(); |
|
144 | 176 | }); |
|
145 | 177 | } |
|
146 | 178 | |
|
147 | 179 | stop.WaitOne(); |
|
148 | 180 | |
|
149 | 181 | Assert.AreEqual(iterations, count); |
|
150 | 182 | Console.WriteLine("Max threads: {0}", pool.MaxRunningThreads); |
|
151 | 183 | pool.Dispose(); |
|
152 | 184 | |
|
153 | 185 | } |
|
154 | 186 | |
|
155 | 187 | [TestMethod] |
|
156 | 188 | public void WorkerPoolDisposeTest() { |
|
157 | 189 | var pool = new WorkerPool(5, 20); |
|
158 | 190 | Assert.AreEqual(5, pool.PoolSize); |
|
159 | 191 | pool.Dispose(); |
|
160 | 192 | Thread.Sleep(500); |
|
161 | 193 | Assert.AreEqual(0, pool.PoolSize); |
|
162 | 194 | pool.Dispose(); |
|
163 | 195 | } |
|
164 | 196 | |
|
165 | 197 | [TestMethod] |
|
166 | 198 | public void MTQueueTest() { |
|
167 | 199 | var queue = new MTQueue<int>(); |
|
168 | 200 | int res; |
|
169 | 201 | |
|
170 | 202 | queue.Enqueue(10); |
|
171 | 203 | Assert.IsTrue(queue.TryDequeue(out res)); |
|
172 | 204 | Assert.AreEqual(10, res); |
|
173 | 205 | Assert.IsFalse(queue.TryDequeue(out res)); |
|
174 | 206 | |
|
175 | 207 | for (int i = 0; i < 1000; i++) |
|
176 | 208 | queue.Enqueue(i); |
|
177 | 209 | |
|
178 | 210 | for (int i = 0; i < 1000; i++) { |
|
179 | 211 | queue.TryDequeue(out res); |
|
180 | 212 | Assert.AreEqual(i, res); |
|
181 | 213 | } |
|
182 | 214 | |
|
183 | 215 | int writers = 0; |
|
184 | 216 | int readers = 0; |
|
185 | 217 | var stop = new ManualResetEvent(false); |
|
186 | 218 | int total = 0; |
|
187 | 219 | |
|
188 | int itemsPerWriter = 1000; | |
|
189 |
int writersCount = |
|
|
220 | int itemsPerWriter = 10000; | |
|
221 | int writersCount = 10; | |
|
190 | 222 | |
|
191 | 223 | for (int i = 0; i < writersCount; i++) { |
|
192 | 224 | Interlocked.Increment(ref writers); |
|
193 | 225 | var wn = i; |
|
194 | 226 | AsyncPool |
|
195 | 227 | .InvokeNewThread(() => { |
|
196 | 228 | for (int ii = 0; ii < itemsPerWriter; ii++) { |
|
197 | 229 | queue.Enqueue(1); |
|
198 | 230 | } |
|
199 | 231 | return 1; |
|
200 | 232 | }) |
|
201 | 233 | .Anyway(() => Interlocked.Decrement(ref writers)); |
|
202 | 234 | } |
|
203 | 235 | |
|
204 | 236 | for (int i = 0; i < 10; i++) { |
|
205 | 237 | Interlocked.Increment(ref readers); |
|
206 | 238 | var wn = i; |
|
207 | 239 | AsyncPool |
|
208 | 240 | .InvokeNewThread(() => { |
|
209 | 241 | int t; |
|
210 | 242 | do { |
|
211 | 243 | while (queue.TryDequeue(out t)) |
|
212 | 244 | Interlocked.Add(ref total, t); |
|
213 | 245 | } while (writers > 0); |
|
214 | 246 | return 1; |
|
215 | 247 | }) |
|
216 | 248 | .Anyway(() => { |
|
217 | 249 | Interlocked.Decrement(ref readers); |
|
218 | 250 | if (readers == 0) |
|
219 | 251 | stop.Set(); |
|
220 | 252 | }); |
|
221 | 253 | } |
|
222 | 254 | |
|
223 | 255 | stop.WaitOne(); |
|
224 | 256 | |
|
225 | 257 | Assert.AreEqual(itemsPerWriter * writersCount, total); |
|
226 | 258 | } |
|
227 | 259 | |
|
228 | 260 | [TestMethod] |
|
229 | 261 | public void ParallelMapTest() { |
|
230 | 262 | |
|
231 | 263 | int count = 100000; |
|
232 | 264 | |
|
233 | 265 | double[] args = new double[count]; |
|
234 | 266 | var rand = new Random(); |
|
235 | 267 | |
|
236 | 268 | for (int i = 0; i < count; i++) |
|
237 | 269 | args[i] = rand.NextDouble(); |
|
238 | 270 | |
|
239 | 271 | var t = Environment.TickCount; |
|
240 | 272 | var res = args.ParallelMap(x => Math.Sin(x*x), 4).Join(); |
|
241 | 273 | |
|
242 | 274 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
243 | 275 | |
|
244 | 276 | t = Environment.TickCount; |
|
245 | 277 | for (int i = 0; i < count; i++) |
|
246 | 278 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
247 | 279 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
248 | 280 | } |
|
249 | 281 | |
|
250 | 282 | [TestMethod] |
|
251 | 283 | public void ChainedMapTest() { |
|
252 | 284 | |
|
253 | 285 | using (var pool = new WorkerPool(0,100,100)) { |
|
254 | 286 | int count = 10000; |
|
255 | 287 | |
|
256 | 288 | double[] args = new double[count]; |
|
257 | 289 | var rand = new Random(); |
|
258 | 290 | |
|
259 | 291 | for (int i = 0; i < count; i++) |
|
260 | 292 | args[i] = rand.NextDouble(); |
|
261 | 293 | |
|
262 | 294 | var t = Environment.TickCount; |
|
263 | 295 | var res = args |
|
264 | 296 | .ChainedMap( |
|
265 | 297 | x => pool.Invoke( |
|
266 | 298 | () => Math.Sin(x * x) |
|
267 | 299 | ), |
|
268 | 300 | 4 |
|
269 | 301 | ) |
|
270 | 302 | .Join(); |
|
271 | 303 | |
|
272 | 304 | Console.WriteLine("Map complete in {0} ms", Environment.TickCount - t); |
|
273 | 305 | |
|
274 | 306 | t = Environment.TickCount; |
|
275 | 307 | for (int i = 0; i < count; i++) |
|
276 | 308 | Assert.AreEqual(Math.Sin(args[i] * args[i]), res[i]); |
|
277 | 309 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
278 | 310 | Console.WriteLine("Max workers: {0}", pool.MaxRunningThreads); |
|
279 | 311 | } |
|
280 | 312 | } |
|
281 | 313 | |
|
282 | 314 | [TestMethod] |
|
283 | 315 | public void ParallelForEachTest() { |
|
284 | 316 | |
|
285 | 317 | int count = 100000; |
|
286 | 318 | |
|
287 | 319 | int[] args = new int[count]; |
|
288 | 320 | var rand = new Random(); |
|
289 | 321 | |
|
290 | 322 | for (int i = 0; i < count; i++) |
|
291 | 323 | args[i] = (int)(rand.NextDouble() * 100); |
|
292 | 324 | |
|
293 | 325 | int result = 0; |
|
294 | 326 | |
|
295 | 327 | var t = Environment.TickCount; |
|
296 | 328 | args.ParallelForEach(x => Interlocked.Add(ref result, x), 4).Join(); |
|
297 | 329 | |
|
298 | 330 | Console.WriteLine("Iteration complete in {0} ms, result: {1}", Environment.TickCount - t, result); |
|
299 | 331 | |
|
300 | 332 | int result2 = 0; |
|
301 | 333 | |
|
302 | 334 | t = Environment.TickCount; |
|
303 | 335 | for (int i = 0; i < count; i++) |
|
304 | 336 | result2 += args[i]; |
|
305 | 337 | Assert.AreEqual(result2, result); |
|
306 | 338 | Console.WriteLine("Verified in {0} ms", Environment.TickCount - t); |
|
307 | 339 | } |
|
308 | 340 | |
|
309 | 341 | [TestMethod] |
|
310 | 342 | public void ComplexCase1Test() { |
|
311 | 343 | var flags = new bool[3]; |
|
312 | 344 | |
|
313 | 345 | // op1 (aync 200ms) => op2 (async 200ms) => op3 (sync map) |
|
314 | 346 | |
|
315 | 347 | var p = PromiseHelper |
|
316 | 348 | .Sleep(200, "Alan") |
|
317 | 349 | .Cancelled(() => flags[0] = true) |
|
318 | 350 | .Chain(x => |
|
319 | 351 | PromiseHelper |
|
320 | 352 | .Sleep(200, "Hi, " + x) |
|
321 |
. |
|
|
353 | .Then(y => y) | |
|
322 | 354 | .Cancelled(() => flags[1] = true) |
|
323 | 355 | ) |
|
324 | 356 | .Cancelled(() => flags[2] = true); |
|
325 | 357 | Thread.Sleep(300); |
|
326 | 358 | p.Cancel(); |
|
327 | 359 | try { |
|
328 | 360 | Assert.AreEqual(p.Join(), "Hi, Alan"); |
|
329 | 361 | Assert.Fail("Shouldn't get here"); |
|
330 | 362 | } catch (OperationCanceledException) { |
|
331 | 363 | } |
|
332 | 364 | |
|
333 | 365 | Assert.IsFalse(flags[0]); |
|
334 | 366 | Assert.IsTrue(flags[1]); |
|
335 | 367 | Assert.IsTrue(flags[2]); |
|
336 | 368 | } |
|
337 | 369 | |
|
338 | 370 | [TestMethod] |
|
339 | 371 | public void ChainedCancel1Test() { |
|
340 | 372 | // ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ ΡΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π²ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ |
|
341 | 373 | // Π·Π°Π²Π΅ΡΡΠ°ΡΡΡΡ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ OperationCanceledException |
|
342 | 374 | var p = PromiseHelper |
|
343 | 375 | .Sleep(1, "Hi, HAL!") |
|
344 |
. |
|
|
376 | .Then(x => { | |
|
345 | 377 | // Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ Π΄Π²Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
346 | 378 | var result = PromiseHelper.Sleep(1000, "HEM ENABLED!!!"); |
|
347 | 379 | // Π²ΡΠΎΡΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΏΠ΅ΡΠ²ΡΡ Π΄ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ |
|
348 | 380 | PromiseHelper |
|
349 | 381 | .Sleep(100, "HAL, STOP!") |
|
350 | 382 | .Then(() => result.Cancel()); |
|
351 | 383 | return result; |
|
352 | 384 | }); |
|
353 | 385 | try { |
|
354 | 386 | p.Join(); |
|
355 | 387 | } catch (TargetInvocationException err) { |
|
356 | 388 | Assert.IsTrue(err.InnerException is OperationCanceledException); |
|
357 | 389 | } |
|
358 | 390 | } |
|
359 | 391 | |
|
360 | 392 | [TestMethod] |
|
361 | 393 | public void ChainedCancel2Test() { |
|
362 | 394 | // ΠΏΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, Π²Π»ΠΎΠΆΠ΅Π½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΠΊΠΆΠ΅ Π΄ΠΎΠ»ΠΆΠ½Ρ ΠΎΡΠΌΠ΅Π½ΡΡΡΡΡ |
|
363 | IPromise p = null; | |
|
364 | 395 | var pSurvive = new Promise<bool>(); |
|
365 | 396 | var hemStarted = new ManualResetEvent(false); |
|
366 | p = PromiseHelper | |
|
397 | var p = PromiseHelper | |
|
367 | 398 | .Sleep(1, "Hi, HAL!") |
|
368 | 399 | .Chain(x => { |
|
369 | 400 | hemStarted.Set(); |
|
370 | 401 | // Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌ Π΄Π²Π΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
371 | 402 | var result = PromiseHelper |
|
372 | .Sleep(1000, "HEM ENABLED!!!") | |
|
403 | .Sleep(10000, "HEM ENABLED!!!") | |
|
373 | 404 | .Then(s => pSurvive.Resolve(false)); |
|
374 | 405 | |
|
375 | 406 | result |
|
376 | 407 | .Cancelled(() => pSurvive.Resolve(true)); |
|
377 | 408 | |
|
378 | 409 | return result; |
|
379 | 410 | }); |
|
380 | 411 | |
|
381 | 412 | hemStarted.WaitOne(); |
|
382 | 413 | p.Cancel(); |
|
383 | 414 | |
|
384 | 415 | try { |
|
385 | 416 | p.Join(); |
|
386 | 417 | } catch (OperationCanceledException) { |
|
387 | 418 | Assert.IsTrue(pSurvive.Join()); |
|
388 | 419 | } |
|
389 | 420 | } |
|
390 | 421 | } |
|
391 | 422 | } |
|
392 | 423 |
@@ -1,238 +1,238 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | using System.Threading.Tasks; |
|
7 | 7 | |
|
8 | 8 | namespace Implab.Diagnostics { |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΊ ΠΏΠΎΡΠΎΠΊΡ ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. |
|
11 | 11 | /// </summary> |
|
12 | 12 | /// <remarks> |
|
13 | 13 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΡΠΌ ΡΠΎΠ±ΡΡΠΈΠΉ Π΄Π»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠ΅ΡΡΠ°, Π³Π΄Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅. |
|
14 | 14 | /// </remarks> |
|
15 | 15 | public class TraceContext { |
|
16 | 16 | LogicalOperation m_currentOperation; |
|
17 | 17 | readonly LogicalOperation m_bound; |
|
18 | 18 | readonly int m_threadId; |
|
19 | 19 | |
|
20 | 20 | [ThreadStatic] |
|
21 | 21 | static TraceContext _current; |
|
22 | 22 | |
|
23 | 23 | /// <summary> |
|
24 | 24 | /// Π’Π΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ Π΄Π»Ρ ΠΏΠΎΡΠΎΠΊΠ°, ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ Π°ΡΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ. |
|
25 | 25 | /// </summary> |
|
26 | 26 | public static TraceContext Current { |
|
27 | 27 | get { |
|
28 | 28 | if (_current == null) { |
|
29 | 29 | _current = new TraceContext(); |
|
30 | 30 | _current.LogEvent(TraceEventType.Created,"[{0}]", _current.ThreadId); |
|
31 | 31 | } |
|
32 | 32 | return _current; |
|
33 | 33 | } |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | TraceContext(TraceContext context) |
|
37 | 37 | : this(context, false) { |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | TraceContext(TraceContext context, bool attach) { |
|
41 | 41 | if (context == null) |
|
42 | 42 | throw new ArgumentNullException("context"); |
|
43 | 43 | |
|
44 | 44 | m_currentOperation = context.CurrentOperation; |
|
45 | 45 | m_bound = attach ? context.BoundOperation : context.CurrentOperation; |
|
46 | 46 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | TraceContext() { |
|
50 | 50 | m_currentOperation = new LogicalOperation(); |
|
51 | 51 | m_bound = m_currentOperation; |
|
52 | 52 | m_threadId = Thread.CurrentThread.ManagedThreadId; |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | /// <summary> |
|
56 | 56 | /// ΠΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ ΠΊΠΎΠΏΠΈΡΡΠ΅Ρ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΠ²ΡΠΎΠ²ΠΊΠΈ Π² ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ. |
|
57 | 57 | /// </summary> |
|
58 | 58 | /// <param name="from">ΠΡΡ ΠΎΠ΄Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ.</param> |
|
59 | 59 | /// <remarks> |
|
60 | 60 | /// <para> |
|
61 | 61 | /// ΠΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ Π·Π° ΡΡΠ΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΈ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ Π΅Π³ΠΎ |
|
62 | 62 | /// ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°. ΠΡΠΈ ΡΡΠΎΠΌ ΠΊΠΎΠΏΠΈΡΡΠ΅ΡΡΡ ΡΡΠ΅ΠΊ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π½ΠΎΠ²ΠΎΠΌ |
|
63 | 63 | /// ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Ρ. |
|
64 | 64 | /// </para> |
|
65 | 65 | /// <para> |
|
66 | 66 | /// ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΡΠΎΡΡΠΎΡΠ»Π°ΡΡ, ΡΠΎ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ <see cref="TraceEventType.Fork"/>. |
|
67 | 67 | /// </para> |
|
68 | 68 | /// </remarks> |
|
69 | 69 | public static void Fork(TraceContext from) { |
|
70 | 70 | if (_current == from) |
|
71 | 71 | return; |
|
72 | 72 | if (from != null) { |
|
73 | 73 | var context = new TraceContext(from); |
|
74 | 74 | context.LogEvent(TraceEventType.Fork, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); |
|
75 | 75 | _current = context; |
|
76 | 76 | } else { |
|
77 | 77 | _current = new TraceContext(); |
|
78 | 78 | } |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | /// <summary> |
|
82 | 82 | /// ΠΠ°Π΄Π°Π΅Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΌΡ ΠΏΠΎΡΠΎΠΊΡ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ, ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΏΠΎΡΠΎΠΊ ΠΌΠΎΠΆΠ΅Ρ Π·Π°ΠΊΠ°Π½ΡΠΈΠ²Π°ΡΡ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ |
|
83 | 83 | /// Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
84 | 84 | /// </summary> |
|
85 | 85 | /// <param name="source"></param> |
|
86 | 86 | public static void Attach(TraceContext source) { |
|
87 | 87 | if (_current == source) |
|
88 | 88 | return; |
|
89 | 89 | if (source != null) { |
|
90 | 90 | var context = new TraceContext(source, true); |
|
91 | 91 | context.LogEvent(TraceEventType.Attach, "[{0}]-->[{1}]", source.ThreadId, context.ThreadId); |
|
92 | 92 | _current = context; |
|
93 | 93 | } else { |
|
94 | 94 | _current = new TraceContext(); |
|
95 | 95 | } |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | /// <summary> |
|
99 | 99 | /// ΠΡΡΠΎΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΏΠΎΡΠΎΠΊΠ°, Π΄Π»Ρ Π΄Π°Π»ΡΠ½Π΅ΠΉΡΠ΅ΠΉ Π΅Π³ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ Π΄ΡΡΠ³ΠΎΠΌΡ ΠΏΠΎΡΠΎΠΊΡ |
|
100 | 100 | /// <see cref="Attach(TraceContext)"/>. |
|
101 | 101 | /// </summary> |
|
102 | 102 | /// <returns>ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°</returns> |
|
103 | 103 | /// <remarks> |
|
104 | 104 | /// ΠΠΎΡΠ»Π΅ ΠΎΡΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΏΠΎΡΠΎΠΊΠ°, ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠ΅ Π² ΡΡΠΎΠΌ |
|
105 | 105 | /// ΠΏΠΎΡΠΎΠΊΠ΅ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ Π½ΠΎΠ²ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ. |
|
106 | 106 | /// </remarks> |
|
107 | 107 | public static TraceContext Detach() { |
|
108 | 108 | var context = Current; |
|
109 | 109 | context.LogEvent(TraceEventType.Detach, null); |
|
110 | 110 | _current = null; |
|
111 | 111 | return context; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | /// <summary> |
|
115 | 115 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΏΠΎΡΡΠΎΡΠ½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π΄Π°Π½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ Ρ ΡΠ°Π½ΠΈΡΡ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ ΡΠ΅ΡΠ΅Π· <see cref="Fork(TraceContext)"/> |
|
116 | 116 | /// </summary> |
|
117 | 117 | /// <returns>ΠΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</returns> |
|
118 | 118 | public static TraceContext Snapshot() { |
|
119 | 119 | return _current == null ? new TraceContext() : new TraceContext(_current,false); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | /// <summary> |
|
123 | 123 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΠΈ Π²ΠΎΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°. |
|
124 | 124 | /// </summary> |
|
125 | 125 | /// <param name="action"></param> |
|
126 | 126 | public void Invoke(Action action) { |
|
127 | 127 | if (action == null) |
|
128 | 128 | throw new ArgumentNullException("action"); |
|
129 | 129 | var old = _current; |
|
130 | 130 | Fork(this); |
|
131 | 131 | try { |
|
132 | 132 | action(); |
|
133 | 133 | } finally { |
|
134 | 134 | if(_current != null) |
|
135 | 135 | _current.EndAllOperations(); |
|
136 | 136 | _current = old; |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | /// <summary> |
|
141 | 141 | /// Π’Π΅ΠΊΡΡΠ°Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
142 | 142 | /// </summary> |
|
143 | 143 | public LogicalOperation CurrentOperation { |
|
144 | 144 | get { |
|
145 | 145 | return m_currentOperation; |
|
146 | 146 | } |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | /// <summary> |
|
150 | 150 | /// ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π½ΠΈΠΆΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π΅Π»ΡΠ·Ρ ΠΎΠΏΡΡΠΊΠ°ΡΡΡΡ Π² ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, Ρ.Π΅. ΠΎΠ½Π° Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Π° Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
151 | 151 | /// </summary> |
|
152 | 152 | public LogicalOperation BoundOperation { |
|
153 | 153 | get { |
|
154 | 154 | return m_bound; |
|
155 | 155 | } |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | /// <summary> |
|
159 | 159 | /// ΠΠΎΡΠΎΠΊ, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ. |
|
160 | 160 | /// </summary> |
|
161 | 161 | public int ThreadId { |
|
162 | 162 | get { |
|
163 | 163 | return m_threadId; |
|
164 | 164 | } |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | /// <summary> |
|
168 | 168 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π±Π΅Π·ΡΠΌΡΠ½Π½ΡΡ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. |
|
169 | 169 | /// </summary> |
|
170 | 170 | public void StartLogicalOperation() { |
|
171 | 171 | StartLogicalOperation(null); |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | /// <summary> |
|
175 | 175 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ. Π‘ΠΎΠ·Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π΄ΠΎΠ±Π²Π°Π»Π΅Π½Π° Π² ΡΡΠ΅ΠΊ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π·Π°ΡΠ΅ΠΌ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΎ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠ΅Π΅ ΡΠΎΠ±ΡΡΠΈΠ΅. |
|
176 | 176 | /// </summary> |
|
177 | 177 | /// <param name="name">ΠΠΌΡ Π½Π°ΡΠΈΠ½Π°Π΅ΠΌΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
178 | 178 | public void StartLogicalOperation(string name) { |
|
179 | 179 | m_currentOperation = new LogicalOperation(name, m_currentOperation); |
|
180 | 180 | LogEvent(TraceEventType.OperationStarted, name); |
|
181 | 181 | } |
|
182 | 182 | |
|
183 | 183 | /// <summary> |
|
184 | 184 | /// ΠΠ°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π½Π°ΡΠ°ΡΡΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π°ΡΠ°ΡΡΠ΅ Π² Π΄ΡΡΠ³ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°Ρ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Ρ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
185 | 185 | /// </summary> |
|
186 | 186 | /// <remarks> |
|
187 | 187 | /// ΠΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΆΡΡΠ½Π°Π»Π° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, Π»ΠΈΠ±ΠΎ ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π»ΠΈΠ±ΠΎ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ |
|
188 | 188 | /// Π½Π°ΡΠ°ΡΠ° Π² Π΄ΡΡΠ³ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. |
|
189 | 189 | /// </remarks> |
|
190 | 190 | public void EndLogicalOperation() { |
|
191 | 191 | if (m_bound == m_currentOperation) { |
|
192 | 192 | LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); |
|
193 | 193 | } else { |
|
194 | 194 | var op = m_currentOperation; |
|
195 | 195 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); |
|
196 | 196 | m_currentOperation = m_currentOperation.Parent; |
|
197 | 197 | } |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | /// <summary> |
|
201 | 201 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΊΠΎΠΏΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ Π½Π° ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅, ΡΡΠΎ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ Π½Π°ΡΠ°ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅, Π° Π·Π°Π²Π΅ΡΡΠΈΡΡ - Π² Π΄ΡΡΠ³ΠΎΠΌ. |
|
202 | 202 | /// </summary> |
|
203 | 203 | /// <returns>ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΠΈΡΡ ΠΊ Π΄ΡΡΠ³ΠΎΠΌΡ ΠΏΠΎΡΠΎΠΊΡ.</returns> |
|
204 | 204 | public TraceContext DetachLogicalOperation() { |
|
205 | 205 | if (m_bound == m_currentOperation) { |
|
206 | 206 | return new TraceContext(); |
|
207 | 207 | } else { |
|
208 | 208 | var detached = new TraceContext(this, true); |
|
209 | 209 | m_currentOperation = m_currentOperation.Parent; |
|
210 | 210 | return detached; |
|
211 | 211 | } |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | public void BindLogicalOperationToPromise(IPromise promise) { |
|
215 | 215 | Safe.ArgumentNotNull(promise, "promise"); |
|
216 | 216 | |
|
217 | 217 | var ctx = DetachLogicalOperation(); |
|
218 |
promise. |
|
|
218 | promise.Anyway(() => { | |
|
219 | 219 | var old = _current; |
|
220 | 220 | TraceContext.Attach(ctx); |
|
221 | 221 | TraceContext.Current.EndLogicalOperation(); |
|
222 | 222 | _current = old; |
|
223 | 223 | }); |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | /// <summary> |
|
227 | 227 | /// ΠΠ°Π²ΡΠ΅ΡΠ°Π΅Ρ Π²ΡΠ΅ Π½Π°ΡΠ°ΡΡΠ΅ Π² ΡΡΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ |
|
228 | 228 | /// </summary> |
|
229 | 229 | public void EndAllOperations() { |
|
230 | 230 | while (m_bound != m_currentOperation) |
|
231 | 231 | EndLogicalOperation(); |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | void LogEvent(TraceEventType type, string format, params object[] args) { |
|
235 | 235 | LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); |
|
236 | 236 | } |
|
237 | 237 | } |
|
238 | 238 | } |
@@ -1,10 +1,10 | |||
|
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 ICancellable { |
|
8 |
|
|
|
8 | void Cancel(); | |
|
9 | 9 | } |
|
10 | 10 | } |
@@ -1,82 +1,82 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | |
|
6 | 6 | namespace Implab { |
|
7 | 7 | public interface IPromise: ICancellable { |
|
8 | 8 | /// <summary> |
|
9 | 9 | /// Check whereather the promise has no more than one dependent promise. |
|
10 | 10 | /// </summary> |
|
11 | 11 | bool IsExclusive { |
|
12 | 12 | get; |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | /// <summary> |
|
16 | 16 | /// Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅ΡΠ΅Π· Π΄Π°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅. |
|
17 | 17 | /// </summary> |
|
18 | 18 | Type PromiseType { get; } |
|
19 | 19 | |
|
20 | 20 | /// <summary> |
|
21 | 21 | /// ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠ²Π»ΡΠ΅ΡΡΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΡΠΌ, Π»ΠΈΠ±ΠΎ ΡΡΠΏΠ΅ΡΠ½ΠΎ, Π»ΠΈΠ±ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. |
|
22 | 22 | /// </summary> |
|
23 | 23 | bool IsResolved { get; } |
|
24 | 24 | |
|
25 | 25 | /// <summary> |
|
26 | 26 | /// ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ. |
|
27 | 27 | /// </summary> |
|
28 | 28 | bool IsCancelled { get; } |
|
29 | 29 | |
|
30 | 30 | IPromise Then(Action success, ErrorHandler error, Action cancel); |
|
31 | 31 | IPromise Then(Action success, ErrorHandler error); |
|
32 | 32 | IPromise Then(Action success); |
|
33 | 33 | |
|
34 | 34 | /// <summary> |
|
35 | 35 | /// ΠΠΎΠ±Π°Π²Π»ΡΠ΅Ρ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π² ΡΠ΅ΠΏΠΎΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, Π½Π΅ ΡΠΎΠ·Π΄Π°Π΅Ρ ΠΏΡΠΎΠΌΠ΅ΠΆΡΡΠΎΡΠ½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
36 | 36 | /// </summary> |
|
37 | 37 | /// <param name="success">Success.</param> |
|
38 | 38 | /// <param name="error">Error.</param> |
|
39 | 39 | /// <param name="cancel">Cancel.</param> |
|
40 | 40 | void Last(Action success, ErrorHandler error, Action cancel); |
|
41 | 41 | void Last(Action success, ErrorHandler error); |
|
42 | 42 | void Last(Action success); |
|
43 | 43 | |
|
44 | 44 | IPromise Error(ErrorHandler error); |
|
45 | 45 | /// <summary> |
|
46 | 46 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ Π»ΠΈΠ±ΠΎ ΠΎΡΠΈΠ±ΠΊΡ, Π»ΠΈΠ±ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ. Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΎΡΠΌΠ΅Π½Ρ Π½Π΅ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅ΡΡΡ. |
|
47 | 47 | /// </summary> |
|
48 | 48 | /// <param name="handler">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ.</param> |
|
49 | 49 | /// <remarks>ΠΠΎΡΠ»Π΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΎΠ½Π° ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ Π΄Π°Π»ΡΡΠ΅.</remarks> |
|
50 | 50 | /// <summary> |
|
51 | 51 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ Π»ΠΈΠ±ΠΎ ΠΎΡΠΈΠ±ΠΊΡ, Π»ΠΈΠ±ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π»ΠΈΠ±ΠΎ ΠΎΡΠΌΠ΅Π½Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
52 | 52 | /// </summary> |
|
53 | 53 | /// <param name="handler">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ.</param> |
|
54 | 54 | /// <remarks>ΠΠΎΡΠ»Π΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ΅ ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΎΠ½Π° ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ Π΄Π°Π»ΡΡΠ΅.</remarks> |
|
55 |
IPromise |
|
|
55 | IPromise Anyway(Action handler); | |
|
56 | 56 | /// <summary> |
|
57 | 57 | /// ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π΄Π»Ρ ΡΠ΅Π³ΠΈΡΡΡΠ°ΡΠΈΠΈ ΠΎΡΠΌΠ΅Π½Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΎΡΠΌΠ΅Π½Ρ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΏΠΎΠ΄Π°Π²Π»Π΅Π½ΠΎ. |
|
58 | 58 | /// </summary> |
|
59 | 59 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠ΅ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ.</returns> |
|
60 | 60 | /// <param name="handler">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠΎΠ±ΡΡΠΈΡ.</param> |
|
61 | 61 | /// <remarks>ΠΡΠ»ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅, ΡΠΎ ΠΎΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΡ ΠΎΡΠΈΠ±ΠΊΠΈ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ°Π±ΠΎΡΡ |
|
62 | 62 | /// ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΌΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</remarks> |
|
63 | 63 | IPromise Cancelled(Action handler); |
|
64 | 64 | |
|
65 | 65 | /// <summary> |
|
66 | 66 | /// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΊ Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡ ΡΠΈΠΏΡ ΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅. |
|
67 | 67 | /// </summary> |
|
68 | 68 | IPromise<T> Cast<T>(); |
|
69 | 69 | |
|
70 | 70 | /// <summary> |
|
71 | 71 | /// Π‘ΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·ΠΈΡΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ΠΌ. |
|
72 | 72 | /// </summary> |
|
73 | 73 | void Join(); |
|
74 | 74 | /// <summary> |
|
75 | 75 | /// Π‘ΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·ΠΈΡΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ΠΌ. |
|
76 | 76 | /// </summary> |
|
77 | 77 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ, ΠΏΠΎ Π΅Π³ΠΎ ΠΈΡΡΠ΅ΡΠ΅Π½ΠΈΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅.</param> |
|
78 | 78 | /// <exception cref="TimeoutException">ΠΡΠ΅Π²ΡΡΠ΅Π½ΠΎ Π²ΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ.</exception> |
|
79 | 79 | void Join(int timeout); |
|
80 | 80 | |
|
81 | 81 | } |
|
82 | 82 | } |
@@ -1,37 +1,43 | |||
|
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 | public interface IPromise<T>: IPromise { | |
|
7 | public interface IPromise<T> : IPromise { | |
|
8 | 8 | |
|
9 | 9 | new T Join(); |
|
10 | 10 | |
|
11 | 11 | new T Join(int timeout); |
|
12 | 12 | |
|
13 | void Last(ResultHandler<T> success, ErrorHandler error, Action cancel); | |
|
14 | ||
|
15 | void Last(ResultHandler<T> success, ErrorHandler error); | |
|
16 | ||
|
17 | void Last(ResultHandler<T> success); | |
|
18 | ||
|
13 | 19 | IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel); |
|
14 | 20 | |
|
15 | 21 | IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error); |
|
16 | 22 | |
|
17 | 23 | IPromise<T> Then(ResultHandler<T> success); |
|
18 | 24 | |
|
19 |
|
|
|
20 | void Last(ResultHandler<T> success, ErrorHandler error); | |
|
21 | void Last(ResultHandler<T> success); | |
|
25 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper, ErrorHandler<T2> error, Action cancel); | |
|
26 | ||
|
27 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper, ErrorHandler<T2> error); | |
|
28 | ||
|
29 | IPromise<T2> Then<T2>(ResultMapper<T, T2> mapper); | |
|
30 | ||
|
31 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained, ErrorHandler<IPromise<T2>> error, Action cancel); | |
|
32 | ||
|
33 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained, ErrorHandler<IPromise<T2>> error); | |
|
34 | ||
|
35 | IPromise<T2> Chain<T2>(ResultMapper<T, IPromise<T2>> chained); | |
|
22 | 36 | |
|
23 | 37 | IPromise<T> Error(ErrorHandler<T> error); |
|
24 | 38 | |
|
25 | IPromise<T2> Then<T2>(ResultMapper<T,T2> mapper, ErrorHandler<T> error); | |
|
26 | ||
|
27 | IPromise<T2> Then<T2>(ResultMapper<T,T2> mapper); | |
|
28 | ||
|
29 | IPromise<T2> Then<T2>(ChainedOperation<T, T2> chained, ErrorHandler<T> error); | |
|
30 | ||
|
31 | IPromise<T2> Then<T2>(ChainedOperation<T, T2> chained); | |
|
32 | ||
|
33 | 39 | new IPromise<T> Cancelled(Action handler); |
|
34 | 40 | |
|
35 |
new IPromise<T> |
|
|
41 | new IPromise<T> Anyway(Action handler); | |
|
36 | 42 | } |
|
37 | 43 | } |
@@ -1,193 +1,193 | |||
|
1 | 1 | using Implab.Diagnostics; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Collections.Generic; |
|
4 | 4 | using System.Diagnostics; |
|
5 | 5 | using System.Linq; |
|
6 | 6 | using System.Text; |
|
7 | 7 | using System.Threading; |
|
8 | 8 | |
|
9 | 9 | namespace Implab.Parallels { |
|
10 | 10 | public static class ArrayTraits { |
|
11 | 11 | class ArrayIterator<TSrc> : DispatchPool<int> { |
|
12 | 12 | readonly Action<TSrc> m_action; |
|
13 | 13 | readonly TSrc[] m_source; |
|
14 | 14 | readonly Promise<int> m_promise = new Promise<int>(); |
|
15 | 15 | readonly TraceContext m_traceContext; |
|
16 | 16 | |
|
17 | 17 | int m_pending; |
|
18 | 18 | int m_next; |
|
19 | 19 | |
|
20 | 20 | public ArrayIterator(TSrc[] source, Action<TSrc> action, int threads) |
|
21 | 21 | : base(threads) { |
|
22 | 22 | |
|
23 | 23 | Debug.Assert(source != null); |
|
24 | 24 | Debug.Assert(action != null); |
|
25 | 25 | |
|
26 | 26 | m_traceContext = TraceContext.Snapshot(); |
|
27 | 27 | m_next = 0; |
|
28 | 28 | m_source = source; |
|
29 | 29 | m_pending = source.Length; |
|
30 | 30 | m_action = action; |
|
31 | 31 | |
|
32 |
m_promise. |
|
|
32 | m_promise.Anyway(Dispose); | |
|
33 | 33 | |
|
34 | 34 | InitPool(); |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | public Promise<int> Promise { |
|
38 | 38 | get { |
|
39 | 39 | return m_promise; |
|
40 | 40 | } |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | protected override void Worker() { |
|
44 | 44 | TraceContext.Fork(m_traceContext); |
|
45 | 45 | base.Worker(); |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | protected override bool TryDequeue(out int unit) { |
|
49 | 49 | unit = Interlocked.Increment(ref m_next) - 1; |
|
50 | 50 | return unit < m_source.Length; |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | protected override void InvokeUnit(int unit) { |
|
54 | 54 | try { |
|
55 | 55 | m_action(m_source[unit]); |
|
56 | 56 | var pending = Interlocked.Decrement(ref m_pending); |
|
57 | 57 | if (pending == 0) |
|
58 | 58 | m_promise.Resolve(m_source.Length); |
|
59 | 59 | } catch (Exception e) { |
|
60 | 60 | m_promise.Reject(e); |
|
61 | 61 | } |
|
62 | 62 | } |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | class ArrayMapper<TSrc, TDst>: DispatchPool<int> { |
|
66 | 66 | readonly Func<TSrc, TDst> m_transform; |
|
67 | 67 | readonly TSrc[] m_source; |
|
68 | 68 | readonly TDst[] m_dest; |
|
69 | 69 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); |
|
70 | 70 | readonly TraceContext m_traceContext; |
|
71 | 71 | |
|
72 | 72 | int m_pending; |
|
73 | 73 | int m_next; |
|
74 | 74 | |
|
75 | 75 | public ArrayMapper(TSrc[] source, Func<TSrc, TDst> transform, int threads) |
|
76 | 76 | : base(threads) { |
|
77 | 77 | |
|
78 | 78 | Debug.Assert (source != null); |
|
79 | 79 | Debug.Assert( transform != null); |
|
80 | 80 | |
|
81 | 81 | m_next = 0; |
|
82 | 82 | m_source = source; |
|
83 | 83 | m_dest = new TDst[source.Length]; |
|
84 | 84 | m_pending = source.Length; |
|
85 | 85 | m_transform = transform; |
|
86 | 86 | m_traceContext = TraceContext.Snapshot(); |
|
87 | 87 | |
|
88 |
m_promise. |
|
|
88 | m_promise.Anyway(Dispose); | |
|
89 | 89 | |
|
90 | 90 | InitPool(); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | public Promise<TDst[]> Promise { |
|
94 | 94 | get { |
|
95 | 95 | return m_promise; |
|
96 | 96 | } |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | protected override void Worker() { |
|
100 | 100 | TraceContext.Fork(m_traceContext); |
|
101 | 101 | base.Worker(); |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | protected override bool TryDequeue(out int unit) { |
|
105 | 105 | unit = Interlocked.Increment(ref m_next) - 1; |
|
106 | 106 | return unit >= m_source.Length ? false : true; |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | protected override void InvokeUnit(int unit) { |
|
110 | 110 | try { |
|
111 | 111 | m_dest[unit] = m_transform(m_source[unit]); |
|
112 | 112 | var pending = Interlocked.Decrement(ref m_pending); |
|
113 | 113 | if (pending == 0) |
|
114 | 114 | m_promise.Resolve(m_dest); |
|
115 | 115 | } catch (Exception e) { |
|
116 | 116 | m_promise.Reject(e); |
|
117 | 117 | } |
|
118 | 118 | } |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | public static IPromise<TDst[]> ParallelMap<TSrc, TDst> (this TSrc[] source, Func<TSrc,TDst> transform, int threads) { |
|
122 | 122 | if (source == null) |
|
123 | 123 | throw new ArgumentNullException("source"); |
|
124 | 124 | if (transform == null) |
|
125 | 125 | throw new ArgumentNullException("transform"); |
|
126 | 126 | |
|
127 | 127 | var mapper = new ArrayMapper<TSrc, TDst>(source, transform, threads); |
|
128 | 128 | return mapper.Promise; |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | public static IPromise<int> ParallelForEach<TSrc>(this TSrc[] source, Action<TSrc> action, int threads) { |
|
132 | 132 | if (source == null) |
|
133 | 133 | throw new ArgumentNullException("source"); |
|
134 | 134 | if (action == null) |
|
135 | 135 | throw new ArgumentNullException("action"); |
|
136 | 136 | |
|
137 | 137 | var iter = new ArrayIterator<TSrc>(source, action, threads); |
|
138 | 138 | return iter.Promise; |
|
139 | 139 | } |
|
140 | 140 | |
|
141 |
public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, |
|
|
141 | public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ResultMapper<TSrc, IPromise<TDst>> transform, int threads) { | |
|
142 | 142 | if (source == null) |
|
143 | 143 | throw new ArgumentNullException("source"); |
|
144 | 144 | if (transform == null) |
|
145 | 145 | throw new ArgumentNullException("transform"); |
|
146 | 146 | if (threads <= 0) |
|
147 | 147 | throw new ArgumentOutOfRangeException("Threads number must be greater then zero"); |
|
148 | 148 | |
|
149 | 149 | if (source.Length == 0) |
|
150 | 150 | return Promise<TDst[]>.ResultToPromise(new TDst[0]); |
|
151 | 151 | |
|
152 | 152 | var promise = new Promise<TDst[]>(); |
|
153 | 153 | var res = new TDst[source.Length]; |
|
154 | 154 | var pending = source.Length; |
|
155 | 155 | |
|
156 | 156 | var semaphore = new Semaphore(threads, threads); |
|
157 | 157 | |
|
158 | 158 | // Analysis disable AccessToDisposedClosure |
|
159 | 159 | AsyncPool.InvokeNewThread(() => { |
|
160 | 160 | for (int i = 0; i < source.Length; i++) { |
|
161 | 161 | if(promise.IsResolved) |
|
162 | 162 | break; // stop processing in case of error or cancellation |
|
163 | 163 | var idx = i; |
|
164 | 164 | |
|
165 | 165 | semaphore.WaitOne(); |
|
166 | 166 | try { |
|
167 | 167 | var p1 = transform(source[i]); |
|
168 |
p1. |
|
|
168 | p1.Anyway(() => semaphore.Release()); | |
|
169 | 169 | p1.Then( |
|
170 | 170 | x => { |
|
171 | 171 | res[idx] = x; |
|
172 | 172 | var left = Interlocked.Decrement(ref pending); |
|
173 | 173 | if (left == 0) |
|
174 | 174 | promise.Resolve(res); |
|
175 | 175 | }, |
|
176 | 176 | e => { |
|
177 | 177 | promise.Reject(e); |
|
178 | 178 | throw new TransientPromiseException(e); |
|
179 | 179 | } |
|
180 | 180 | ); |
|
181 | 181 | |
|
182 | 182 | } catch (Exception e) { |
|
183 | 183 | promise.Reject(e); |
|
184 | 184 | } |
|
185 | 185 | } |
|
186 | 186 | return 0; |
|
187 | 187 | }); |
|
188 | 188 | |
|
189 |
return promise. |
|
|
189 | return promise.Anyway(semaphore.Dispose); | |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | } |
|
193 | 193 | } |
@@ -1,764 +1,820 | |||
|
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 | public delegate IPromise<TNew> ChainedOperation<in TSrc,TNew>(TSrc result); | |
|
15 | 14 | |
|
16 | 15 | /// <summary> |
|
17 | 16 | /// ΠΠ»Π°ΡΡ Π΄Π»Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ². Π’Π°ΠΊ Π½Π°Π·ΡΠ²Π°Π΅ΠΌΠΎΠ΅ "ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅". |
|
18 | 17 | /// </summary> |
|
19 | 18 | /// <typeparam name="T">Π’ΠΈΠΏ ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°</typeparam> |
|
20 | 19 | /// <remarks> |
|
21 | 20 | /// <para>Π‘Π΅ΡΠ²ΠΈΡ ΠΏΡΠΈ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ Π΅Π³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Ρ Π΄Π°Π΅Ρ ΠΎΠ±Π΅ΡΠ°ΠΈΠ½ΠΈΠ΅ ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, |
|
22 | 21 | /// ΠΊΠ»ΠΈΠ΅Π½Ρ ΠΏΠΎΠ»ΡΡΠΈΠ² ΡΠ°ΠΊΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΡΡΠ΄ ΠΎΠ±ΡΠ°ΡΠ½ΡΡ Π²ΡΠ·ΠΎΠ²ΠΎ Π΄Π»Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ |
|
23 | 22 | /// ΡΠΎΠ±ΡΡΠΈΠΉ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΡΠΎΠ΅ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠ΅Π΄ΠΎΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².</para> |
|
24 | 23 | /// <para> |
|
25 | 24 | /// ΠΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠ°ΠΊ ΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠ»Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ Π½Π° |
|
26 | 25 | /// Π΄Π°Π½Π½ΡΠ΅ ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Then</c>. |
|
27 | 26 | /// </para> |
|
28 | 27 | /// <para> |
|
29 | 28 | /// Π‘Π΅ΡΠ²ΠΈΡ, Π² ΡΠ²ΠΎΡ ΠΎΡΠ΅ΡΠ΅Π΄Ρ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ (Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ), |
|
30 | 29 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄Ρ <c>Resolve</c> Π»ΠΈΠ±ΠΎ <c>Reject</c> Π΄Π»Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅ΡΠ½Π° ΠΎ |
|
31 | 30 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
32 | 31 | /// </para> |
|
33 | 32 | /// <para> |
|
34 | 33 | /// ΠΡΠ»ΠΈ ΡΠ΅ΡΠ²Π΅Ρ ΡΡΠΏΠ΅Π» Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΅ΡΠ΅ Π΄ΠΎ ΡΠΎΠ³ΠΎ, ΠΊΠ°ΠΊ ΠΊΠ»ΠΈΠ΅Π½Ρ Π½Π° Π½Π΅Π³ΠΎ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π»ΡΡ, |
|
35 | 34 | /// ΡΠΎ Π² ΠΌΠΎΠΌΠ΅Π½Ρ ΠΏΠΎΠ΄ΠΏΠΈΡΠΊΠΈ ΠΊΠ»ΠΈΠ΅Π½ΡΠ° Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠΈΠ΅ ΡΠΎΠ±ΡΡΠΈΡ Π² ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΌ |
|
36 | 35 | /// ΡΠ΅ΠΆΠΈΠΌΠ΅ ΠΈ ΠΊΠ»ΠΈΠ΅Π½Ρ Π±ΡΠ΄Π΅Ρ ΠΎΠΏΠΎΠ²Π΅ΡΠ΅Π½ Π² Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅. ΠΠ½Π°ΡΠ΅, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π΄ΠΎΠ±Π°Π²Π»ΡΡΡΡΡ Π² |
|
37 | 36 | /// ΡΠΏΠΈΡΠΎΠΊ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ |
|
38 | 37 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
39 | 38 | /// </para> |
|
40 | 39 | /// <para> |
|
41 | 40 | /// ΠΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²ΡΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ Π»ΠΈΠ±ΠΎ ΠΈΠ½ΠΈΡΠΈΠΈΡΠΎΠ²Π°ΡΡ |
|
42 | 41 | /// ΡΠ²ΡΠ·Π°Π½Π½ΡΠ΅ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°ΠΊΠΆΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΡΠ»Π΅Π΄ΡΠ΅Ρ |
|
43 | 42 | /// ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΡΠΎΡΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Π΅ <c>Then</c>. |
|
44 | 43 | /// </para> |
|
45 | 44 | /// <para> |
|
46 | 45 | /// Π’Π°ΠΊΠΆΠ΅ Ρ ΠΎΡΠΎΡΠΈΠΌ ΠΏΡΠ°Π²ΠΈΠ»ΠΎΠΌ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ <c>Resolve</c> ΠΈ <c>Reject</c> Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠ·ΡΠ²Π°ΡΡ |
|
47 | 46 | /// ΡΠΎΠ»ΡΠΊΠΎ ΠΈΠ½ΠΈΡΠΈΠ°ΡΠΎΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈΠ½Π°ΡΠ΅ ΠΌΠΎΠ³ΡΡ Π²ΠΎΠ·Π½ΠΈΠΊΠ½ΡΡΡ ΠΏΡΠΎΡΠΈΠ²ΠΎΡΠ΅ΡΠΈΡ. |
|
48 | 47 | /// </para> |
|
49 | 48 | /// </remarks> |
|
50 | 49 | public class Promise<T> : IPromise<T> { |
|
51 | 50 | |
|
52 | 51 | protected struct HandlerDescriptor { |
|
53 | 52 | public ResultHandler<T> resultHandler; |
|
54 | 53 | public ErrorHandler<T> errorHandler; |
|
55 | 54 | public Action cancellHandler; |
|
56 | 55 | public Promise<T> medium; |
|
57 | 56 | |
|
58 | 57 | public void Resolve(T result) { |
|
59 | 58 | if (resultHandler != null) { |
|
60 | 59 | try { |
|
61 | 60 | resultHandler(result); |
|
62 | 61 | } catch (Exception e) { |
|
63 | 62 | Reject(e); |
|
64 | 63 | return; |
|
65 | 64 | } |
|
66 | 65 | } |
|
67 | 66 | if (medium != null) |
|
68 | 67 | medium.Resolve(result); |
|
69 | 68 | } |
|
70 | 69 | |
|
71 | 70 | public void Reject(Exception err) { |
|
72 | 71 | if (errorHandler != null) { |
|
73 | 72 | try { |
|
74 | 73 | var res = errorHandler(err); |
|
75 | 74 | if (medium != null) |
|
76 | 75 | medium.Resolve(res); |
|
77 | 76 | } catch (TransientPromiseException err2) { |
|
78 | 77 | if (medium != null) |
|
79 | 78 | medium.Reject(err2.InnerException); |
|
80 | 79 | } catch (Exception err2) { |
|
81 | 80 | if (medium != null) |
|
82 | 81 | medium.Reject(err2); |
|
83 | 82 | } |
|
84 | 83 | } else if (medium != null) |
|
85 | 84 | medium.Reject(err); |
|
86 | 85 | } |
|
87 | 86 | |
|
88 | 87 | public void Cancel() { |
|
89 | 88 | if (cancellHandler != null) { |
|
90 | 89 | try { |
|
91 | 90 | cancellHandler(); |
|
92 | 91 | } catch (Exception err) { |
|
93 | 92 | Reject(err); |
|
94 | 93 | return; |
|
95 | 94 | } |
|
96 | 95 | } |
|
97 | 96 | if (medium != null) |
|
98 | 97 | medium.Cancel(); |
|
99 | 98 | } |
|
100 | 99 | } |
|
101 | 100 | |
|
102 | 101 | const int UNRESOLVED_SATE = 0; |
|
103 | 102 | const int TRANSITIONAL_STATE = 1; |
|
104 | 103 | const int SUCCEEDED_STATE = 2; |
|
105 | 104 | const int REJECTED_STATE = 3; |
|
106 | 105 | const int CANCELLED_STATE = 4; |
|
107 | 106 | |
|
108 | 107 | readonly bool m_cancellable; |
|
109 | 108 | |
|
110 | 109 | int m_childrenCount = 0; |
|
111 | 110 | int m_state; |
|
112 | 111 | T m_result; |
|
113 | 112 | Exception m_error; |
|
114 | 113 | |
|
115 | 114 | readonly MTQueue<HandlerDescriptor> m_handlers = new MTQueue<HandlerDescriptor>(); |
|
116 | 115 | |
|
117 | 116 | public Promise() { |
|
118 | 117 | m_cancellable = true; |
|
119 | 118 | } |
|
120 | 119 | |
|
121 | 120 | public Promise(IPromise parent, bool cancellable) { |
|
122 | 121 | m_cancellable = cancellable; |
|
123 | 122 | if (parent != null) |
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
123 | AddHandler( | |
|
124 | null, | |
|
125 | null, | |
|
126 | () => { | |
|
127 | if (parent.IsExclusive) | |
|
128 | parent.Cancel(); | |
|
129 | }, | |
|
130 | null | |
|
131 | ); | |
|
128 | 132 | } |
|
129 | 133 | |
|
130 | 134 | bool BeginTransit() { |
|
131 | 135 | return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE); |
|
132 | 136 | } |
|
133 | 137 | |
|
134 | 138 | void CompleteTransit(int state) { |
|
135 | 139 | if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE)) |
|
136 | 140 | throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state"); |
|
137 | 141 | } |
|
138 | 142 | |
|
139 | 143 | void WaitTransition() { |
|
140 | 144 | while (m_state == TRANSITIONAL_STATE) { |
|
141 | 145 | /* noop */ |
|
142 | 146 | } |
|
143 | 147 | } |
|
144 | 148 | |
|
145 | 149 | public bool IsResolved { |
|
146 | 150 | get { |
|
147 | 151 | return m_state > 1; |
|
148 | 152 | } |
|
149 | 153 | } |
|
150 | 154 | |
|
151 | 155 | public bool IsCancelled { |
|
152 | 156 | get { |
|
153 | 157 | return m_state == CANCELLED_STATE; |
|
154 | 158 | } |
|
155 | 159 | } |
|
156 | 160 | |
|
157 | 161 | public Type PromiseType { |
|
158 | 162 | get { return typeof(T); } |
|
159 | 163 | } |
|
160 | 164 | |
|
161 | 165 | /// <summary> |
|
162 | 166 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. |
|
163 | 167 | /// </summary> |
|
164 | 168 | /// <param name="result">Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ.</param> |
|
165 | 169 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
166 | 170 | public void Resolve(T result) { |
|
167 | 171 | if (BeginTransit()) { |
|
168 | 172 | m_result = result; |
|
169 | 173 | CompleteTransit(SUCCEEDED_STATE); |
|
170 | 174 | OnStateChanged(); |
|
171 | 175 | } else { |
|
172 | 176 | WaitTransition(); |
|
173 | 177 | if (m_state != CANCELLED_STATE) |
|
174 | 178 | throw new InvalidOperationException("The promise is already resolved"); |
|
175 | 179 | } |
|
176 | 180 | } |
|
177 | 181 | |
|
178 | 182 | /// <summary> |
|
179 | 183 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΡΡΠΏΠ΅ΡΠ½ΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ. Π Π΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π±ΡΠ΄Π΅Ρ ΠΏΡΡΡΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ. |
|
180 | 184 | /// </summary> |
|
181 | 185 | /// <remarks> |
|
182 | 186 | /// ΠΠ°Π½Π½ΡΠΉ Π²Π°ΡΠΈΠ°Π½Ρ ΡΠ΄ΠΎΠ±Π΅Π½ Π² ΡΠ»ΡΡΠ°ΡΡ , ΠΊΠΎΠ³Π΄Π° ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ΅Π½ ΡΠ°ΠΊΡ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π΅ΠΆΠ΅Π»ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅. |
|
183 | 187 | /// </remarks> |
|
184 | 188 | public void Resolve() { |
|
185 | 189 | Resolve(default(T)); |
|
186 | 190 | } |
|
187 | 191 | |
|
188 | 192 | /// <summary> |
|
189 | 193 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΡΠΎΠΎΠ±ΡΠ°Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ |
|
190 | 194 | /// </summary> |
|
191 | 195 | /// <remarks> |
|
192 | 196 | /// ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΌΠ½ΠΎΠ³ΠΎΠΏΡΠΎΡΠ½ΠΎΠΉ ΡΡΠ΅Π΄Π΅, ΠΏΡΠΈ Π΅Π³ΠΎ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΡΠ°Π·Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠΎΠΊΠΎΠ² |
|
193 | 197 | /// ΠΌΠΎΠ³Ρ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠΈΠ±ΠΊΡ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠ΅ΡΠ²Π°Ρ Π±ΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½Π° Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, ΠΎΡΡΠ°Π»ΡΠ½ΡΠ΅ |
|
194 | 198 | /// Π±ΡΠ΄ΡΡ ΠΏΡΠΎΠΈΠ³Π½ΠΎΡΠΈΡΠΎΠ²Π°Π½Ρ. |
|
195 | 199 | /// </remarks> |
|
196 | 200 | /// <param name="error">ΠΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ</param> |
|
197 | 201 | /// <exception cref="InvalidOperationException">ΠΠ°Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ</exception> |
|
198 | 202 | public void Reject(Exception error) { |
|
199 | 203 | if (BeginTransit()) { |
|
200 | 204 | m_error = error; |
|
201 | 205 | CompleteTransit(REJECTED_STATE); |
|
202 | 206 | OnStateChanged(); |
|
203 | 207 | } else { |
|
204 | 208 | WaitTransition(); |
|
205 | 209 | if (m_state == SUCCEEDED_STATE) |
|
206 | 210 | throw new InvalidOperationException("The promise is already resolved"); |
|
207 | 211 | } |
|
208 | 212 | } |
|
209 | 213 | |
|
210 | 214 | /// <summary> |
|
211 | 215 | /// ΠΡΠΌΠ΅Π½ΡΠ΅Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, Π΅ΡΠ»ΠΈ ΡΡΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ. |
|
212 | 216 | /// </summary> |
|
213 | /// <returns><c>true</c> ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ»Π° ΠΎΡΠΌΠ΅Π½Π΅Π½Π°, ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ Π½Π΅ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ.<c>false</c> ΠΎΡΠΌΠ΅Π½Π° Π½Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π°, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ ΠΎΡΡΠ°Π±ΠΎΡΠ°Π»ΠΈ.</returns> | |
|
214 |
public |
|
|
217 | /// <remarks>ΠΠ»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ Π±ΡΠ»Π° Π»ΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Π° ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ <see cref="IsCancelled"/>.</remarks> | |
|
218 | public void Cancel() { | |
|
215 | 219 | if (m_cancellable && BeginTransit()) { |
|
216 | 220 | CompleteTransit(CANCELLED_STATE); |
|
217 | 221 | OnStateChanged(); |
|
218 | return true; | |
|
219 | 222 | } |
|
220 | return false; | |
|
221 | 223 | } |
|
222 | 224 | |
|
223 | // ΡΠ΄Π΅Π»Π°Π½ΠΎ Π΄Π»Ρ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΠΎΠ³ΠΎ ΡΠΈΠΏΠ° void | |
|
224 | protected void InternalCancel() { | |
|
225 | Cancel(); | |
|
226 | } | |
|
227 | ||
|
228 | ||
|
229 | 225 | public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error, Action cancel) { |
|
230 | 226 | if (success == null && error == null && cancel == null) |
|
231 | 227 | return this; |
|
232 | 228 | |
|
233 | 229 | var medium = new Promise<T>(this, true); |
|
234 | 230 | |
|
235 | 231 | AddHandler(success, error, cancel, medium); |
|
236 | 232 | |
|
237 | 233 | return medium; |
|
238 | 234 | } |
|
239 | 235 | |
|
240 | 236 | /// <summary> |
|
241 | 237 | /// Adds new handlers to this promise. |
|
242 | 238 | /// </summary> |
|
243 | 239 | /// <param name="success">The handler of the successfully completed operation. |
|
244 | 240 | /// This handler will recieve an operation result as a parameter.</param> |
|
245 | 241 | /// <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> |
|
246 | 242 | /// <returns>The new promise chained to this one.</returns> |
|
247 | 243 | public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { |
|
248 | 244 | if (success == null && error == null) |
|
249 | 245 | return this; |
|
250 | 246 | |
|
251 | 247 | var medium = new Promise<T>(this, true); |
|
252 | 248 | |
|
253 | 249 | AddHandler(success, error, null, medium); |
|
254 | 250 | |
|
255 | 251 | return medium; |
|
256 | 252 | } |
|
257 | 253 | |
|
258 | public IPromise Then(Action success, ErrorHandler error, Action cancel) { | |
|
259 | return Then( | |
|
260 | x => success(), | |
|
261 | e => { | |
|
262 | error(e); | |
|
263 | return default(T); | |
|
264 | }, | |
|
265 | cancel | |
|
266 | ); | |
|
267 | } | |
|
268 | ||
|
269 | public IPromise Then(Action success, ErrorHandler error) { | |
|
270 | return Then( | |
|
271 | x => success(), | |
|
272 | e => { | |
|
273 | error(e); | |
|
274 | return default(T); | |
|
275 | } | |
|
276 | ); | |
|
277 | } | |
|
278 | ||
|
279 | public IPromise Then(Action success) { | |
|
280 | return Then(x => success()); | |
|
281 | } | |
|
254 | ||
|
282 | 255 | |
|
283 | 256 | |
|
284 | 257 | public IPromise<T> Then(ResultHandler<T> success) { |
|
285 | 258 | if (success == null) |
|
286 | 259 | return this; |
|
287 | 260 | |
|
288 | 261 | var medium = new Promise<T>(this, true); |
|
289 | 262 | |
|
290 | 263 | AddHandler(success, null, null, medium); |
|
291 | 264 | |
|
292 | 265 | return medium; |
|
293 | 266 | } |
|
294 | 267 | |
|
268 | /// <summary> | |
|
269 | /// ΠΠΎΡΠ»Π΅Π΄Π½ΠΈΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ Π² ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. | |
|
270 | /// </summary> | |
|
271 | /// <param name="success"></param> | |
|
272 | /// <param name="error"></param> | |
|
273 | /// <param name="cancel"></param> | |
|
274 | /// <remarks> | |
|
275 | /// <para> | |
|
276 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π½Π΅ ΡΠΎΠ·Π΄Π°Π΅Ρ ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠ³ΠΎ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ ΠΏΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½ Π΄Π»Ρ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ | |
|
277 | /// ΡΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΡΠ΅ΠΏΠΎΡΠΊΠΈ. | |
|
278 | /// </para> | |
|
279 | /// <para> | |
|
280 | /// ΠΡΠ»ΠΈ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·Π²Π°ΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠ°Π·, Π»ΠΈΠ±ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΡΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ° | |
|
281 | /// Π½Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΠ΄ΠΈΠ½ΠΎΡΠ½ΠΎΠΉ <see cref="IsExclusive"/> ΠΈ, ΠΊΠ°ΠΊ ΡΠ»Π΅Π΄ΡΡΠ²ΠΈΠ΅, Π±ΡΠ΄Π΅Ρ Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΠΎΡΠΌΠ΅Π½Π° | |
|
282 | /// Π²ΡΠ΅ΠΉ ΡΠ΅ΠΏΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΡΠ½ΠΈΠ·Ρ (Ρ ΡΠ°ΠΌΠΎΠ³ΠΎ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅Π³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ). | |
|
283 | /// </para> | |
|
284 | /// </remarks> | |
|
295 | 285 | public void Last(ResultHandler<T> success, ErrorHandler error, Action cancel) { |
|
296 | 286 | if (success == null && error == null && cancel == null) |
|
297 | 287 | return; |
|
298 | 288 | |
|
299 | 289 | ErrorHandler<T> errorHandler = null; |
|
300 | 290 | if (error != null) |
|
301 | 291 | errorHandler = err => { |
|
302 | 292 | error(err); |
|
303 | 293 | return default(T); |
|
304 | 294 | }; |
|
305 | 295 | AddHandler(success, errorHandler, cancel, null); |
|
306 | 296 | } |
|
307 | 297 | |
|
308 | 298 | public void Last(ResultHandler<T> success, ErrorHandler error) { |
|
309 | 299 | Last(success, error, null); |
|
310 | 300 | } |
|
311 | 301 | |
|
312 | 302 | public void Last(ResultHandler<T> success) { |
|
313 | 303 | Last(success, null, null); |
|
314 | 304 | } |
|
315 | 305 | |
|
316 | public void Last(Action success,ErrorHandler error, Action cancel) { | |
|
317 | Last(x => success(), error, cancel); | |
|
318 | } | |
|
319 | ||
|
320 | public void Last(Action success,ErrorHandler error) { | |
|
321 | Last(x => success(), error, null); | |
|
322 | } | |
|
323 | ||
|
324 | public void Last(Action success) { | |
|
325 | Last(x => success(), null, null); | |
|
326 | } | |
|
327 | ||
|
328 | 306 | public IPromise Error(ErrorHandler error) { |
|
329 | 307 | if (error == null) |
|
330 | 308 | return this; |
|
331 | 309 | |
|
332 | 310 | var medium = new Promise<T>(this, true); |
|
333 | 311 | |
|
334 | 312 | AddHandler( |
|
335 | 313 | null, |
|
336 | 314 | e => { |
|
337 | 315 | error(e); |
|
338 | 316 | return default(T); |
|
339 | 317 | }, |
|
340 | 318 | null, |
|
341 | 319 | medium |
|
342 | 320 | ); |
|
343 | 321 | |
|
344 | 322 | return medium; |
|
345 | 323 | } |
|
346 | 324 | |
|
347 | 325 | /// <summary> |
|
348 | 326 | /// Handles error and allows to keep the promise. |
|
349 | 327 | /// </summary> |
|
350 | 328 | /// <remarks> |
|
351 | 329 | /// If the specified handler throws an exception, this exception will be used to reject the promise. |
|
352 | 330 | /// </remarks> |
|
353 | 331 | /// <param name="handler">The error handler which returns the result of the promise.</param> |
|
354 | 332 | /// <returns>New promise.</returns> |
|
355 | 333 | public IPromise<T> Error(ErrorHandler<T> handler) { |
|
356 | 334 | if (handler == null) |
|
357 | 335 | return this; |
|
358 | 336 | |
|
359 | 337 | var medium = new Promise<T>(this, true); |
|
360 | 338 | |
|
361 | 339 | AddHandler(null, handler, null, medium); |
|
362 | 340 | |
|
363 | 341 | return medium; |
|
364 | 342 | } |
|
365 | 343 | |
|
366 | 344 | /// <summary> |
|
367 | 345 | /// ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°ΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π΅Π½ΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ. |
|
368 | 346 | /// </summary> |
|
369 | 347 | /// <typeparam name="TNew">ΠΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°.</typeparam> |
|
370 | 348 | /// <param name="mapper">ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΠΊ Π½ΠΎΠ²ΠΎΠΌΡ ΡΠΈΠΏΡ.</param> |
|
371 | 349 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
372 | 350 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
373 | 351 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ.</returns> |
|
374 | public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<T> error) { | |
|
375 | if (mapper == null) | |
|
376 | throw new ArgumentNullException("mapper"); | |
|
352 | public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error, Action cancel) { | |
|
353 | Safe.ArgumentNotNull(mapper, "mapper"); | |
|
354 | ||
|
355 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ | |
|
356 | var medium = new Promise<TNew>(this, true); | |
|
377 | 357 | |
|
378 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΡΠΈΡΠ΅ΠΏΠ»Π΅Π½Π½ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ | |
|
379 | var chained = new Promise<TNew>(this, true); | |
|
380 | ||
|
381 | ResultHandler<T> resultHandler = result => chained.Resolve(mapper(result)); | |
|
358 | ResultHandler<T> resultHandler = result => medium.Resolve(mapper(result)); | |
|
382 | 359 | ErrorHandler<T> errorHandler; |
|
383 | 360 | if (error != null) |
|
384 | 361 | errorHandler = e => { |
|
385 | 362 | try { |
|
386 |
|
|
|
363 | medium.Resolve(error(e)); | |
|
387 | 364 | } catch (Exception e2) { |
|
388 | 365 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ |
|
389 |
|
|
|
366 | medium.Reject(e2); | |
|
390 | 367 | } |
|
391 | 368 | return default(T); |
|
392 | 369 | }; |
|
393 | 370 | else |
|
394 | 371 | errorHandler = e => { |
|
395 |
|
|
|
372 | medium.Reject(e); | |
|
396 | 373 | return default(T); |
|
397 | 374 | }; |
|
398 | 375 | |
|
376 | Action cancelHandler; | |
|
377 | if (cancel != null) | |
|
378 | cancelHandler = () => { | |
|
379 | cancel(); | |
|
380 | medium.Cancel(); | |
|
381 | }; | |
|
382 | else | |
|
383 | cancelHandler = medium.Cancel; | |
|
384 | ||
|
399 | 385 | |
|
400 | 386 | AddHandler( |
|
401 | 387 | resultHandler, |
|
402 | 388 | errorHandler, |
|
403 |
c |
|
|
389 | cancelHandler, | |
|
404 | 390 | null |
|
405 | 391 | ); |
|
406 | 392 | |
|
407 |
return |
|
|
393 | return medium; | |
|
394 | } | |
|
395 | ||
|
396 | public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler<TNew> error) { | |
|
397 | return Then(mapper, error, null); | |
|
408 | 398 | } |
|
409 | 399 | |
|
410 | 400 | public IPromise<TNew> Then<TNew>(ResultMapper<T, TNew> mapper) { |
|
411 | return Then(mapper, null); | |
|
401 | return Then(mapper, null, null); | |
|
412 | 402 | } |
|
413 | 403 | |
|
414 | 404 | /// <summary> |
|
415 | 405 | /// Π‘ΡΠ΅ΠΏΠ»ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. Π£ΠΊΠ°Π·Π°Π½Π½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π²ΡΠ·Π²Π°Π½Π° ΠΏΠΎΡΠ»Π΅ |
|
416 | 406 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ, Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ Π΄Π»Ρ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ |
|
417 | 407 | /// Π½ΠΎΠ²ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ. |
|
418 | 408 | /// </summary> |
|
419 | 409 | /// <typeparam name="TNew">Π’ΠΈΠΏ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</typeparam> |
|
420 | 410 | /// <param name="chained">ΠΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄ΠΎΠ»ΠΆΠ½Π° Π±ΡΠ΄Π΅Ρ Π½Π°ΡΠ°ΡΡΡΡ ΠΏΠΎΡΠ»Π΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΠ΅ΠΊΡΡΠ΅ΠΉ.</param> |
|
421 | 411 | /// <param name="error">ΠΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΠ°Π½Π½ΡΠΉ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΠΎΠ»ΡΡΠΈΡ |
|
422 | 412 | /// ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π²ΠΎΠ·Π½ΠΈΠΊΡΠ΅Π΅ ΠΏΡΠΈ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ΅ΠΊΡΠ΅ΡΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> |
|
423 | 413 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΉ Π°ΡΠ½Ρ ΡΠΎΠ½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</returns> |
|
424 |
public IPromise<TNew> |
|
|
414 | public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error, Action cancel) { | |
|
415 | ||
|
416 | Safe.ArgumentNotNull(chained, "chained"); | |
|
425 | 417 | |
|
426 | 418 | // ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Π² ΡΠΎΠΌ, ΡΡΠΎ Π½Π° ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ Π΅ΡΠ΅ Π½Π΅ Π½Π°ΡΠ°ΡΠ° Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΏΠΎΡΡΠΎΠΌΡ Π½ΡΠΆΠ½ΠΎ |
|
427 | 419 | // ΡΠΎΠ·Π΄Π°ΡΡ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΠ°, ΠΊ ΠΊΠΎΡΠΎΡΠΎΠΌΡ Π±ΡΠ΄ΡΡ ΠΏΠΎΠ΄Π²ΡΠ·ΡΠ²Π°ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ. |
|
428 | 420 | // ΠΊΠΎΠ³Π΄Π° Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π° ΡΠ΅Π°Π»ΡΠ½Π°Ρ Π°ΡΠΈΠ½Ρ ΡΠΎΠ½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ, ΠΎΠ½Π° ΠΎΠ±ΡΠ°ΡΠΈΡΡΡΡ ΠΊ ΠΏΠΎΡΡΠ΅Π΄Π½ΠΈΠΊΡ, ΡΡΠΎΠ±Ρ |
|
429 | 421 | // ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π· Π½Π΅Π³ΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡ. |
|
430 | 422 | var medium = new Promise<TNew>(this, true); |
|
431 | 423 | |
|
432 | 424 | ResultHandler<T> resultHandler = delegate(T result) { |
|
433 | 425 | if (medium.IsCancelled) |
|
434 | 426 | return; |
|
435 | 427 | |
|
436 | 428 | var promise = chained(result); |
|
437 | 429 | |
|
438 |
promise. |
|
|
430 | promise.Last( | |
|
439 | 431 | medium.Resolve, |
|
440 |
|
|
|
441 | medium.Reject(err); | |
|
442 | throw new TransientPromiseException(err); | |
|
443 | } | |
|
432 | medium.Reject, | |
|
433 | () => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ° | |
|
444 | 434 | ); |
|
445 | 435 | |
|
446 | 436 | // notify chained operation that it's not needed anymore |
|
447 | 437 | // ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ |
|
448 | 438 | // Π·Π°Π²ΠΈΡΠΈΡ IsExclusive |
|
449 | 439 | medium.Cancelled(() => { |
|
450 | 440 | if (promise.IsExclusive) |
|
451 | 441 | promise.Cancel(); |
|
452 | 442 | }); |
|
453 | ||
|
454 | // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ° | |
|
455 | promise.Cancelled(() => medium.Reject(new OperationCanceledException())); | |
|
456 | 443 | }; |
|
457 | 444 | |
|
458 |
ErrorHandler<T> errorHandler |
|
|
459 | if (error != null) { | |
|
445 | ErrorHandler<T> errorHandler; | |
|
446 | ||
|
447 | if (error != null) | |
|
448 | errorHandler = delegate(Exception e) { | |
|
460 | 449 | try { |
|
461 |
|
|
|
450 | var promise = error(e); | |
|
451 | ||
|
452 | promise.Last( | |
|
453 | medium.Resolve, | |
|
454 | medium.Reject, | |
|
455 | () => medium.Reject(new OperationCanceledException()) // Π²Π½Π΅ΡΠ½ΡΡ ΠΎΡΠΌΠ΅Π½Π° ΡΠ²ΡΠ·Π°Π½Π½ΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°Π΅ΡΡΡ ΠΊΠ°ΠΊ ΠΎΡΠΈΠ±ΠΊΠ° | |
|
456 | ); | |
|
457 | ||
|
458 | // notify chained operation that it's not needed anymore | |
|
459 | // ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π²ΡΠ·ΠΎΠ²Π° Then, Cancelled Π²Π°ΠΆΠ΅Π½, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΎΡ ΡΡΠΎΠ³ΠΎ | |
|
460 | // Π·Π°Π²ΠΈΡΠΈΡ IsExclusive | |
|
461 | medium.Cancelled(() => { | |
|
462 | if (promise.IsExclusive) | |
|
463 | promise.Cancel(); | |
|
464 | }); | |
|
462 | 465 | } catch (Exception e2) { |
|
463 | 466 | medium.Reject(e2); |
|
464 | return default(T); | |
|
465 | 467 | } |
|
466 | } | |
|
467 | // Π² ΡΠ»ΡΡΠ°Π΅ ΠΎΡΠΈΠ±ΠΊΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ Π΄Π°Π»ΡΡΠ΅ ΠΏΠΎ ΡΠ΅ΠΏΠΎΡΠΊΠ΅ | |
|
468 | medium.Reject(e); | |
|
469 | return default(T); | |
|
470 | }; | |
|
468 | return default(T); | |
|
469 | }; | |
|
470 | else | |
|
471 | errorHandler = err => { | |
|
472 | medium.Reject(err); | |
|
473 | return default(T); | |
|
474 | }; | |
|
475 | ||
|
476 | ||
|
477 | Action cancelHandler; | |
|
478 | if (cancel != null) | |
|
479 | cancelHandler = () => { | |
|
480 | if (cancel != null) | |
|
481 | cancel(); | |
|
482 | medium.Cancel(); | |
|
483 | }; | |
|
484 | else | |
|
485 | cancelHandler = medium.Cancel; | |
|
471 | 486 | |
|
472 | 487 | AddHandler( |
|
473 | 488 | resultHandler, |
|
474 | 489 | errorHandler, |
|
475 |
|
|
|
490 | cancelHandler, | |
|
476 | 491 | null |
|
477 | 492 | ); |
|
478 | 493 | |
|
479 | 494 | return medium; |
|
480 | 495 | } |
|
481 | 496 | |
|
482 |
public IPromise<TNew> |
|
|
483 |
return |
|
|
497 | public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained, ErrorHandler<IPromise<TNew>> error) { | |
|
498 | return Chain(chained, error, null); | |
|
499 | } | |
|
500 | ||
|
501 | public IPromise<TNew> Chain<TNew>(ResultMapper<T, IPromise<TNew>> chained) { | |
|
502 | return Chain(chained, null, null); | |
|
484 | 503 | } |
|
485 | 504 | |
|
486 | 505 | public IPromise<T> Cancelled(Action handler) { |
|
487 |
var medium = new Promise<T>(this, |
|
|
506 | var medium = new Promise<T>(this,true); | |
|
488 | 507 | AddHandler(null, null, handler, medium); |
|
489 | 508 | return medium; |
|
490 | 509 | } |
|
491 | 510 | |
|
492 | 511 | /// <summary> |
|
493 | 512 | /// Adds the specified handler for all cases (success, error, cancel) |
|
494 | 513 | /// </summary> |
|
495 | 514 | /// <param name="handler">The handler that will be called anyway</param> |
|
496 | 515 | /// <returns>self</returns> |
|
497 |
public IPromise<T> |
|
|
498 | if (handler == null) | |
|
499 | throw new ArgumentNullException("handler"); | |
|
516 | public IPromise<T> Anyway(Action handler) { | |
|
517 | Safe.ArgumentNotNull(handler, "handler"); | |
|
518 | ||
|
500 | 519 | AddHandler( |
|
501 | 520 | x => handler(), |
|
502 | 521 | e => { |
|
503 | 522 | handler(); |
|
504 | 523 | throw new TransientPromiseException(e); |
|
505 | 524 | }, |
|
506 | 525 | handler, |
|
507 | 526 | null |
|
508 | 527 | ); |
|
509 | 528 | return this; |
|
510 | 529 | } |
|
511 | 530 | |
|
512 | 531 | /// <summary> |
|
513 | 532 | /// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΊ Π½ΡΠΆΠ½ΠΎΠΌΡ ΡΠΈΠΏΡ |
|
514 | 533 | /// </summary> |
|
515 | 534 | /// <typeparam name="T2"></typeparam> |
|
516 | 535 | /// <returns></returns> |
|
517 | 536 | public IPromise<T2> Cast<T2>() { |
|
518 | 537 | return Then(x => (T2)(object)x, null); |
|
519 | 538 | } |
|
520 | 539 | |
|
521 | 540 | /// <summary> |
|
522 | 541 | /// ΠΠΎΠΆΠΈΠ΄Π°Π΅ΡΡΡ ΠΎΡΠ»ΠΎΠΆΠ΅Π½Π½ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ Π°, Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ |
|
523 | 542 | /// Π΅Π³ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ, Π² ΠΏΡΠΎΡΠΈΠ²Π½ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π±ΡΠΎΡΠ°Π΅Ρ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅. |
|
524 | 543 | /// </summary> |
|
525 | 544 | /// <remarks> |
|
526 | 545 | /// <para> |
|
527 | 546 | /// ΠΡΠ»ΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ Π±ΡΠ»ΠΎ ΠΏΡΠ΅ΡΠ²Π°Π½ΠΎ ΠΏΠΎ ΡΠ°ΠΉΠΌΠ°ΡΡΡ, ΡΡΠΎ Π½Π΅ Π·Π½Π°ΡΠΈΡ, |
|
528 | 547 | /// ΡΡΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ»ΠΎ ΠΎΡΠΌΠ΅Π½Π΅Π½ΠΎ ΠΈΠ»ΠΈ ΡΡΠΎ-ΡΠΎ Π² ΡΡΠΎΠΌ ΡΠΎΠ΄Π΅, ΡΡΠΎ ΡΠΎΠ»ΡΠΊΠΎ |
|
529 | 548 | /// ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ ΠΌΡ Π΅Π³ΠΎ Π½Π΅ Π΄ΠΎΠΆΠ΄Π°Π»ΠΈΡΡ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π²ΡΠ΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ |
|
530 | 549 | /// ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΈ, ΠΊΠ°ΠΊ Π±ΡΠ»ΠΈ ΡΠ°ΠΊ ΠΎΡΡΠ°Π»ΠΈΡΡ ΠΈ ΠΎΠ½ΠΈ Π±ΡΠ΄ΡΡ Π²ΡΠ·Π²Π°Π½Ρ, ΠΊΠΎΠ³Π΄Π° |
|
531 | 550 | /// ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
532 | 551 | /// </para> |
|
533 | 552 | /// <para> |
|
534 | 553 | /// Π’Π°ΠΊΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π²ΠΏΠΎΠ»Π½Π΅ ΠΎΠΏΡΠ°Π²Π΄Π°Π½ΠΎ ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠ°ΠΉΠΌΠ°ΡΡ ΠΌΠΎΠΆΠ΅Ρ ΠΈΡΡΠ΅ΡΡ |
|
535 | 554 | /// Π² ΡΠΎΡ ΠΌΠΎΠΌΠ΅Π½Ρ, ΠΊΠΎΠ³Π΄Π° Π½Π°ΡΠ°Π»Π°ΡΡ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ° ΡΠ΅ΠΏΠΎΡΠΊΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊΠΎΠ², ΠΈ |
|
536 | 555 | /// ΠΊ ΡΠΎΠΌΡ ΠΆΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΡΠΎΡΡΡ Π² ΡΠ΅ΠΏΠΎΡΠΊΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ ΠΈ Π΅Π³ΠΎ |
|
537 | 556 | /// ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΈΠ²Π΅ΡΡΠΈ ΠΊ Π½Π΅ΠΏΡΠΎΠ³Π½ΠΎΠ·ΠΈΡΡΠ΅ΠΌΠΎΠΌΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΡ. |
|
538 | 557 | /// </para> |
|
539 | 558 | /// </remarks> |
|
540 | 559 | /// <param name="timeout">ΠΡΠ΅ΠΌΡ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΡ</param> |
|
541 | 560 | /// <returns>Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ</returns> |
|
542 | 561 | public T Join(int timeout) { |
|
543 | 562 | var evt = new ManualResetEvent(false); |
|
544 |
|
|
|
563 | Anyway(() => evt.Set()); | |
|
545 | 564 | |
|
546 | 565 | if (!evt.WaitOne(timeout, true)) |
|
547 | 566 | throw new TimeoutException(); |
|
548 | 567 | |
|
549 | 568 | switch (m_state) { |
|
550 | 569 | case SUCCEEDED_STATE: |
|
551 | 570 | return m_result; |
|
552 | 571 | case CANCELLED_STATE: |
|
553 | 572 | throw new OperationCanceledException(); |
|
554 | 573 | case REJECTED_STATE: |
|
555 | 574 | throw new TargetInvocationException(m_error); |
|
556 | 575 | default: |
|
557 | 576 | throw new ApplicationException(String.Format("Invalid promise state {0}", m_state)); |
|
558 | 577 | } |
|
559 | 578 | } |
|
560 | 579 | |
|
561 | 580 | public T Join() { |
|
562 | 581 | return Join(Timeout.Infinite); |
|
563 | 582 | } |
|
564 | 583 | |
|
565 | 584 | void AddHandler(ResultHandler<T> success, ErrorHandler<T> error, Action cancel, Promise<T> medium) { |
|
566 | 585 | if (success != null || error != null) |
|
567 | 586 | Interlocked.Increment(ref m_childrenCount); |
|
568 | 587 | |
|
569 | 588 | var handler = new HandlerDescriptor { |
|
570 | 589 | resultHandler = success, |
|
571 | 590 | errorHandler = error, |
|
572 | 591 | cancellHandler = cancel, |
|
573 | 592 | medium = medium |
|
574 | 593 | }; |
|
575 | 594 | |
|
576 | 595 | bool queued; |
|
577 | 596 | |
|
578 | 597 | if (!IsResolved) { |
|
579 | 598 | m_handlers.Enqueue(handler); |
|
580 | 599 | queued = true; |
|
581 | 600 | } else { |
|
582 | 601 | // the promise is in resolved state, just invoke the handled with minimum overhead |
|
583 | 602 | queued = false; |
|
584 | 603 | InvokeHandler(handler); |
|
585 | 604 | } |
|
586 | 605 | |
|
587 | 606 | if (queued && IsResolved && m_handlers.TryDequeue(out handler)) |
|
588 | 607 | // if the promise have been resolved while we was adding handler to the queue |
|
589 | 608 | // we can't guarantee that someone is still processing it |
|
590 | 609 | // therefore we will fetch a handler from the queue and execute it |
|
591 | 610 | // note that fetched handler may be not the one that we have added |
|
592 | 611 | // even we can fetch no handlers at all :) |
|
593 | 612 | InvokeHandler(handler); |
|
594 | 613 | } |
|
595 | 614 | |
|
596 | 615 | protected virtual void InvokeHandler(HandlerDescriptor handler) { |
|
597 | 616 | switch (m_state) { |
|
598 | 617 | case SUCCEEDED_STATE: |
|
599 | 618 | handler.Resolve(m_result); |
|
600 | 619 | break; |
|
601 | 620 | case REJECTED_STATE: |
|
602 | 621 | handler.Reject(m_error); |
|
603 | 622 | break; |
|
604 | 623 | case CANCELLED_STATE: |
|
605 | 624 | handler.Cancel(); |
|
606 | 625 | break; |
|
607 | 626 | default: |
|
608 | 627 | // do nothing |
|
609 | 628 | return; |
|
610 | 629 | } |
|
611 | 630 | } |
|
612 | 631 | |
|
613 | 632 | void OnStateChanged() { |
|
614 | 633 | HandlerDescriptor handler; |
|
615 | 634 | while (m_handlers.TryDequeue(out handler)) |
|
616 | 635 | InvokeHandler(handler); |
|
617 | 636 | } |
|
618 | 637 | |
|
619 | 638 | public bool IsExclusive { |
|
620 | 639 | get { |
|
621 | 640 | return m_childrenCount <= 1; |
|
622 | 641 | } |
|
623 | 642 | } |
|
624 | 643 | |
|
625 | 644 | /// <summary> |
|
626 | 645 | /// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ, ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠΌ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² Π΄ΡΡΠ³ΠΈΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. |
|
627 | 646 | /// ΠΡΠ»ΠΈ Ρ ΠΎΡΡΠ±Ρ ΠΎΠ΄Π½ΠΎ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ, ΡΠΎ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ΅Π½ΠΈΠ΅ ΡΠΎΠΆΠ΅ Π½Π΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ. |
|
628 | 647 | /// ΠΡΠΈ ΠΎΡΠΌΠ΅Π½Π΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ ΡΠ°ΠΊΠΆΠ΅ Π±ΡΠ΄ΡΡ ΠΎΡΠΌΠ΅Π½Π΅Π½Ρ, Π΅ΡΠ»ΠΈ Π½ΠΈΠΊΡΠΎ Π±ΠΎΠ»ΡΡΠ΅ Π½Π° Π½ΠΈΡ Π½Π΅ ΠΏΠΎΠ΄ΠΏΠΈΡΠ°Π½. |
|
629 | 648 | /// </summary> |
|
630 | 649 | /// <param name="promises">Π‘ΠΏΠΈΡΠΎΠΊ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠ»ΠΈ ΡΠΏΠΈΡΠΎΠΊ ΠΏΡΡΡΠΎΠΉ, ΡΠΎ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ ΡΠΆΠ΅ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠΌ.</param> |
|
631 | 650 | /// <returns>ΠΠ±Π΅ΡΠ°Π½ΠΈΠ΅ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ.</returns> |
|
632 | 651 | /// <exception cref="ArgumentNullException"><paramref name="promises"/> Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ null</exception> |
|
633 | 652 | public static IPromise<T[]> CreateComposite(IList<IPromise<T>> promises) { |
|
634 | 653 | if (promises == null) |
|
635 | 654 | throw new ArgumentNullException(); |
|
636 | 655 | |
|
637 | 656 | // ΡΠΎΠ·Π΄Π°Π΅ΠΌ Π°ΠΊΠΊΡΠΌΡΠ»ΡΡΠΎΡ Π΄Π»Ρ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ² ΠΈ ΡΠ΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ |
|
638 | 657 | var result = new T[promises.Count]; |
|
639 | 658 | var promise = new Promise<T[]>(); |
|
640 | 659 | |
|
641 | 660 | // special case |
|
642 | 661 | if (promises.Count == 0) { |
|
643 | 662 | promise.Resolve(result); |
|
644 | 663 | return promise; |
|
645 | 664 | } |
|
646 | 665 | |
|
647 | 666 | int pending = promises.Count; |
|
648 | 667 | |
|
649 | 668 | for (int i = 0; i < promises.Count; i++) { |
|
650 | 669 | var dest = i; |
|
651 | 670 | |
|
652 | 671 | if (promises[i] != null) { |
|
653 | 672 | promises[i].Then( |
|
654 | 673 | x => { |
|
655 | 674 | result[dest] = x; |
|
656 | 675 | if (Interlocked.Decrement(ref pending) == 0) |
|
657 | 676 | promise.Resolve(result); |
|
658 | 677 | }, |
|
659 | 678 | e => { |
|
660 | 679 | promise.Reject(e); |
|
661 | 680 | return default(T); |
|
662 | 681 | } |
|
663 | 682 | ); |
|
664 | 683 | } else { |
|
665 | 684 | if (Interlocked.Decrement(ref pending) == 0) |
|
666 | 685 | promise.Resolve(result); |
|
667 | 686 | } |
|
668 | 687 | } |
|
669 | 688 | |
|
670 | 689 | promise.Cancelled( |
|
671 | 690 | () => { |
|
672 | 691 | foreach (var d in promises) |
|
673 | 692 | if (d != null && d.IsExclusive) |
|
674 | 693 | d.Cancel(); |
|
675 | 694 | } |
|
676 | 695 | ); |
|
677 | 696 | |
|
678 | 697 | return promise; |
|
679 | 698 | } |
|
680 | 699 | |
|
681 | 700 | /// <summary> |
|
682 | 701 | /// ΠΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ Π² ΠΎΠ΄Π½ΠΎ. Π Π΅Π·ΡΠ»ΡΡΠΈΡΡΡΡΠ΅Π΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΎ ΠΏΡΠΈ |
|
683 | 702 | /// Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ Π²ΡΠ΅Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ. ΠΡΠΈ ΡΡΠΎΠΌ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ |
|
684 | 703 | /// ΠΈΠ³Π½ΠΎΡΠΈΡΡΡΡΡΡ. |
|
685 | 704 | /// </summary> |
|
686 | 705 | /// <param name="promises">ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΠΏΠ΅ΡΠ²ΠΈΡΠ½ΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠΉ, ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΡΠ΄ΡΡ ΠΎΠ±ΡΠ΅Π΄Π΅Π½Π΅Π½Ρ Π² ΠΎΠ΄Π½ΠΎ.</param> |
|
687 | 706 | /// <returns>ΠΠΎΠ²ΠΎΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΡΡΠ΅Π΅ Π² ΡΠ΅Π±Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΠ΅.</returns> |
|
688 | 707 | /// <remarks> |
|
689 | 708 | /// ΠΡΠ»ΠΈ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ Π²ΡΡΡΠ΅ΡΠ°ΡΡΡΡ <c>null</c>, ΡΠΎ ΠΎΠ½ΠΈ Π²ΠΎΡΠΏΡΠΈΠ½ΠΈΠΌΠ°ΡΡΡΡ ΠΊΠ°ΠΊ Π²ΡΠΏΠΎΠ»Π½Π΅Π½Π½ΡΠ΅ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ. |
|
690 | 709 | /// </remarks> |
|
691 | 710 | public static IPromise CreateComposite(ICollection<IPromise> promises) { |
|
692 | 711 | if (promises == null) |
|
693 | 712 | throw new ArgumentNullException(); |
|
694 | 713 | if (promises.Count == 0) |
|
695 | 714 | return Promise<object>.ResultToPromise(null); |
|
696 | 715 | |
|
697 | 716 | int countdown = promises.Count; |
|
698 | 717 | |
|
699 | 718 | var result = new Promise<object>(); |
|
700 | 719 | |
|
701 | 720 | foreach (var d in promises) { |
|
702 | 721 | if (d == null) { |
|
703 | 722 | if (Interlocked.Decrement(ref countdown) == 0) |
|
704 | 723 | result.Resolve(null); |
|
705 | 724 | } else { |
|
706 | 725 | d.Then(() => { |
|
707 | 726 | if (Interlocked.Decrement(ref countdown) == 0) |
|
708 | 727 | result.Resolve(null); |
|
709 | 728 | }); |
|
710 | 729 | } |
|
711 | 730 | } |
|
712 | 731 | |
|
713 | 732 | result.Cancelled(() => { |
|
714 | 733 | foreach (var d in promises) |
|
715 | 734 | if (d != null && d.IsExclusive) |
|
716 | 735 | d.Cancel(); |
|
717 | 736 | }); |
|
718 | 737 | |
|
719 | 738 | return result; |
|
720 | 739 | } |
|
721 | 740 | |
|
722 | 741 | public static Promise<T> ResultToPromise(T result) { |
|
723 | 742 | var p = new Promise<T>(); |
|
724 | 743 | p.Resolve(result); |
|
725 | 744 | return p; |
|
726 | 745 | } |
|
727 | 746 | |
|
728 | 747 | public static Promise<T> ExceptionToPromise(Exception error) { |
|
729 | 748 | if (error == null) |
|
730 | 749 | throw new ArgumentNullException(); |
|
731 | 750 | |
|
732 | 751 | var p = new Promise<T>(); |
|
733 | 752 | p.Reject(error); |
|
734 | 753 | return p; |
|
735 | 754 | } |
|
736 | 755 | |
|
737 | 756 | #region IPromiseBase explicit implementation |
|
738 | 757 | |
|
758 | IPromise IPromise.Then(Action success, ErrorHandler error, Action cancel) { | |
|
759 | return Then( | |
|
760 | x => success(), | |
|
761 | e => { | |
|
762 | error(e); | |
|
763 | return default(T); | |
|
764 | }, | |
|
765 | cancel | |
|
766 | ); | |
|
767 | } | |
|
768 | ||
|
769 | IPromise IPromise.Then(Action success, ErrorHandler error) { | |
|
770 | return Then( | |
|
771 | x => success(), | |
|
772 | e => { | |
|
773 | error(e); | |
|
774 | return default(T); | |
|
775 | } | |
|
776 | ); | |
|
777 | } | |
|
778 | ||
|
779 | IPromise IPromise.Then(Action success) { | |
|
780 | return Then(x => success()); | |
|
781 | } | |
|
782 | ||
|
783 | void IPromise.Last(Action success, ErrorHandler error, Action cancel) { | |
|
784 | Last(x => success(), error, cancel); | |
|
785 | } | |
|
786 | ||
|
787 | void IPromise.Last(Action success, ErrorHandler error) { | |
|
788 | Last(x => success(), error, null); | |
|
789 | } | |
|
790 | ||
|
791 | void IPromise.Last(Action success) { | |
|
792 | Last(x => success(), null, null); | |
|
793 | } | |
|
794 | ||
|
739 | 795 | IPromise IPromise.Error(ErrorHandler error) { |
|
740 | 796 | return Error(error); |
|
741 | 797 | } |
|
742 | 798 | |
|
743 |
IPromise IPromise. |
|
|
744 |
return |
|
|
799 | IPromise IPromise.Anyway(Action handler) { | |
|
800 | return Anyway(handler); | |
|
745 | 801 | } |
|
746 | 802 | |
|
747 | 803 | IPromise IPromise.Cancelled(Action handler) { |
|
748 | 804 | return Cancelled(handler); |
|
749 | 805 | } |
|
750 | 806 | |
|
751 | 807 | void IPromise.Join() { |
|
752 | 808 | Join(); |
|
753 | 809 | } |
|
754 | 810 | |
|
755 | 811 | void IPromise.Join(int timeout) { |
|
756 | 812 | Join(timeout); |
|
757 | 813 | } |
|
758 | 814 | |
|
759 | 815 | #endregion |
|
760 | 816 | |
|
761 | 817 | |
|
762 | 818 | |
|
763 | 819 | } |
|
764 | 820 | } |
@@ -1,69 +1,65 | |||
|
1 | 1 | using System.Threading; |
|
2 | 2 | using System; |
|
3 | 3 | #if NET_4_5 |
|
4 | 4 | using System.Threading.Tasks; |
|
5 | 5 | #endif |
|
6 | 6 | |
|
7 | 7 | namespace Implab { |
|
8 | 8 | public static class PromiseExtensions { |
|
9 | 9 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { |
|
10 | 10 | Safe.ArgumentNotNull(that, "that"); |
|
11 | 11 | var context = SynchronizationContext.Current; |
|
12 | 12 | if (context == null) |
|
13 | 13 | return that; |
|
14 | 14 | |
|
15 | 15 | var p = new SyncContextPromise<T>(context, that, true); |
|
16 | 16 | |
|
17 |
that. |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 | return default(T); | |
|
22 | } | |
|
17 | that.Last( | |
|
18 | p.Resolve, | |
|
19 | p.Reject, | |
|
20 | p.Cancel | |
|
23 | 21 | ); |
|
24 | 22 | return p; |
|
25 | 23 | } |
|
26 | 24 | |
|
27 | 25 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { |
|
28 | 26 | Safe.ArgumentNotNull(that, "that"); |
|
29 | 27 | Safe.ArgumentNotNull(context, "context"); |
|
30 | 28 | |
|
31 | 29 | var p = new SyncContextPromise<T>(context, that, true); |
|
32 | 30 | |
|
33 |
that. |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
|
37 | return default(T); | |
|
38 | } | |
|
31 | that.Last( | |
|
32 | p.Resolve, | |
|
33 | p.Reject, | |
|
34 | p.Cancel | |
|
39 | 35 | ); |
|
40 | 36 | return p; |
|
41 | 37 | } |
|
42 | 38 | |
|
43 | 39 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { |
|
44 | 40 | Safe.ArgumentNotNull(that, "that"); |
|
45 | 41 | Safe.ArgumentNotNull(callback, "callback"); |
|
46 | 42 | return ar => { |
|
47 | 43 | try { |
|
48 | 44 | that.Resolve(callback(ar)); |
|
49 | 45 | } catch (Exception err) { |
|
50 | 46 | that.Reject(err); |
|
51 | 47 | } |
|
52 | 48 | }; |
|
53 | 49 | } |
|
54 | 50 | |
|
55 | 51 | #if NET_4_5 |
|
56 | 52 | |
|
57 | 53 | public static Task<T> GetTask<T>(this IPromise<T> that) { |
|
58 | 54 | Safe.ArgumentNotNull(that, "that"); |
|
59 | 55 | var tcs = new TaskCompletionSource<T>(); |
|
60 | 56 | |
|
61 | 57 | that.Last(tcs.SetResult, tcs.SetException, tcs.SetCanceled); |
|
62 | 58 | |
|
63 | 59 | return tcs.Task; |
|
64 | 60 | } |
|
65 | 61 | |
|
66 | 62 | #endif |
|
67 | 63 | } |
|
68 | 64 | } |
|
69 | 65 |
@@ -1,27 +1,27 | |||
|
1 | 1 | using System.Reflection; |
|
2 | 2 | using System.Runtime.CompilerServices; |
|
3 | 3 | using System.Runtime.InteropServices; |
|
4 | 4 | |
|
5 | 5 | // Information about this assembly is defined by the following attributes. |
|
6 | 6 | // Change them to the values specific to your project. |
|
7 | 7 | |
|
8 | 8 | [assembly: AssemblyTitle("Implab")] |
|
9 | 9 | [assembly: AssemblyDescription("Tools")] |
|
10 | 10 | [assembly: AssemblyConfiguration("")] |
|
11 | 11 | [assembly: AssemblyCompany("")] |
|
12 | 12 | [assembly: AssemblyProduct("")] |
|
13 | 13 | [assembly: AssemblyCopyright("Implab")] |
|
14 | 14 | [assembly: AssemblyTrademark("")] |
|
15 | 15 | // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". |
|
16 | 16 | // The form "{Major}.{Minor}.*" will automatically update the build and revision, |
|
17 | 17 | // and "{Major}.{Minor}.{Build}.*" will update just the revision. |
|
18 | 18 | |
|
19 |
[assembly: AssemblyVersion(" |
|
|
19 | [assembly: AssemblyVersion("2.0.*")] | |
|
20 | 20 | [assembly: ComVisible(false)] |
|
21 | 21 | |
|
22 | 22 | // The following attributes are used to specify the signing key for the assembly, |
|
23 | 23 | // if desired. See the Mono documentation for more information about signing. |
|
24 | 24 | |
|
25 | 25 | //[assembly: AssemblyDelaySign(false)] |
|
26 | 26 | //[assembly: AssemblyKeyFile("")] |
|
27 | 27 |
@@ -1,140 +1,136 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Threading; |
|
6 | 6 | |
|
7 | 7 | namespace Implab |
|
8 | 8 | { |
|
9 | 9 | /// <summary> |
|
10 | 10 | /// This class allows to interact with asyncronuos task. |
|
11 | 11 | /// </summary> |
|
12 | 12 | /// <remarks> |
|
13 | 13 | /// Members of this object are thread safe. |
|
14 | 14 | /// </remarks> |
|
15 | 15 | public class TaskController: IProgressNotifier, ITaskController |
|
16 | 16 | { |
|
17 | 17 | readonly object m_lock; |
|
18 | 18 | string m_message; |
|
19 | 19 | |
|
20 | 20 | float m_current; |
|
21 | 21 | float m_max; |
|
22 | 22 | |
|
23 | 23 | bool m_cancelled; |
|
24 | 24 | |
|
25 | 25 | public event EventHandler Cancelled; |
|
26 | 26 | public event EventHandler<ValueEventArgs<string>> MessageUpdated; |
|
27 | 27 | public event EventHandler<ValueEventArgs<float>> ProgressUpdated; |
|
28 | 28 | public event EventHandler<ProgressInitEventArgs> ProgressInit; |
|
29 | 29 | |
|
30 | 30 | public TaskController() |
|
31 | 31 | { |
|
32 | 32 | m_lock = new Object(); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | public string Message |
|
36 | 36 | { |
|
37 | 37 | get |
|
38 | 38 | { |
|
39 | 39 | lock (m_lock) |
|
40 | 40 | return m_message; |
|
41 | 41 | } |
|
42 | 42 | set |
|
43 | 43 | { |
|
44 | 44 | lock (m_lock) |
|
45 | 45 | { |
|
46 | 46 | m_message = value; |
|
47 | 47 | OnMessageUpdated(); |
|
48 | 48 | } |
|
49 | 49 | } |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | public float CurrentProgress |
|
53 | 53 | { |
|
54 | 54 | get |
|
55 | 55 | { |
|
56 | 56 | lock (m_lock) |
|
57 | 57 | return m_current; |
|
58 | 58 | } |
|
59 | 59 | set |
|
60 | 60 | { |
|
61 | 61 | lock (m_lock) |
|
62 | 62 | { |
|
63 | 63 | var prev = m_current; |
|
64 | 64 | m_current = value; |
|
65 | 65 | if (m_current >= m_max) |
|
66 | 66 | m_current = m_max; |
|
67 | 67 | if (m_current != prev) |
|
68 | 68 | OnProgressUpdated(); |
|
69 | 69 | } |
|
70 | 70 | } |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | public void InitProgress(float current, float max, string message) |
|
74 | 74 | { |
|
75 | 75 | if (max < 0) |
|
76 | 76 | throw new ArgumentOutOfRangeException("max"); |
|
77 | 77 | if (current < 0 || current > max) |
|
78 | 78 | throw new ArgumentOutOfRangeException("current"); |
|
79 | 79 | |
|
80 | 80 | lock(m_lock) { |
|
81 | 81 | m_current = current; |
|
82 | 82 | m_max = max; |
|
83 | 83 | m_message = message; |
|
84 | 84 | OnProgressInit(); |
|
85 | 85 | } |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | public bool IsCancelled { |
|
89 | 89 | get { |
|
90 | 90 | lock (m_lock) |
|
91 | 91 | return m_cancelled; |
|
92 | 92 | } |
|
93 | 93 | } |
|
94 | 94 | |
|
95 |
public |
|
|
95 | public void Cancel() { | |
|
96 | 96 | lock (m_lock) { |
|
97 |
if (!m_cancelled) |
|
|
97 | if (!m_cancelled) | |
|
98 | 98 | m_cancelled = true; |
|
99 | return true; | |
|
100 | } else { | |
|
101 | return false; | |
|
102 | } | |
|
103 | 99 | } |
|
104 | 100 | } |
|
105 | 101 | |
|
106 | 102 | protected virtual void OnCancelled() { |
|
107 | 103 | var temp = Cancelled; |
|
108 | 104 | if (temp != null) { |
|
109 | 105 | temp(this,new EventArgs()); |
|
110 | 106 | } |
|
111 | 107 | } |
|
112 | 108 | |
|
113 | 109 | protected virtual void OnMessageUpdated() |
|
114 | 110 | { |
|
115 | 111 | var temp = MessageUpdated; |
|
116 | 112 | if (temp != null) |
|
117 | 113 | { |
|
118 | 114 | temp(this, new ValueEventArgs<string>(m_message)); |
|
119 | 115 | } |
|
120 | 116 | } |
|
121 | 117 | |
|
122 | 118 | protected virtual void OnProgressUpdated() |
|
123 | 119 | { |
|
124 | 120 | var temp = ProgressUpdated; |
|
125 | 121 | if (temp != null) |
|
126 | 122 | { |
|
127 | 123 | temp(this,new ValueEventArgs<float>(m_current)); |
|
128 | 124 | } |
|
129 | 125 | } |
|
130 | 126 | |
|
131 | 127 | protected virtual void OnProgressInit() |
|
132 | 128 | { |
|
133 | 129 | var temp = ProgressInit; |
|
134 | 130 | if (temp != null) |
|
135 | 131 | { |
|
136 | 132 | temp(this, new ProgressInitEventArgs(m_current,m_max, m_message)); |
|
137 | 133 | } |
|
138 | 134 | } |
|
139 | 135 | } |
|
140 | 136 | } |
General Comments 0
You need to be logged in to leave comments.
Login now