PromiseExtensions.cs
192 lines
| 6.4 KiB
| text/x-csharp
|
CSharpLexer
/ Implab / PromiseExtensions.cs
cin
|
r72 | using System.Threading; | ||
cin
|
r75 | using System; | ||
cin
|
r109 | using Implab.Diagnostics; | ||
cin
|
r119 | using System.Collections.Generic; | ||
cin
|
r109 | |||
cin
|
r75 | #if NET_4_5 | ||
using System.Threading.Tasks; | ||||
#endif | ||||
cin
|
r72 | |||
namespace Implab { | ||||
public static class PromiseExtensions { | ||||
public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) { | ||||
cin
|
r75 | Safe.ArgumentNotNull(that, "that"); | ||
cin
|
r72 | var context = SynchronizationContext.Current; | ||
if (context == null) | ||||
return that; | ||||
cin
|
r119 | var p = new SyncContextPromise<T>(context); | ||
p.On(that.Cancel, PromiseEventType.Cancelled); | ||||
cin
|
r72 | |||
cin
|
r104 | that.On( | ||
cin
|
r76 | p.Resolve, | ||
p.Reject, | ||||
p.Cancel | ||||
cin
|
r72 | ); | ||
return p; | ||||
} | ||||
public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) { | ||||
cin
|
r75 | Safe.ArgumentNotNull(that, "that"); | ||
cin
|
r72 | Safe.ArgumentNotNull(context, "context"); | ||
cin
|
r119 | var p = new SyncContextPromise<T>(context); | ||
p.On(that.Cancel, PromiseEventType.Cancelled); | ||||
cin
|
r72 | |||
cin
|
r104 | that.On( | ||
cin
|
r76 | p.Resolve, | ||
p.Reject, | ||||
p.Cancel | ||||
cin
|
r72 | ); | ||
return p; | ||||
} | ||||
cin
|
r75 | |||
cin
|
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"); | ||||
cin
|
r138 | that.On(() => head.On(cleanup), PromiseEventType.Cancelled); | ||
cin
|
r101 | |||
return that; | ||||
} | ||||
cin
|
r75 | public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) { | ||
Safe.ArgumentNotNull(that, "that"); | ||||
Safe.ArgumentNotNull(callback, "callback"); | ||||
cin
|
r109 | var op = TraceContext.Instance.CurrentOperation; | ||
cin
|
r75 | return ar => { | ||
cin
|
r109 | TraceContext.Instance.EnterLogicalOperation(op,false); | ||
cin
|
r75 | try { | ||
that.Resolve(callback(ar)); | ||||
} catch (Exception err) { | ||||
that.Reject(err); | ||||
cin
|
r109 | } finally { | ||
TraceContext.Instance.Leave(); | ||||
cin
|
r75 | } | ||
}; | ||||
} | ||||
cin
|
r110 | |||
static void CancelCallback(object cookie) { | ||||
((ICancellable)cookie).Cancel(); | ||||
} | ||||
/// <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"); | ||||
var timer = new Timer(CancelCallback, that, milliseconds, -1); | ||||
that.On(timer.Dispose, PromiseEventType.All); | ||||
return that; | ||||
} | ||||
cin
|
r119 | |||
cin
|
r124 | public static IPromise Bundle(this ICollection<IPromise> that) { | ||
cin
|
r119 | Safe.ArgumentNotNull(that, "that"); | ||
int count = that.Count; | ||||
cin
|
r124 | int errors = 0; | ||
cin
|
r119 | var medium = new Promise(); | ||
cin
|
r136 | if (count == 0) { | ||
medium.Resolve(); | ||||
return medium; | ||||
} | ||||
cin
|
r124 | medium.On(() => { | ||
foreach(var p2 in that) | ||||
p2.Cancel(); | ||||
}, PromiseEventType.ErrorOrCancel); | ||||
cin
|
r119 | foreach (var p in that) | ||
p.On( | ||||
() => { | ||||
if (Interlocked.Decrement(ref count) == 0) | ||||
medium.Resolve(); | ||||
}, | ||||
error => { | ||||
cin
|
r124 | if (Interlocked.Increment(ref errors) == 1) | ||
medium.Reject( | ||||
new Exception("The dependency promise is failed", error) | ||||
); | ||||
cin
|
r119 | }, | ||
cin
|
r138 | reason => { | ||
cin
|
r124 | if (Interlocked.Increment(ref errors) == 1) | ||
cin
|
r138 | medium.Cancel( | ||
cin
|
r124 | new Exception("The dependency promise is cancelled") | ||
); | ||||
cin
|
r119 | } | ||
); | ||||
return medium; | ||||
} | ||||
cin
|
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) | ||||
); | ||||
}, | ||||
cin
|
r138 | reason => { | ||
cin
|
r124 | if (Interlocked.Increment(ref errors) == 1) | ||
cin
|
r138 | medium.Cancel( | ||
new Exception("The dependency promise is cancelled", reason) | ||||
cin
|
r124 | ); | ||
} | ||||
); | ||||
i++; | ||||
} | ||||
return medium; | ||||
} | ||||
cin
|
r75 | |||
#if NET_4_5 | ||||
public static Task<T> GetTask<T>(this IPromise<T> that) { | ||||
Safe.ArgumentNotNull(that, "that"); | ||||
var tcs = new TaskCompletionSource<T>(); | ||||
cin
|
r138 | that.On(tcs.SetResult, tcs.SetException, r => tcs.SetCanceled()); | ||
cin
|
r75 | |||
return tcs.Task; | ||||
} | ||||
#endif | ||||
cin
|
r72 | } | ||
} | ||||