PromiseExtensions.cs
289 lines
| 10.8 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / PromiseExtensions.cs
|
|
r72 | using System.Threading; | ||
|
|
r75 | using System; | ||
|
|
r109 | using Implab.Diagnostics; | ||
|
|
r119 | using System.Collections.Generic; | ||
|
|
r109 | |||
|
|
r72 | namespace Implab { | ||
| public static class PromiseExtensions { | ||||
| public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { | ||||
|
|
r75 | Safe.ArgumentNotNull(that, "that"); | ||
|
|
r72 | var context = SynchronizationContext.Current; | ||
| if (context == null) | ||||
| return that; | ||||
|
|
r119 | var p = new SyncContextPromise<T>(context); | ||
|
|
r185 | p.CancellationRequested(that.Cancel); | ||
|
|
r72 | |||
|
|
r104 | that.On( | ||
|
|
r76 | p.Resolve, | ||
| p.Reject, | ||||
|
|
r185 | p.CancelOperation | ||
|
|
r72 | ); | ||
| return p; | ||||
| } | ||||
| public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | ||||
|
|
r75 | Safe.ArgumentNotNull(that, "that"); | ||
|
|
r72 | Safe.ArgumentNotNull(context, "context"); | ||
|
|
r119 | var p = new SyncContextPromise<T>(context); | ||
|
|
r185 | p.CancellationRequested(that.Cancel); | ||
|
|
r72 | |||
|
|
r104 | that.On( | ||
|
|
r76 | p.Resolve, | ||
| p.Reject, | ||||
|
|
r185 | p.CancelOperation | ||
|
|
r72 | ); | ||
| return p; | ||||
| } | ||||
|
|
r75 | |||
|
|
r101 | /// <summary> | ||
| /// Ensures the dispatched. | ||||
| /// </summary> | ||||
| /// <returns>The dispatched.</returns> | ||||
| /// <param name="that">That.</param> | ||||
| /// <param name="head">Head.</param> | ||||
| /// <param name="cleanup">Cleanup.</param> | ||||
| /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | ||||
| /// <typeparam name="T">The 2nd type parameter.</typeparam> | ||||
| public static TPromise EnsureDispatched<TPromise,T>(this TPromise that, IPromise<T> head, Action<T> cleanup) where TPromise : IPromise{ | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
| Safe.ArgumentNotNull(head, "head"); | ||||
|
|
r138 | that.On(() => head.On(cleanup), PromiseEventType.Cancelled); | ||
|
|
r101 | |||
| return that; | ||||
| } | ||||
|
|
r75 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { | ||
| Safe.ArgumentNotNull(that, "that"); | ||||
| Safe.ArgumentNotNull(callback, "callback"); | ||||
|
|
r109 | var op = TraceContext.Instance.CurrentOperation; | ||
|
|
r75 | return ar => { | ||
|
|
r109 | TraceContext.Instance.EnterLogicalOperation(op,false); | ||
|
|
r75 | try { | ||
| that.Resolve(callback(ar)); | ||||
| } catch (Exception err) { | ||||
| that.Reject(err); | ||||
|
|
r109 | } finally { | ||
| TraceContext.Instance.Leave(); | ||||
|
|
r75 | } | ||
| }; | ||||
| } | ||||
|
|
r110 | |||
|
|
r185 | static void CancelByTimeoutCallback(object cookie) { | ||
| ((ICancellable)cookie).Cancel(new TimeoutException()); | ||||
|
|
r110 | } | ||
| /// <summary> | ||||
| /// Cancells promise after the specified timeout is elapsed. | ||||
| /// </summary> | ||||
| /// <param name="that">The promise to cancel on timeout.</param> | ||||
| /// <param name="milliseconds">The timeout in milliseconds.</param> | ||||
| /// <typeparam name="TPromise">The 1st type parameter.</typeparam> | ||||
| public static TPromise Timeout<TPromise>(this TPromise that, int milliseconds) where TPromise : IPromise { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r185 | var timer = new Timer(CancelByTimeoutCallback, that, milliseconds, -1); | ||
|
|
r110 | that.On(timer.Dispose, PromiseEventType.All); | ||
| return that; | ||||
| } | ||||
|
|
r119 | |||
|
|
r124 | public static IPromise Bundle(this ICollection<IPromise> that) { | ||
|
|
r119 | Safe.ArgumentNotNull(that, "that"); | ||
| int count = that.Count; | ||||
|
|
r124 | int errors = 0; | ||
|
|
r119 | var medium = new Promise(); | ||
|
|
r136 | if (count == 0) { | ||
| medium.Resolve(); | ||||
| return medium; | ||||
| } | ||||
|
|
r124 | medium.On(() => { | ||
| foreach(var p2 in that) | ||||
| p2.Cancel(); | ||||
| }, PromiseEventType.ErrorOrCancel); | ||||
|
|
r119 | foreach (var p in that) | ||
| p.On( | ||||
| () => { | ||||
| if (Interlocked.Decrement(ref count) == 0) | ||||
| medium.Resolve(); | ||||
| }, | ||||
| error => { | ||||
|
|
r124 | if (Interlocked.Increment(ref errors) == 1) | ||
| medium.Reject( | ||||
| new Exception("The dependency promise is failed", error) | ||||
| ); | ||||
|
|
r119 | }, | ||
|
|
r138 | reason => { | ||
|
|
r124 | if (Interlocked.Increment(ref errors) == 1) | ||
|
|
r138 | medium.Cancel( | ||
|
|
r124 | new Exception("The dependency promise is cancelled") | ||
| ); | ||||
|
|
r119 | } | ||
| ); | ||||
| return medium; | ||||
| } | ||||
|
|
r124 | |||
| public static IPromise<T[]> Bundle<T>(this ICollection<IPromise<T>> that) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
| int count = that.Count; | ||||
| int errors = 0; | ||||
| var medium = new Promise<T[]>(); | ||||
| 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) | ||||
| ); | ||||
| }, | ||||
|
|
r138 | reason => { | ||
|
|
r124 | if (Interlocked.Increment(ref errors) == 1) | ||
|
|
r138 | medium.Cancel( | ||
| new Exception("The dependency promise is cancelled", reason) | ||||
|
|
r124 | ); | ||
| } | ||||
| ); | ||||
| i++; | ||||
| } | ||||
| return medium; | ||||
| } | ||||
|
|
r145 | |||
| public static IPromise Then(this IPromise that, Action success, Action<Exception> error, Action<Exception> cancel) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r149 | var d = new ActionTask(success, error, cancel, false); | ||
|
|
r145 | that.On(d.Resolve, d.Reject, d.CancelOperation); | ||
|
|
r185 | d.CancellationRequested(that.Cancel); | ||
|
|
r145 | return d; | ||
| } | ||||
| public static IPromise Then(this IPromise that, Action success, Action<Exception> 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<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error, Func<Exception, T> cancel) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r149 | var d = new FuncTask<T>(success, error, cancel, false); | ||
|
|
r145 | that.On(d.Resolve, d.Reject, d.CancelOperation); | ||
|
|
r185 | d.CancellationRequested(that.Cancel); | ||
|
|
r145 | return d; | ||
| } | ||||
| public static IPromise<T> Then<T>(this IPromise that, Func<T> success, Func<Exception, T> error) { | ||||
| return Then(that, success, error, null); | ||||
| } | ||||
| public static IPromise<T> Then<T>(this IPromise that, Func<T> success) { | ||||
| return Then(that, success, null, null); | ||||
| } | ||||
| public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error, Func<Exception, T2> cancel) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r149 | var d = new FuncTask<T,T2>(success, error, cancel, false); | ||
|
|
r145 | that.On(d.Resolve, d.Reject, d.CancelOperation); | ||
|
|
r185 | d.CancellationRequested(that.Cancel); | ||
|
|
r145 | return d; | ||
| } | ||||
| public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success, Func<Exception, T2> error) { | ||||
| return Then(that, success, error, null); | ||||
| } | ||||
| public static IPromise<T2> Then<T, T2>(this IPromise<T> that, Func<T, T2> success) { | ||||
| return Then(that, success, null, null); | ||||
| } | ||||
| #region chain traits | ||||
| public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error, Func<Exception,IPromise> cancel) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r149 | var d = new ActionChainTask(success, error, cancel, false); | ||
|
|
r145 | that.On(d.Resolve, d.Reject, d.CancelOperation); | ||
|
|
r185 | d.CancellationRequested(that.Cancel); | ||
|
|
r145 | return d; | ||
| } | ||||
| public static IPromise Chain(this IPromise that, Func<IPromise> success, Func<Exception,IPromise> error) { | ||||
| return Chain(that, success, error, null); | ||||
| } | ||||
| public static IPromise Chain(this IPromise that, Func<IPromise> success) { | ||||
| return Chain(that, success, null, null); | ||||
| } | ||||
| public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success, Func<Exception, IPromise<T>> error, Func<Exception, IPromise<T>> cancel) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r149 | var d = new FuncChainTask<T>(success, error, cancel, false); | ||
|
|
r145 | that.On(d.Resolve, d.Reject, d.CancelOperation); | ||
| if (success != null) | ||||
| d.CancellationRequested(that.Cancel); | ||||
| return d; | ||||
| } | ||||
| public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success, Func<Exception, IPromise<T>> error) { | ||||
| return Chain(that, success, error, null); | ||||
| } | ||||
| public static IPromise<T> Chain<T>(this IPromise that, Func<IPromise<T>> success) { | ||||
| return Chain(that, success, null, null); | ||||
| } | ||||
| public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success, Func<Exception, IPromise<T2>> error, Func<Exception, IPromise<T2>> cancel) { | ||||
| Safe.ArgumentNotNull(that, "that"); | ||||
|
|
r149 | var d = new FuncChainTask<T,T2>(success, error, cancel, false); | ||
|
|
r145 | that.On(d.Resolve, d.Reject, d.CancelOperation); | ||
| if (success != null) | ||||
| d.CancellationRequested(that.Cancel); | ||||
| return d; | ||||
| } | ||||
| public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success, Func<Exception, IPromise<T2>> error) { | ||||
| return Chain(that, success, error, null); | ||||
| } | ||||
| public static IPromise<T2> Chain<T, T2>(this IPromise<T> that, Func<T, IPromise<T2>> success) { | ||||
| return Chain(that, success, null, null); | ||||
| } | ||||
| #endregion | ||||
|
|
r75 | |||
| #if NET_4_5 | ||||
|
|
r151 | public static PromiseAwaiter<T> GetAwaiter<T>(this IPromise<T> that) { | ||
|
|
r75 | Safe.ArgumentNotNull(that, "that"); | ||
|
|
r151 | return new PromiseAwaiter<T>(that); | ||
|
|
r75 | } | ||
| #endif | ||||
|
|
r72 | } | ||
| } | ||||
