##// END OF EJS Templates
close v2-1...
close v2-1 bad idea

File last commit:

r209:a867536c68fc v2
r241:c19cee55e85e v2-1
Show More
PromiseExtensions.cs
107 lines | 3.5 KiB | text/x-csharp | CSharpLexer
/ Implab / PromiseExtensions.cs
cin
promises refactoring
r72 using System.Threading;
cin
major refactoring, added tasks support
r75 using System;
cin
minor fixes
r109 using Implab.Diagnostics;
cin
major refactoring, added tasks support
r75 #if NET_4_5
using System.Threading.Tasks;
#endif
cin
promises refactoring
r72
namespace Implab {
public static class PromiseExtensions {
public static IPromise<T> DispatchToCurrentContext<T>(this IPromise<T> that) {
cin
major refactoring, added tasks support
r75 Safe.ArgumentNotNull(that, "that");
cin
promises refactoring
r72 var context = SynchronizationContext.Current;
if (context == null)
return that;
cin
code cleanup...
r101 var p = new SyncContextPromise<T>(context, that);
cin
promises refactoring
r72
cin
renamed Promise.Last -> Promise.On...
r104 that.On(
cin
Refactoring of the IPromise<T> interface...
r76 p.Resolve,
p.Reject,
p.Cancel
cin
promises refactoring
r72 );
return p;
}
public static IPromise<T> DispatchToContext<T>(this IPromise<T> that, SynchronizationContext context) {
cin
major refactoring, added tasks support
r75 Safe.ArgumentNotNull(that, "that");
cin
promises refactoring
r72 Safe.ArgumentNotNull(context, "context");
cin
code cleanup...
r101 var p = new SyncContextPromise<T>(context, that);
cin
promises refactoring
r72
cin
renamed Promise.Last -> Promise.On...
r104 that.On(
cin
Refactoring of the IPromise<T> interface...
r76 p.Resolve,
p.Reject,
p.Cancel
cin
promises refactoring
r72 );
return p;
}
cin
major refactoring, added tasks support
r75
cin
code cleanup...
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
renamed Promise.Last -> Promise.On...
r104 that.On(null,null,() => head.On(cleanup));
cin
code cleanup...
r101
return that;
}
cin
major refactoring, added tasks support
r75 public static AsyncCallback AsyncCallback<T>(this Promise<T> that, Func<IAsyncResult,T> callback) {
Safe.ArgumentNotNull(that, "that");
Safe.ArgumentNotNull(callback, "callback");
cin
minor fixes
r109 var op = TraceContext.Instance.CurrentOperation;
cin
major refactoring, added tasks support
r75 return ar => {
cin
minor fixes
r109 TraceContext.Instance.EnterLogicalOperation(op,false);
cin
major refactoring, added tasks support
r75 try {
that.Resolve(callback(ar));
} catch (Exception err) {
that.Reject(err);
cin
minor fixes
r109 } finally {
TraceContext.Instance.Leave();
cin
major refactoring, added tasks support
r75 }
};
}
cin
added promise timeout helper
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
major refactoring, added tasks support
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
renamed Promise.Last -> Promise.On...
r104 that.On(tcs.SetResult, tcs.SetException, tcs.SetCanceled);
cin
major refactoring, added tasks support
r75
return tcs.Task;
}
#endif
cin
promises refactoring
r72 }
}