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