| @@ -1,95 +1,95 | |||||
| 1 | using System; |
|
1 | using System; | |
| 2 | using System.Collections.Generic; |
|
2 | using System.Collections.Generic; | |
| 3 | using System.Linq; |
|
3 | using System.Linq; | |
| 4 | using System.Text; |
|
4 | using System.Text; | |
| 5 | using System.Windows.Forms; |
|
5 | using System.Windows.Forms; | |
| 6 | using System.Threading; |
|
6 | using System.Threading; | |
| 7 |
|
7 | |||
| 8 | namespace Implab.Fx |
|
8 | namespace Implab.Fx | |
| 9 | { |
|
9 | { | |
| 10 | public static class PromiseHelpers |
|
10 | public static class PromiseHelpers | |
| 11 | { |
|
11 | { | |
| 12 | /// <summary> |
|
12 | /// <summary> | |
| 13 | /// Перенаправляет обработку обещания в поток указанного элемента управления. |
|
13 | /// Перенаправляет обработку обещания в поток указанного элемента управления. | |
| 14 | /// </summary> |
|
14 | /// </summary> | |
| 15 | /// <typeparam name="T">Тип результата обещания</typeparam> |
|
15 | /// <typeparam name="T">Тип результата обещания</typeparam> | |
| 16 | /// <param name="that">Исходное обещание</param> |
|
16 | /// <param name="that">Исходное обещание</param> | |
| 17 | /// <param name="ctl">Элемент управления</param> |
|
17 | /// <param name="ctl">Элемент управления</param> | |
| 18 | /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> |
|
18 | /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> | |
| 19 | /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> |
|
19 | /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> | |
| 20 | /// <example> |
|
20 | /// <example> | |
| 21 | /// client |
|
21 | /// client | |
| 22 | /// .Get("description.txt") // returns a promise |
|
22 | /// .Get("description.txt") // returns a promise | |
| 23 | /// .DirectToControl(m_ctl) // handle the promise in the thread of the control |
|
23 | /// .DirectToControl(m_ctl) // handle the promise in the thread of the control | |
| 24 | /// .Then( |
|
24 | /// .Then( | |
| 25 | /// description => m_ctl.Text = description // now it's safe |
|
25 | /// description => m_ctl.Text = description // now it's safe | |
| 26 | /// ) |
|
26 | /// ) | |
| 27 | /// </example> |
|
27 | /// </example> | |
| 28 | public static Promise<T> DispatchToControl<T>(this Promise<T> that, Control ctl) |
|
28 | public static Promise<T> DispatchToControl<T>(this IPromise<T> that, Control ctl) | |
| 29 | { |
|
29 | { | |
| 30 | if (that == null) |
|
30 | if (that == null) | |
| 31 | throw new ArgumentNullException("that"); |
|
31 | throw new ArgumentNullException("that"); | |
| 32 | if (ctl == null) |
|
32 | if (ctl == null) | |
| 33 | throw new ArgumentNullException("ctl"); |
|
33 | throw new ArgumentNullException("ctl"); | |
| 34 |
|
34 | |||
| 35 | var directed = new Promise<T>(); |
|
35 | var directed = new Promise<T>(); | |
| 36 |
|
36 | |||
| 37 | that.Then( |
|
37 | that.Then( | |
| 38 | res => |
|
38 | res => | |
| 39 | { |
|
39 | { | |
| 40 | if (ctl.InvokeRequired) |
|
40 | if (ctl.InvokeRequired) | |
| 41 | ctl.Invoke(new Action<T>(directed.Resolve), res); |
|
41 | ctl.Invoke(new Action<T>(directed.Resolve), res); | |
| 42 | else |
|
42 | else | |
| 43 | directed.Resolve(res); |
|
43 | directed.Resolve(res); | |
| 44 | }, |
|
44 | }, | |
| 45 | err => |
|
45 | err => | |
| 46 | { |
|
46 | { | |
| 47 | if (ctl.InvokeRequired) |
|
47 | if (ctl.InvokeRequired) | |
| 48 | ctl.Invoke(new Action<Exception>(directed.Reject), err); |
|
48 | ctl.Invoke(new Action<Exception>(directed.Reject), err); | |
| 49 | else |
|
49 | else | |
| 50 | directed.Reject(err); |
|
50 | directed.Reject(err); | |
| 51 | } |
|
51 | } | |
| 52 | ); |
|
52 | ); | |
| 53 |
|
53 | |||
| 54 | return directed; |
|
54 | return directed; | |
| 55 | } |
|
55 | } | |
| 56 |
|
56 | |||
| 57 | /// <summary> |
|
57 | /// <summary> | |
| 58 | /// Направляет обработку обещания в текущий поток, если у него существует контекст синхронизации. |
|
58 | /// Направляет обработку обещания в текущий поток, если у него существует контекст синхронизации. | |
| 59 | /// </summary> |
|
59 | /// </summary> | |
| 60 | /// <typeparam name="T">Тип результата обещания.</typeparam> |
|
60 | /// <typeparam name="T">Тип результата обещания.</typeparam> | |
| 61 | /// <param name="that">Обещание которое нужно обработать в текущем потоке.</param> |
|
61 | /// <param name="that">Обещание которое нужно обработать в текущем потоке.</param> | |
| 62 | /// <returns>Перенаправленное обещание.</returns> |
|
62 | /// <returns>Перенаправленное обещание.</returns> | |
| 63 | public static Promise<T> DispatchToCurrentThread<T>(this Promise<T> that) |
|
63 | public static Promise<T> DispatchToCurrentThread<T>(this Promise<T> that) | |
| 64 | { |
|
64 | { | |
| 65 | var sync = SynchronizationContext.Current; |
|
65 | var sync = SynchronizationContext.Current; | |
| 66 | if (sync == null) |
|
66 | if (sync == null) | |
| 67 | throw new InvalidOperationException("The current thread doesn't have a syncronization context"); |
|
67 | throw new InvalidOperationException("The current thread doesn't have a syncronization context"); | |
| 68 | return DispatchToSyncContext(that, sync); |
|
68 | return DispatchToSyncContext(that, sync); | |
| 69 | } |
|
69 | } | |
| 70 |
|
70 | |||
| 71 | /// <summary> |
|
71 | /// <summary> | |
| 72 | /// Направляет обработку обещания в указанный контекст синхронизации. |
|
72 | /// Направляет обработку обещания в указанный контекст синхронизации. | |
| 73 | /// </summary> |
|
73 | /// </summary> | |
| 74 | /// <typeparam name="T">Тип результата обещания.</typeparam> |
|
74 | /// <typeparam name="T">Тип результата обещания.</typeparam> | |
| 75 | /// <param name="that">Обещание, которое требуется обработать в указанном контексте синхронизации.</param> |
|
75 | /// <param name="that">Обещание, которое требуется обработать в указанном контексте синхронизации.</param> | |
| 76 | /// <param name="sync">Контекст синхронизации в который будет направлено обещание.</param> |
|
76 | /// <param name="sync">Контекст синхронизации в который будет направлено обещание.</param> | |
| 77 | /// <returns>Новое обещание, которое будет обрабатываться в указанном контексте.</returns> |
|
77 | /// <returns>Новое обещание, которое будет обрабатываться в указанном контексте.</returns> | |
| 78 | public static Promise<T> DispatchToSyncContext<T>(this Promise<T> that, SynchronizationContext sync) |
|
78 | public static Promise<T> DispatchToSyncContext<T>(this Promise<T> that, SynchronizationContext sync) | |
| 79 | { |
|
79 | { | |
| 80 | if (that == null) |
|
80 | if (that == null) | |
| 81 | throw new ArgumentNullException("that"); |
|
81 | throw new ArgumentNullException("that"); | |
| 82 | if (sync == null) |
|
82 | if (sync == null) | |
| 83 | throw new ArgumentNullException("sync"); |
|
83 | throw new ArgumentNullException("sync"); | |
| 84 |
|
84 | |||
| 85 | var d = new Promise<T>(); |
|
85 | var d = new Promise<T>(); | |
| 86 |
|
86 | |||
| 87 | that.Then( |
|
87 | that.Then( | |
| 88 | res => sync.Post(state => d.Resolve(res), null), |
|
88 | res => sync.Post(state => d.Resolve(res), null), | |
| 89 | err => sync.Post(state => d.Reject(err), null) |
|
89 | err => sync.Post(state => d.Reject(err), null) | |
| 90 | ); |
|
90 | ); | |
| 91 |
|
91 | |||
| 92 | return d; |
|
92 | return d; | |
| 93 | } |
|
93 | } | |
| 94 | } |
|
94 | } | |
| 95 | } |
|
95 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
