using System.Threading; using System; using Implab.Diagnostics; using System.Collections.Generic; using System.Linq; namespace Implab { public static class PromiseExtensions { public static IPromise DispatchToCurrentContext(this IPromise that) { Safe.ArgumentNotNull(that, "that"); var context = SynchronizationContext.Current; if (context == null) return that; var p = new SyncContextPromise(context); p.CancellationRequested(that.Cancel); that.On( p.Resolve, p.Reject, p.CancelOperation ); return p; } public static IPromise DispatchToContext(this IPromise that, SynchronizationContext context) { Safe.ArgumentNotNull(that, "that"); Safe.ArgumentNotNull(context, "context"); var p = new SyncContextPromise(context); p.CancellationRequested(that.Cancel); that.On( p.Resolve, p.Reject, p.CancelOperation ); return p; } /// /// Ensures the dispatched. /// /// The dispatched. /// That. /// Head. /// Cleanup. /// The 1st type parameter. /// The 2nd type parameter. public static TPromise EnsureDispatched(this TPromise that, IPromise head, Action cleanup) where TPromise : IPromise { Safe.ArgumentNotNull(that, "that"); Safe.ArgumentNotNull(head, "head"); that.On(() => head.On(cleanup), PromiseEventType.Cancelled); return that; } /// /// Adds a cancellation point to the chain of promises. When a cancellation request reaches the cancellation point the operation is /// cancelled immediatelly, and the request is passed towards. If the operation at the higher level can not be cancelled is't result /// will be collected with callback. /// /// The type of the promise result. /// The promise to which the cancellation point should be attached. /// The callback which is used to cleanup the result of the operation if the cancellation point is cancelled already. /// The promise public static IPromise CancellationPoint(this IPromise that, Action cleanup) { var meduim = new Promise(); that.On(meduim.Resolve, meduim.Reject, meduim.CancelOperation); meduim.CancellationRequested(that.Cancel); meduim.CancellationRequested(meduim.CancelOperation); if (cleanup != null) meduim.On((Action)null, null, (e) => { that.On(cleanup); }); return meduim; } public static AsyncCallback AsyncCallback(this Promise that, Func callback) { Safe.ArgumentNotNull(that, "that"); Safe.ArgumentNotNull(callback, "callback"); var op = TraceContext.Instance.CurrentOperation; return ar => { TraceContext.Instance.EnterLogicalOperation(op, false); try { that.Resolve(callback(ar)); } catch (Exception err) { that.Reject(err); } finally { TraceContext.Instance.Leave(); } }; } static void CancelByTimeoutCallback(object cookie) { ((ICancellable)cookie).Cancel(new TimeoutException()); } /// /// Cancells promise after the specified timeout is elapsed. /// /// The promise to cancel on timeout. /// The timeout in milliseconds. /// The 1st type parameter. public static TPromise Timeout(this TPromise that, int milliseconds) where TPromise : IPromise { Safe.ArgumentNotNull(that, "that"); var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1); that.On(timer.Dispose, PromiseEventType.All); return that; } public static IPromise PromiseAll(this IEnumerable that) { Safe.ArgumentNotNull(that, "that"); return PromiseAll(that.ToList()); } public static IPromise PromiseAll(this IEnumerable> that) { return PromiseAll(that, null); } public static IPromise PromiseAll(this IEnumerable> that, Action cleanup) { Safe.ArgumentNotNull(that, "that"); return PromiseAll(that.ToList(), cleanup); } public static IPromise PromiseAll(this ICollection that) { Safe.ArgumentNotNull(that, "that"); int count = that.Count; int errors = 0; var medium = new Promise(); if (count == 0) { medium.Resolve(); return medium; } medium.On(() => { foreach (var p2 in that) p2.Cancel(); }, PromiseEventType.ErrorOrCancel); foreach (var p in that) p.On( () => { if (Interlocked.Decrement(ref count) == 0) medium.Resolve(); }, error => { if (Interlocked.Increment(ref errors) == 1) medium.Reject( new Exception("The dependency promise is failed", error) ); }, reason => { if (Interlocked.Increment(ref errors) == 1) medium.Cancel( new Exception("The dependency promise is cancelled") ); } ); return medium; } public static IPromise PromiseAll(this ICollection> that) { return PromiseAll(that, null); } /// /// Creates a new promise which will be satisfied when all promises are satisfied. /// /// /// /// A callback used to cleanup already resolved promises in case of an error /// public static IPromise PromiseAll(this ICollection> that, Action cleanup) { Safe.ArgumentNotNull(that, "that"); int count = that.Count; if (count == 0) return Promise.FromResult(new T[0]); int errors = 0; var medium = new Promise(); var results = new T[that.Count]; medium.On(() => { foreach (var p2 in that) { p2.Cancel(); if (cleanup != null) p2.On(cleanup); } }, PromiseEventType.ErrorOrCancel); int i = 0; foreach (var p in that) { var idx = i; p.On( x => { results[idx] = x; if (Interlocked.Decrement(ref count) == 0) medium.Resolve(results); }, error => { if (Interlocked.Increment(ref errors) == 1) medium.Reject( new Exception("The dependency promise is failed", error) ); }, reason => { if (Interlocked.Increment(ref errors) == 1) medium.Cancel( new Exception("The dependency promise is cancelled", reason) ); } ); i++; } return medium; } public static IPromise Then(this IPromise that, Action success, Action error, Action cancel) { Safe.ArgumentNotNull(that, "that"); var d = new ActionTask(success, error, cancel, false); that.On(d.Resolve, d.Reject, d.CancelOperation); d.CancellationRequested(that.Cancel); return d; } public static IPromise Then(this IPromise that, Action success, Action error) { return Then(that, success, error, null); } public static IPromise Then(this IPromise that, Action success) { return Then(that, success, null, null); } public static IPromise Then(this IPromise that, Func success, Func error, Func cancel) { Safe.ArgumentNotNull(that, "that"); var d = new FuncTask(success, error, cancel, false); that.On(d.Resolve, d.Reject, d.CancelOperation); d.CancellationRequested(that.Cancel); return d; } public static IPromise Then(this IPromise that, Func success, Func error) { return Then(that, success, error, null); } public static IPromise Then(this IPromise that, Func success) { return Then(that, success, null, null); } public static IPromise Then(this IPromise that, Func success, Func error, Func cancel) { Safe.ArgumentNotNull(that, "that"); Safe.ArgumentNotNull(success, "success"); var d = new FuncTask(success, error, cancel, false); that.On(d.Resolve, d.Reject, d.CancelOperation); d.CancellationRequested(that.Cancel); return d; } public static IPromise Then(this IPromise that, Action success, Func error, Func cancel) { Safe.ArgumentNotNull(that, "that"); var d = new FuncTask( x => { success(x); return x; }, error, cancel, false ); that.On(d.Resolve, d.Reject, d.CancelOperation); d.CancellationRequested(that.Cancel); return d; } public static IPromise Then(this IPromise that, Action success, Func error) { return Then(that, success, error, null); } public static IPromise Then(this IPromise that, Action success) { return Then(that, success, null, null); } public static IPromise Then(this IPromise that, Func success, Func error) { return Then(that, success, error, null); } public static IPromise Then(this IPromise that, Func success) { return Then(that, success, null, null); } public static IPromise Always(this IPromise that, Action handler) { Func errorOrCancel; if (handler != null) errorOrCancel = e => { handler(); throw new PromiseTransientException(e); }; else errorOrCancel = null; return Then( that, x => { handler(); return x; }, errorOrCancel, errorOrCancel); } public static IPromise Always(this IPromise that, Action handler) { Action errorOrCancel; if (handler != null) errorOrCancel = e => { handler(); throw new PromiseTransientException(e); }; else errorOrCancel = null; return Then( that, handler, errorOrCancel, errorOrCancel); } public static IPromise Error(this IPromise that, Action handler, bool handleCancellation) { Action errorOrCancel; if (handler != null) errorOrCancel = e => { handler(e); throw new PromiseTransientException(e); }; else errorOrCancel = null; return Then(that, null, errorOrCancel, handleCancellation ? errorOrCancel : null); } public static IPromise Error(this IPromise that, Action handler) { return Error(that, handler, false); } public static IPromise Error(this IPromise that, Action handler, bool handleCancellation) { Func errorOrCancel; if (handler != null) errorOrCancel = e => { handler(e); throw new PromiseTransientException(e); }; else errorOrCancel = null; return Then(that, null, errorOrCancel, handleCancellation ? errorOrCancel : null); } public static IPromise Error(this IPromise that, Action handler) { return Error(that, handler, false); } #region chain traits public static IPromise Chain(this IPromise that, Func success, Func error, Func cancel) { Safe.ArgumentNotNull(that, "that"); var d = new ActionChainTask(success, error, cancel, false); that.On(d.Resolve, d.Reject, d.CancelOperation); d.CancellationRequested(that.Cancel); return d; } public static IPromise Chain(this IPromise that, Func success, Func error) { return Chain(that, success, error, null); } public static IPromise Chain(this IPromise that, Func success) { return Chain(that, success, null, null); } public static IPromise Chain(this IPromise that, Func> success, Func> error, Func> cancel) { Safe.ArgumentNotNull(that, "that"); var d = new FuncChainTask(success, error, cancel, false); that.On(d.Resolve, d.Reject, d.CancelOperation); if (success != null) d.CancellationRequested(that.Cancel); return d; } public static IPromise Chain(this IPromise that, Func> success, Func> error) { return Chain(that, success, error, null); } public static IPromise Chain(this IPromise that, Func> success) { return Chain(that, success, null, null); } public static IPromise Chain(this IPromise that, Func> success, Func> error, Func> cancel) { Safe.ArgumentNotNull(that, "that"); var d = new FuncChainTask(success, error, cancel, false); that.On(d.Resolve, d.Reject, d.CancelOperation); if (success != null) d.CancellationRequested(that.Cancel); return d; } public static IPromise Chain(this IPromise that, Func> success, Func> error) { return Chain(that, success, error, null); } public static IPromise Chain(this IPromise that, Func> success) { return Chain(that, success, null, null); } #endregion public static IPromise Guard(this IPromise that, Func, IPromise> continuation, Action cleanup) { Safe.ArgumentNotNull(that, "that"); Safe.ArgumentNotNull(continuation, "continuation"); return continuation(that).Error((err) => { that.On(cleanup); }, true); } #if NET_4_5 public static PromiseAwaiter GetAwaiter(this IPromise that) { Safe.ArgumentNotNull(that, "that"); return new PromiseAwaiter(that); } public static PromiseAwaiter GetAwaiter(this IPromise that) { Safe.ArgumentNotNull(that, "that"); return new PromiseAwaiter(that); } public static IPromise BoundCancellationToken(this IPromise that, CancellationToken ct) { Safe.ArgumentNotNull(that, "that"); ct.Register(that.Cancel); return that.Then(null, null, (err) => { ct.ThrowIfCancellationRequested(); throw new PromiseTransientException(err); }); } public static IPromise BoundCancellationToken(this IPromise that, CancellationToken ct) { Safe.ArgumentNotNull(that, "that"); ct.Register(that.Cancel); return that.Then(null, null, (err) => { ct.ThrowIfCancellationRequested(); throw new PromiseTransientException(err); }); } #endif } }