annotate Implab.Fx/PromiseHelpers.cs @ 74:c4140283575c v2

minor fixes
author cin
date Mon, 08 Sep 2014 17:40:46 +0400
parents d67b95eddaf4
children c761fc982e1d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
1 using System;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
2 using System.Windows.Forms;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
3 using System.Threading;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
5 namespace Implab.Fx
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
6 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
7 public static class PromiseHelpers
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
8 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
9 /// <summary>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
10 /// Перенаправляет обработку обещания в поток указанного элемента управления.
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
11 /// </summary>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
12 /// <typeparam name="T">Тип результата обещания</typeparam>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
13 /// <param name="that">Исходное обещание</param>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
14 /// <param name="ctl">Элемент управления</param>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
15 /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
16 /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
17 /// <example>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
18 /// client
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
19 /// .Get("description.txt") // returns a promise
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
20 /// .DirectToControl(m_ctl) // handle the promise in the thread of the control
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
21 /// .Then(
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
22 /// description => m_ctl.Text = description // now it's safe
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
23 /// )
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
24 /// </example>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
25 public static Promise<T> DispatchToControl<T>(this Promise<T> that, Control ctl)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
26 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
27 if (that == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
28 throw new ArgumentNullException("that");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
29 if (ctl == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
30 throw new ArgumentNullException("ctl");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
31
72
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
32 var directed = new ControlBoundPromise<T>(ctl,that,true);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
33
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
34 that.Then(
72
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
35 directed.Resolve,
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
36 err =>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
37 {
72
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
38 directed.Reject(err);
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
39 return default(T);
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
40 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
41 );
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
42
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
43 return directed;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
44 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
45
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
46 /// <summary>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
47 /// Направляет обработку обещания в текущий поток, если у него существует контекст синхронизации.
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
48 /// </summary>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
49 /// <typeparam name="T">Тип результата обещания.</typeparam>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
50 /// <param name="that">Обещание которое нужно обработать в текущем потоке.</param>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
51 /// <returns>Перенаправленное обещание.</returns>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
52 public static Promise<T> DispatchToCurrentThread<T>(this Promise<T> that)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
53 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
54 var sync = SynchronizationContext.Current;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
55 if (sync == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
56 throw new InvalidOperationException("The current thread doesn't have a syncronization context");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
57 return DispatchToSyncContext(that, sync);
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
58 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
59
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
60 /// <summary>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
61 /// Направляет обработку обещания в указанный контекст синхронизации.
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
62 /// </summary>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
63 /// <typeparam name="T">Тип результата обещания.</typeparam>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
64 /// <param name="that">Обещание, которое требуется обработать в указанном контексте синхронизации.</param>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
65 /// <param name="sync">Контекст синхронизации в который будет направлено обещание.</param>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
66 /// <returns>Новое обещание, которое будет обрабатываться в указанном контексте.</returns>
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
67 public static Promise<T> DispatchToSyncContext<T>(this Promise<T> that, SynchronizationContext sync)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
68 {
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
69 if (that == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
70 throw new ArgumentNullException("that");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
71 if (sync == null)
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
72 throw new ArgumentNullException("sync");
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
73
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
74 var d = new Promise<T>();
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
75
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
76 that.Then(
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
77 res => sync.Post(state => d.Resolve(res), null),
72
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
78 err => {
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
79 sync.Post(state => d.Reject(err), null);
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
80 return default(T);
d67b95eddaf4 promises refactoring
cin
parents: 4
diff changeset
81 }
4
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
82 );
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
83
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
84 return d;
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
85 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
86 }
381095ad0a69 Implab.Fx: implemented animation object
cin
parents:
diff changeset
87 }