Mercurial > pub > ImplabNet
diff Implab/PromiseExtensions.cs @ 249:d82909310094 v3
Implab.Test moved to xunit
Complete set of PromiseHelpers (Then, Catch, Finally)
Removed obsolete types ICancellable, ICancellationToken
author | cin |
---|---|
date | Wed, 31 Jan 2018 11:28:38 +0300 |
parents | 5cb4826c2c2a |
children | 7d52dc684bbd |
line wrap: on
line diff
--- a/Implab/PromiseExtensions.cs Tue Jan 30 01:37:17 2018 +0300 +++ b/Implab/PromiseExtensions.cs Wed Jan 31 11:28:38 2018 +0300 @@ -8,97 +8,145 @@ public static class PromiseExtensions { public static IPromise Then(this IPromise that, Action fulfilled, Action<Exception> rejected) { - var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise Then(this IPromise that, Action fulfilled) { + var reaction = PromiseActionReaction.Create(fulfilled, null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then(this IPromise that, Action fulfilled, Func<Exception, IPromise> rejected) { - var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Action<Exception> rejected) { - var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise Then(this IPromise that, Func<IPromise> fulfilled) { + var reaction = PromiseActionReaction.Create(fulfilled, null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then(this IPromise that, Func<IPromise> fulfilled, Func<Exception, IPromise> rejected) { - var reaction = new PromiseActionReaction(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Action<Exception> rejected) { - var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled) { + var reaction = PromiseActionReaction<T>.Create(fulfilled, null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then<T>(this IPromise<T> that, Action<T> fulfilled, Func<Exception, IPromise> rejected) { - var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Action<Exception> rejected) { - var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled) { + var reaction = PromiseActionReaction<T>.Create(fulfilled, null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise Then<T>(this IPromise<T> that, Func<T, IPromise> fulfilled, Func<Exception, IPromise> rejected) { - var reaction = new PromiseActionReaction<T>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseActionReaction<T>.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, Tout> rejected) { - var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled) { + var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tout>(this IPromise that, Func<Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) { - var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) { - var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled) { + var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tout>(this IPromise that, Func<IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) { - var reaction = new PromiseFuncReaction<Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, Tout> rejected) { - var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled) { + var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, Tout> fulfilled, Func<Exception, IPromise<Tout>> rejected) { - var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, Tout> rejected) { - var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); + that.Then(reaction); + return reaction.Promise; + } + + public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled) { + var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, (Func<Exception, Tout>)null, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } public static IPromise<Tout> Then<Tin, Tout>(this IPromise<Tin> that, Func<Tin, IPromise<Tout>> fulfilled, Func<Exception, IPromise<Tout>> rejected) { - var reaction = new PromiseFuncReaction<Tin, Tout>(fulfilled, rejected, Promise.DefaultDispatcher); + var reaction = PromiseFuncReaction<Tin, Tout>.Create(fulfilled, rejected, Promise.DefaultDispatcher); that.Then(reaction); return reaction.Promise; } @@ -126,6 +174,44 @@ public static IPromise<Tout> Catch<Tin, Tout>(this IPromise<Tin> that, Func<Exception, IPromise<Tout>> rejected) { return Then(that, (Func<Tin, Tout>)null, rejected); } + + public static IPromise Finally(this IPromise that, Action final) { + return Then(that, final, e => { + final(); + throw e.Rethrow(); + }); + } + + public static IPromise Finally(this IPromise that, Func<IPromise> final) { + return Then(that, final, e => { + final(); + throw e.Rethrow(); + }); + } + + public static IPromise<T> Finally<T>(this IPromise<T> that, Action final) { + return Then<T, T>(that, x => { + final(); + return x; + }, new Func<Exception, T>(e => { + final(); + throw e.Rethrow(); + })); + } + + public static IPromise<T> Finally<T>(this IPromise<T> that, Func<IPromise> final) { + return Then<T, T>(that, x => { + return final() + .Then(() => x); + }, new Func<Exception, IPromise<T>>(e => { + return final() + .Then(new Func<T>(() => { + throw e.Rethrow(); + })); + })); + } + + } }