@@ -1,90 +1,107 | |||||
1 | using System.Threading; |
|
1 | using System.Threading; | |
2 | using System; |
|
2 | using System; | |
3 | using Implab.Diagnostics; |
|
3 | using Implab.Diagnostics; | |
4 |
|
4 | |||
5 |
|
5 | |||
6 | #if NET_4_5 |
|
6 | #if NET_4_5 | |
7 | using System.Threading.Tasks; |
|
7 | using System.Threading.Tasks; | |
8 | #endif |
|
8 | #endif | |
9 |
|
9 | |||
10 | namespace Implab { |
|
10 | namespace Implab { | |
11 | public static class PromiseExtensions { |
|
11 | public static class PromiseExtensions { | |
12 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { |
|
12 | public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { | |
13 | Safe.ArgumentNotNull(that, "that"); |
|
13 | Safe.ArgumentNotNull(that, "that"); | |
14 | var context = SynchronizationContext.Current; |
|
14 | var context = SynchronizationContext.Current; | |
15 | if (context == null) |
|
15 | if (context == null) | |
16 | return that; |
|
16 | return that; | |
17 |
|
17 | |||
18 | var p = new SyncContextPromise<T>(context, that); |
|
18 | var p = new SyncContextPromise<T>(context, that); | |
19 |
|
19 | |||
20 | that.On( |
|
20 | that.On( | |
21 | p.Resolve, |
|
21 | p.Resolve, | |
22 | p.Reject, |
|
22 | p.Reject, | |
23 | p.Cancel |
|
23 | p.Cancel | |
24 | ); |
|
24 | ); | |
25 | return p; |
|
25 | return p; | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { |
|
28 | public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | |
29 | Safe.ArgumentNotNull(that, "that"); |
|
29 | Safe.ArgumentNotNull(that, "that"); | |
30 | Safe.ArgumentNotNull(context, "context"); |
|
30 | Safe.ArgumentNotNull(context, "context"); | |
31 |
|
31 | |||
32 | var p = new SyncContextPromise<T>(context, that); |
|
32 | var p = new SyncContextPromise<T>(context, that); | |
33 |
|
33 | |||
34 | that.On( |
|
34 | that.On( | |
35 | p.Resolve, |
|
35 | p.Resolve, | |
36 | p.Reject, |
|
36 | p.Reject, | |
37 | p.Cancel |
|
37 | p.Cancel | |
38 | ); |
|
38 | ); | |
39 | return p; |
|
39 | return p; | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | /// <summary> |
|
42 | /// <summary> | |
43 | /// Ensures the dispatched. |
|
43 | /// Ensures the dispatched. | |
44 | /// </summary> |
|
44 | /// </summary> | |
45 | /// <returns>The dispatched.</returns> |
|
45 | /// <returns>The dispatched.</returns> | |
46 | /// <param name="that">That.</param> |
|
46 | /// <param name="that">That.</param> | |
47 | /// <param name="head">Head.</param> |
|
47 | /// <param name="head">Head.</param> | |
48 | /// <param name="cleanup">Cleanup.</param> |
|
48 | /// <param name="cleanup">Cleanup.</param> | |
49 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> |
|
49 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | |
50 | /// <typeparam name="T">The 2nd type parameter.</typeparam> |
|
50 | /// <typeparam name="T">The 2nd type parameter.</typeparam> | |
51 | public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ |
|
51 | public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ | |
52 | Safe.ArgumentNotNull(that, "that"); |
|
52 | Safe.ArgumentNotNull(that, "that"); | |
53 | Safe.ArgumentNotNull(head, "head"); |
|
53 | Safe.ArgumentNotNull(head, "head"); | |
54 |
|
54 | |||
55 | that.On(null,null,() => head.On(cleanup)); |
|
55 | that.On(null,null,() => head.On(cleanup)); | |
56 |
|
56 | |||
57 | return that; |
|
57 | return that; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { |
|
60 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { | |
61 | Safe.ArgumentNotNull(that, "that"); |
|
61 | Safe.ArgumentNotNull(that, "that"); | |
62 | Safe.ArgumentNotNull(callback, "callback"); |
|
62 | Safe.ArgumentNotNull(callback, "callback"); | |
63 | var op = TraceContext.Instance.CurrentOperation; |
|
63 | var op = TraceContext.Instance.CurrentOperation; | |
64 | return ar => { |
|
64 | return ar => { | |
65 | TraceContext.Instance.EnterLogicalOperation(op,false); |
|
65 | TraceContext.Instance.EnterLogicalOperation(op,false); | |
66 | try { |
|
66 | try { | |
67 | that.Resolve(callback(ar)); |
|
67 | that.Resolve(callback(ar)); | |
68 | } catch (Exception err) { |
|
68 | } catch (Exception err) { | |
69 | that.Reject(err); |
|
69 | that.Reject(err); | |
70 | } finally { |
|
70 | } finally { | |
71 | TraceContext.Instance.Leave(); |
|
71 | TraceContext.Instance.Leave(); | |
72 | } |
|
72 | } | |
73 | }; |
|
73 | }; | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
|
76 | static void CancelCallback(object cookie) { | |||
|
77 | ((ICancellable)cookie).Cancel(); | |||
|
78 | } | |||
|
79 | ||||
|
80 | /// <summary> | |||
|
81 | /// Cancells promise after the specified timeout is elapsed. | |||
|
82 | /// </summary> | |||
|
83 | /// <param name="that">The promise to cancel on timeout.</param> | |||
|
84 | /// <param name="milliseconds">The timeout in milliseconds.</param> | |||
|
85 | /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | |||
|
86 | public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise { | |||
|
87 | Safe.ArgumentNotNull(that, "that"); | |||
|
88 | var timer = new Timer(CancelCallback, that, milliseconds, -1); | |||
|
89 | that.On(timer.Dispose, PromiseEventType.All); | |||
|
90 | return that; | |||
|
91 | } | |||
|
92 | ||||
76 | #if NET_4_5 |
|
93 | #if NET_4_5 | |
77 |
|
94 | |||
78 | public static Task<T> GetTask<T>(this IPromise<T> that) { |
|
95 | public static Task<T> GetTask<T>(this IPromise<T> that) { | |
79 | Safe.ArgumentNotNull(that, "that"); |
|
96 | Safe.ArgumentNotNull(that, "that"); | |
80 | var tcs = new TaskCompletionSource<T>(); |
|
97 | var tcs = new TaskCompletionSource<T>(); | |
81 |
|
98 | |||
82 | that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); |
|
99 | that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); | |
83 |
|
100 | |||
84 | return tcs.Task; |
|
101 | return tcs.Task; | |
85 | } |
|
102 | } | |
86 |
|
103 | |||
87 | #endif |
|
104 | #endif | |
88 | } |
|
105 | } | |
89 | } |
|
106 | } | |
90 |
|
107 |
General Comments 0
You need to be logged in to leave comments.
Login now