Mercurial > pub > ImplabNet
annotate Implab/PromiseExtensions.cs @ 129:471f596b2603 v2
Added SharedLock to synchronization routines
author | cin |
---|---|
date | Thu, 29 Jan 2015 18:31:06 +0300 |
parents | a336cb13c6a9 |
children | e9e7940c7d98 |
rev | line source |
---|---|
72 | 1 using System.Threading; |
75 | 2 using System; |
109 | 3 using Implab.Diagnostics; |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
4 using System.Collections.Generic; |
109 | 5 |
6 | |
75 | 7 #if NET_4_5 |
8 using System.Threading.Tasks; | |
9 #endif | |
72 | 10 |
11 namespace Implab { | |
12 public static class PromiseExtensions { | |
13 public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { | |
75 | 14 Safe.ArgumentNotNull(that, "that"); |
72 | 15 var context = SynchronizationContext.Current; |
16 if (context == null) | |
17 return that; | |
18 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
19 var p = new SyncContextPromise<T>(context); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
20 p.On(that.Cancel, PromiseEventType.Cancelled); |
72 | 21 |
104 | 22 that.On( |
76 | 23 p.Resolve, |
24 p.Reject, | |
25 p.Cancel | |
72 | 26 ); |
27 return p; | |
28 } | |
29 | |
30 public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | |
75 | 31 Safe.ArgumentNotNull(that, "that"); |
72 | 32 Safe.ArgumentNotNull(context, "context"); |
33 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
34 var p = new SyncContextPromise<T>(context); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
35 p.On(that.Cancel, PromiseEventType.Cancelled); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
36 |
72 | 37 |
104 | 38 that.On( |
76 | 39 p.Resolve, |
40 p.Reject, | |
41 p.Cancel | |
72 | 42 ); |
43 return p; | |
44 } | |
75 | 45 |
101 | 46 /// <summary> |
47 /// Ensures the dispatched. | |
48 /// </summary> | |
49 /// <returns>The dispatched.</returns> | |
50 /// <param name="that">That.</param> | |
51 /// <param name="head">Head.</param> | |
52 /// <param name="cleanup">Cleanup.</param> | |
53 /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | |
54 /// <typeparam name="T">The 2nd type parameter.</typeparam> | |
55 public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ | |
56 Safe.ArgumentNotNull(that, "that"); | |
57 Safe.ArgumentNotNull(head, "head"); | |
58 | |
104 | 59 that.On(null,null,() => head.On(cleanup)); |
101 | 60 |
61 return that; | |
62 } | |
63 | |
75 | 64 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { |
65 Safe.ArgumentNotNull(that, "that"); | |
66 Safe.ArgumentNotNull(callback, "callback"); | |
109 | 67 var op = TraceContext.Instance.CurrentOperation; |
75 | 68 return ar => { |
109 | 69 TraceContext.Instance.EnterLogicalOperation(op,false); |
75 | 70 try { |
71 that.Resolve(callback(ar)); | |
72 } catch (Exception err) { | |
73 that.Reject(err); | |
109 | 74 } finally { |
75 TraceContext.Instance.Leave(); | |
75 | 76 } |
77 }; | |
78 } | |
110 | 79 |
80 static void CancelCallback(object cookie) { | |
81 ((ICancellable)cookie).Cancel(); | |
82 } | |
83 | |
84 /// <summary> | |
85 /// Cancells promise after the specified timeout is elapsed. | |
86 /// </summary> | |
87 /// <param name="that">The promise to cancel on timeout.</param> | |
88 /// <param name="milliseconds">The timeout in milliseconds.</param> | |
89 /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | |
90 public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise { | |
91 Safe.ArgumentNotNull(that, "that"); | |
92 var timer = new Timer(CancelCallback, that, milliseconds, -1); | |
93 that.On(timer.Dispose, PromiseEventType.All); | |
94 return that; | |
95 } | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
96 |
124 | 97 public static IPromise Bundle(this ICollection<IPromise> that) { |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
98 Safe.ArgumentNotNull(that, "that"); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
99 |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
100 int count = that.Count; |
124 | 101 int errors = 0; |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
102 var medium = new Promise(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
103 |
124 | 104 medium.On(() => { |
105 foreach(var p2 in that) | |
106 p2.Cancel(); | |
107 }, PromiseEventType.ErrorOrCancel); | |
108 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
109 foreach (var p in that) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
110 p.On( |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
111 () => { |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
112 if (Interlocked.Decrement(ref count) == 0) |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
113 medium.Resolve(); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
114 }, |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
115 error => { |
124 | 116 if (Interlocked.Increment(ref errors) == 1) |
117 medium.Reject( | |
118 new Exception("The dependency promise is failed", error) | |
119 ); | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
120 }, |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
121 () => { |
124 | 122 if (Interlocked.Increment(ref errors) == 1) |
123 medium.Reject( | |
124 new Exception("The dependency promise is cancelled") | |
125 ); | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
126 } |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
127 ); |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
128 |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
129 return medium; |
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
110
diff
changeset
|
130 } |
124 | 131 |
132 public static IPromise<T[]> Bundle<T>(this ICollection<IPromise<T>> that) { | |
133 Safe.ArgumentNotNull(that, "that"); | |
134 | |
135 int count = that.Count; | |
136 int errors = 0; | |
137 var medium = new Promise<T[]>(); | |
138 var results = new T[that.Count]; | |
139 | |
140 medium.On(() => { | |
141 foreach(var p2 in that) | |
142 p2.Cancel(); | |
143 }, PromiseEventType.ErrorOrCancel); | |
144 | |
145 int i = 0; | |
146 foreach (var p in that) { | |
147 var idx = i; | |
148 p.On( | |
149 x => { | |
150 results[idx] = x; | |
151 if (Interlocked.Decrement(ref count) == 0) | |
152 medium.Resolve(results); | |
153 }, | |
154 error => { | |
155 if (Interlocked.Increment(ref errors) == 1) | |
156 medium.Reject( | |
157 new Exception("The dependency promise is failed", error) | |
158 ); | |
159 }, | |
160 () => { | |
161 if (Interlocked.Increment(ref errors) == 1) | |
162 medium.Reject( | |
163 new Exception("The dependency promise is cancelled") | |
164 ); | |
165 } | |
166 ); | |
167 i++; | |
168 } | |
169 | |
170 return medium; | |
171 } | |
75 | 172 |
173 #if NET_4_5 | |
174 | |
175 public static Task<T> GetTask<T>(this IPromise<T> that) { | |
176 Safe.ArgumentNotNull(that, "that"); | |
177 var tcs = new TaskCompletionSource<T>(); | |
178 | |
104 | 179 that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); |
75 | 180 |
181 return tcs.Task; | |
182 } | |
183 | |
184 #endif | |
72 | 185 } |
186 } | |
187 |