using System.Threading; using System; using Implab.Diagnostics; using System.Collections.Generic; #if NET_4_5 using System.Threading.Tasks; #endif 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.On(that.Cancel, PromiseEventType.Cancelled); that.On( p.Resolve, p.Reject, p.Cancel ); 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.On(that.Cancel, PromiseEventType.Cancelled); that.On( p.Resolve, p.Reject, p.Cancel ); 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; } 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 CancelCallback(object cookie) { ((ICancellable)cookie).Cancel(); } /// /// 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(CancelCallback, that, milliseconds, -1); that.On(timer.Dispose, PromiseEventType.All); return that; } public static IPromise Bundle(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 Bundle(this ICollection> that) { Safe.ArgumentNotNull(that, "that"); int count = that.Count; int errors = 0; var medium = new Promise(); var results = new T[that.Count]; medium.On(() => { foreach(var p2 in that) p2.Cancel(); }, 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; } #if NET_4_5 public static Task GetTask(this IPromise that) { Safe.ArgumentNotNull(that, "that"); var tcs = new TaskCompletionSource(); that.On(tcs.SetResult, tcs.SetException, r => tcs.SetCanceled()); return tcs.Task; } #endif } }