##// END OF EJS Templates
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'...
cin -
r207:558f34b2fb50 v2
parent child
Show More
@@ -2,7 +2,7
2
2
3 namespace Implab.Components
3 namespace Implab.Components
4 {
4 {
5 public class StateChangeEventArgs {
5 public class StateChangeEventArgs : EventArgs {
6 /// <summary>
6 /// <summary>
7 /// The error information if any
7 /// The error information if any
8 /// </summary>
8 /// </summary>
@@ -56,6 +56,31 namespace Implab {
56 return that;
56 return that;
57 }
57 }
58
58
59 /// <summary>
60 /// Adds a cancellation point to the chain of promises. When a cancellation request reaches the cancellation point the operation is
61 /// cancelled immediatelly, and the request is passed towards. If the operation at the higher level can not be cancelled is't result
62 /// will be collected with <paramref name="cleanup"/> callback.
63 /// </summary>
64 /// <typeparam name="T">The type of the promise result.</typeparam>
65 /// <param name="that">The promise to which the cancellation point should be attached.</param>
66 /// <param name="cleanup">The callback which is used to cleanup the result of the operation if the cancellation point is cancelled already.</param>
67 /// <returns>The promise</returns>
68 public static IPromise<T> CancellationPoint<T>(this IPromise<T> that, Action<T> cleanup) {
69 var meduim = new Promise<T>();
70
71 that.On(meduim.Resolve, meduim.Reject, meduim.CancelOperation);
72
73 meduim.CancellationRequested(that.Cancel);
74 meduim.CancellationRequested(meduim.CancelOperation);
75
76 if (cleanup != null)
77 meduim.On((Action<T>)null, null, (e) => {
78 that.On(cleanup);
79 });
80
81 return meduim;
82 }
83
59 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
84 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult, T> callback) {
60 Safe.ArgumentNotNull(that, "that");
85 Safe.ArgumentNotNull(that, "that");
61 Safe.ArgumentNotNull(callback, "callback");
86 Safe.ArgumentNotNull(callback, "callback");
@@ -222,6 +247,30 namespace Implab {
222 return d;
247 return d;
223 }
248 }
224
249
250 public static IPromise<T> Then<T>(this IPromise<T> that, Action<T> success, Func<Exception, T> error, Func<Exception, T> cancel) {
251 Safe.ArgumentNotNull(that, "that");
252 var d = new FuncTask<T, T>(
253 x => {
254 success(x);
255 return x;
256 },
257 error,
258 cancel,
259 false
260 );
261 that.On(d.Resolve, d.Reject, d.CancelOperation);
262 d.CancellationRequested(that.Cancel);
263 return d;
264 }
265
266 public static IPromise<T> Then<T>(this IPromise<T> that, Action<T> success, Func<Exception, T> error) {
267 return Then(that, success, error, null);
268 }
269
270 public static IPromise<T> Then<T>(this IPromise<T> that, Action<T> success) {
271 return Then(that, success, null, null);
272 }
273
225 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) {
274 public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) {
226 return Then(that, success, error, null);
275 return Then(that, success, error, null);
227 }
276 }
@@ -230,6 +279,77 namespace Implab {
230 return Then(that, success, null, null);
279 return Then(that, success, null, null);
231 }
280 }
232
281
282 public static IPromise<T> Always<T>(this IPromise<T> that, Action handler) {
283 Func<Exception, T> errorOrCancel;
284 if (handler != null)
285 errorOrCancel = e => {
286 handler();
287 throw new PromiseTransientException(e);
288 };
289 else
290 errorOrCancel = null;
291
292 return Then(
293 that,
294 x => {
295 handler();
296 return x;
297 },
298 errorOrCancel,
299 errorOrCancel);
300 }
301
302 public static IPromise Always(this IPromise that, Action handler) {
303 Action<Exception> errorOrCancel;
304 if (handler != null)
305 errorOrCancel = e => {
306 handler();
307 throw new PromiseTransientException(e);
308 };
309 else
310 errorOrCancel = null;
311
312 return Then(
313 that,
314 handler,
315 errorOrCancel,
316 errorOrCancel);
317 }
318
319 public static IPromise Error(this IPromise that, Action<Exception> handler, bool handleCancellation) {
320 Action<Exception> errorOrCancel;
321 if (handler != null)
322 errorOrCancel = e => {
323 handler(e);
324 throw new PromiseTransientException(e);
325 };
326 else
327 errorOrCancel = null;
328
329 return Then(that, null, errorOrCancel, handleCancellation ? errorOrCancel : null);
330 }
331
332 public static IPromise Error(this IPromise that, Action<Exception> handler) {
333 return Error(that, handler, false);
334 }
335
336 public static IPromise<T> Error<T>(this IPromise<T> that, Action<Exception> handler, bool handleCancellation) {
337 Func<Exception, T> errorOrCancel;
338 if (handler != null)
339 errorOrCancel = e => {
340 handler(e);
341 throw new PromiseTransientException(e);
342 };
343 else
344 errorOrCancel = null;
345
346 return Then(that, null, errorOrCancel, handleCancellation ? errorOrCancel : null);
347 }
348
349 public static IPromise<T> Error<T>(this IPromise<T> that, Action<Exception> handler) {
350 return Error(that, handler, false);
351 }
352
233 #region chain traits
353 #region chain traits
234 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error, Func<Exception,IPromise> cancel) {
354 public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception, IPromise> error, Func<Exception, IPromise> cancel) {
235 Safe.ArgumentNotNull(that, "that");
355 Safe.ArgumentNotNull(that, "that");
@@ -4,6 +4,7 using System.Linq;
4 using System.Text;
4 using System.Text;
5 using System.Text.RegularExpressions;
5 using System.Text.RegularExpressions;
6 using System.Diagnostics;
6 using System.Diagnostics;
7 using System.Collections;
7
8
8 namespace Implab
9 namespace Implab
9 {
10 {
@@ -60,12 +61,28 namespace Implab
60 }
61 }
61 }
62 }
62
63
64 public static void Dispose(IEnumerable<IDisposable> objects) {
65 foreach (var d in objects)
66 if (d != null)
67 d.Dispose();
68 }
69
63 public static void Dispose(object obj) {
70 public static void Dispose(object obj) {
64 var d = obj as IDisposable;
71 var d = obj as IDisposable;
65 if (d != null)
72 if (d != null)
66 d.Dispose();
73 d.Dispose();
67 }
74 }
68
75
76 public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) {
77 if (handler != null)
78 handler(sender, args);
79 }
80
81 public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) {
82 if (handler != null)
83 handler(sender, args);
84 }
85
69 [DebuggerStepThrough]
86 [DebuggerStepThrough]
70 public static IPromise<T> Run<T>(Func<T> action) {
87 public static IPromise<T> Run<T>(Func<T> action) {
71 ArgumentNotNull(action, "action");
88 ArgumentNotNull(action, "action");
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now