comparison Implab/PromiseExtensions.cs @ 138:f75cfa58e3d4 v2

added ICancellable.Cancel(Exception) to allow specify the reason of cancellation
author cin
date Tue, 17 Feb 2015 18:16:26 +0300
parents e9e7940c7d98
children 706fccb85524
comparison
equal deleted inserted replaced
137:238e15580926 138:f75cfa58e3d4
54 /// <typeparam name="T">The 2nd 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{ 55 public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{
56 Safe.ArgumentNotNull(that, "that"); 56 Safe.ArgumentNotNull(that, "that");
57 Safe.ArgumentNotNull(head, "head"); 57 Safe.ArgumentNotNull(head, "head");
58 58
59 that.On(null,null,() => head.On(cleanup)); 59 that.On(() => head.On(cleanup), PromiseEventType.Cancelled);
60 60
61 return that; 61 return that;
62 } 62 }
63 63
64 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { 64 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
121 if (Interlocked.Increment(ref errors) == 1) 121 if (Interlocked.Increment(ref errors) == 1)
122 medium.Reject( 122 medium.Reject(
123 new Exception("The dependency promise is failed", error) 123 new Exception("The dependency promise is failed", error)
124 ); 124 );
125 }, 125 },
126 () => { 126 reason => {
127 if (Interlocked.Increment(ref errors) == 1) 127 if (Interlocked.Increment(ref errors) == 1)
128 medium.Reject( 128 medium.Cancel(
129 new Exception("The dependency promise is cancelled") 129 new Exception("The dependency promise is cancelled")
130 ); 130 );
131 } 131 }
132 ); 132 );
133 133
160 if (Interlocked.Increment(ref errors) == 1) 160 if (Interlocked.Increment(ref errors) == 1)
161 medium.Reject( 161 medium.Reject(
162 new Exception("The dependency promise is failed", error) 162 new Exception("The dependency promise is failed", error)
163 ); 163 );
164 }, 164 },
165 () => { 165 reason => {
166 if (Interlocked.Increment(ref errors) == 1) 166 if (Interlocked.Increment(ref errors) == 1)
167 medium.Reject( 167 medium.Cancel(
168 new Exception("The dependency promise is cancelled") 168 new Exception("The dependency promise is cancelled", reason)
169 ); 169 );
170 } 170 }
171 ); 171 );
172 i++; 172 i++;
173 } 173 }
179 179
180 public static Task<T> GetTask<T>(this IPromise<T> that) { 180 public static Task<T> GetTask<T>(this IPromise<T> that) {
181 Safe.ArgumentNotNull(that, "that"); 181 Safe.ArgumentNotNull(that, "that");
182 var tcs = new TaskCompletionSource<T>(); 182 var tcs = new TaskCompletionSource<T>();
183 183
184 that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled); 184 that.On(tcs.SetResult, tcs.SetException, r => tcs.SetCanceled());
185 185
186 return tcs.Task; 186 return tcs.Task;
187 } 187 }
188 188
189 #endif 189 #endif