Mercurial > pub > ImplabNet
comparison Implab.Fx/PromiseHelpers.cs @ 76:c761fc982e1d v2
Refactoring of the IPromise<T> interface
Added tests
| author | cin |
|---|---|
| date | Wed, 10 Sep 2014 17:53:05 +0400 |
| parents | d67b95eddaf4 |
| children | b4c4d65b7def |
comparison
equal
deleted
inserted
replaced
| 75:4439140706d0 | 76:c761fc982e1d |
|---|---|
| 15 /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> | 15 /// <returns>Новое обещание, обработчики которого будут выполнены в потоке элемента управления.</returns> |
| 16 /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> | 16 /// <exception cref="ArgumentNullException">Параметр не может быть <c>null</c>.</exception> |
| 17 /// <example> | 17 /// <example> |
| 18 /// client | 18 /// client |
| 19 /// .Get("description.txt") // returns a promise | 19 /// .Get("description.txt") // returns a promise |
| 20 /// .DirectToControl(m_ctl) // handle the promise in the thread of the control | 20 /// .DispatchToControl(m_ctl) // handle the promise in the thread of the control |
| 21 /// .Then( | 21 /// .Then( |
| 22 /// description => m_ctl.Text = description // now it's safe | 22 /// description => m_ctl.Text = description // now it's safe |
| 23 /// ) | 23 /// ) |
| 24 /// </example> | 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) | 27 Safe.ArgumentNotNull(that, "that"); |
| 28 throw new ArgumentNullException("that"); | 28 Safe.ArgumentNotNull(ctl, "ctl"); |
| 29 if (ctl == null) | |
| 30 throw new ArgumentNullException("ctl"); | |
| 31 | 29 |
| 32 var directed = new ControlBoundPromise<T>(ctl,that,true); | 30 var directed = new ControlBoundPromise<T>(ctl,that,true); |
| 33 | 31 |
| 34 that.Then( | 32 that.Last( |
| 35 directed.Resolve, | 33 directed.Resolve, |
| 36 err => | 34 directed.Reject, |
| 37 { | 35 directed.Cancel |
| 38 directed.Reject(err); | |
| 39 return default(T); | |
| 40 } | |
| 41 ); | 36 ); |
| 42 | 37 |
| 43 return directed; | 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 } |
