72
|
1 using System.Threading;
|
75
|
2 using System;
|
109
|
3 using Implab.Diagnostics;
|
|
4
|
|
5
|
75
|
6 #if NET_4_5
|
|
7 using System.Threading.Tasks;
|
|
8 #endif
|
72
|
9
|
|
10 namespace Implab {
|
|
11 public static class PromiseExtensions {
|
|
12 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
|
75
|
13 Safe.ArgumentNotNull(that, "that");
|
72
|
14 var context = SynchronizationContext.Current;
|
|
15 if (context == null)
|
|
16 return that;
|
|
17
|
101
|
18 var p = new SyncContextPromise<T>(context, that);
|
72
|
19
|
104
|
20 that.On(
|
76
|
21 p.Resolve,
|
|
22 p.Reject,
|
|
23 p.Cancel
|
72
|
24 );
|
|
25 return p;
|
|
26 }
|
|
27
|
|
28 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
|
75
|
29 Safe.ArgumentNotNull(that, "that");
|
72
|
30 Safe.ArgumentNotNull(context, "context");
|
|
31
|
101
|
32 var p = new SyncContextPromise<T>(context, that);
|
72
|
33
|
104
|
34 that.On(
|
76
|
35 p.Resolve,
|
|
36 p.Reject,
|
|
37 p.Cancel
|
72
|
38 );
|
|
39 return p;
|
|
40 }
|
75
|
41
|
101
|
42 /// <summary>
|
|
43 /// Ensures the dispatched.
|
|
44 /// </summary>
|
|
45 /// <returns>The dispatched.</returns>
|
|
46 /// <param name="that">That.</param>
|
|
47 /// <param name="head">Head.</param>
|
|
48 /// <param name="cleanup">Cleanup.</param>
|
|
49 /// <typeparam name="TPromise">The 1st 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{
|
|
52 Safe.ArgumentNotNull(that, "that");
|
|
53 Safe.ArgumentNotNull(head, "head");
|
|
54
|
104
|
55 that.On(null,null,() => head.On(cleanup));
|
101
|
56
|
|
57 return that;
|
|
58 }
|
|
59
|
75
|
60 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
|
|
61 Safe.ArgumentNotNull(that, "that");
|
|
62 Safe.ArgumentNotNull(callback, "callback");
|
109
|
63 var op = TraceContext.Instance.CurrentOperation;
|
75
|
64 return ar => {
|
109
|
65 TraceContext.Instance.EnterLogicalOperation(op,false);
|
75
|
66 try {
|
|
67 that.Resolve(callback(ar));
|
|
68 } catch (Exception err) {
|
|
69 that.Reject(err);
|
109
|
70 } finally {
|
|
71 TraceContext.Instance.Leave();
|
75
|
72 }
|
|
73 };
|
|
74 }
|
|
75
|
|
76 #if NET_4_5
|
|
77
|
|
78 public static Task<T> GetTask<T>(this IPromise<T> that) {
|
|
79 Safe.ArgumentNotNull(that, "that");
|
|
80 var tcs = new TaskCompletionSource<T>();
|
|
81
|
104
|
82 that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
|
75
|
83
|
|
84 return tcs.Task;
|
|
85 }
|
|
86
|
|
87 #endif
|
72
|
88 }
|
|
89 }
|
|
90
|