diff --git a/Implab/IPromise.cs b/Implab/IPromise.cs --- a/Implab/IPromise.cs +++ b/Implab/IPromise.cs @@ -7,10 +7,26 @@ namespace Implab { public interface IPromise<T>: IPromiseBase { - + + T Join(); + + T Join(int timeout); + IPromise<T> Then(ResultHandler<T> success, ErrorHandler error); + IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error); + IPromise<T> Then(ResultHandler<T> success); + IPromise<T> Error(ErrorHandler error); + IPromise<T> Error(ErrorHandler<T> error); + IPromise<T2> Map<T2>(ResultMapper<T,T2> mapper, ErrorHandler error); + IPromise<T2> Map<T2>(ResultMapper<T, T2> mapper); + IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained, ErrorHandler error); + IPromise<T2> Chain<T2>(ChainedOperation<T, T2> chained); + + IPromise<T> Cancelled(Action handler); + IPromise<T> Finally(Action handler); + IPromise<T> Anyway(Action handler); } } diff --git a/Implab/IPromiseBase.cs b/Implab/IPromiseBase.cs --- a/Implab/IPromiseBase.cs +++ b/Implab/IPromiseBase.cs @@ -15,5 +15,9 @@ namespace Implab { bool IsResolved { get; } bool IsCancelled { get; } + + IPromiseBase Then(Action success,ErrorHandler error); + IPromiseBase Then(Action success); + } } diff --git a/Implab/Parallels/ArrayTraits.cs b/Implab/Parallels/ArrayTraits.cs --- a/Implab/Parallels/ArrayTraits.cs +++ b/Implab/Parallels/ArrayTraits.cs @@ -125,7 +125,7 @@ namespace Implab.Parallels { return iter.Promise; } - public static Promise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) { + public static IPromise<TDst[]> ChainedMap<TSrc, TDst>(this TSrc[] source, ChainedOperation<TSrc, TDst> transform, int threads) { if (source == null) throw new ArgumentNullException("source"); if (transform == null) diff --git a/Implab/Promise.cs b/Implab/Promise.cs --- a/Implab/Promise.cs +++ b/Implab/Promise.cs @@ -191,7 +191,7 @@ namespace Implab { /// This handler will recieve an operation result as a parameter.</param> /// <param name="error">Handles an exception that may occur during the operation.</param> /// <returns>The new promise chained to this one.</returns> - public Promise<T> Then(ResultHandler<T> success, ErrorHandler error) { + public IPromise<T> Then(ResultHandler<T> success, ErrorHandler error) { if (success == null && error == null) return this; @@ -225,6 +225,16 @@ namespace Implab { return medium; } + public IPromiseBase Then(Action success,ErrorHandler error) + { + return Then(x => success(), error); + } + + public IPromiseBase Then(Action success) + { + return Then(success); + } + /// <summary> /// Adds new handlers to this promise. /// </summary> @@ -232,7 +242,7 @@ namespace Implab { /// This handler will recieve an operation result as a parameter.</param> /// <param name="error">Handles an exception that may occur during the operation and returns the value which will be used as the result of the operation.</param> /// <returns>The new promise chained to this one.</returns> - public Promise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { + public IPromise<T> Then(ResultHandler<T> success, ErrorHandler<T> error) { if (success == null && error == null) return this; @@ -266,7 +276,7 @@ namespace Implab { } - public Promise<T> Then(ResultHandler<T> success) { + public IPromise<T> Then(ResultHandler<T> success) { if (success == null) return this; @@ -287,8 +297,8 @@ namespace Implab { return medium; } - public Promise<T> Error(ErrorHandler error) { - return Then(null, error); + public IPromise<T> Error(ErrorHandler error) { + return Then((ResultHandler<T>)null, error); } /// <summary> @@ -299,7 +309,7 @@ namespace Implab { /// </remarks> /// <param name="handler">The error handler which returns the result of the promise.</param> /// <returns>New promise.</returns> - public Promise<T> Error(ErrorHandler<T> handler) { + public IPromise<T> Error(ErrorHandler<T> handler) { if (handler == null) return this; @@ -320,7 +330,7 @@ namespace Implab { return medium; } - public Promise<T> Anyway(Action handler) { + public IPromise<T> Anyway(Action handler) { if (handler == null) return this; @@ -358,7 +368,7 @@ namespace Implab { /// <param name="error">Обработчик ошибки. Данный обработчик получит /// исключение возникшее при выполнении операции.</param> /// <returns>Новое обещание, которое будет выполнено при выполнении исходного обещания.</returns> - public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { + public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper, ErrorHandler error) { if (mapper == null) throw new ArgumentNullException("mapper"); @@ -385,7 +395,7 @@ namespace Implab { return chained; } - public Promise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { + public IPromise<TNew> Map<TNew>(ResultMapper<T, TNew> mapper) { return Map(mapper, null); } @@ -399,7 +409,7 @@ namespace Implab { /// <param name="error">Обработчик ошибки. Данный обработчик получит /// исключение возникшее при выполнении текуещй операции.</param> /// <returns>Новое обещание, которое будет выполнено по окончанию указанной аснхронной операции.</returns> - public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { + public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained, ErrorHandler error) { // проблема в том, что на момент связывания еще не начата асинхронная операция, поэтому нужно // создать посредника, к которому будут подвызяваться следующие обработчики. @@ -437,11 +447,11 @@ namespace Implab { return medium; } - public Promise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { + public IPromise<TNew> Chain<TNew>(ChainedOperation<T, TNew> chained) { return Chain(chained, null); } - public Promise<T> Cancelled(Action handler) { + public IPromise<T> Cancelled(Action handler) { AddHandler(null, null, handler); return this; } @@ -451,7 +461,7 @@ namespace Implab { /// </summary> /// <param name="handler">The handler that will be called anyway</param> /// <returns>self</returns> - public Promise<T> Finally(Action handler) { + public IPromise<T> Finally(Action handler) { if (handler == null) throw new ArgumentNullException("handler"); AddHandler(