Esempio n. 1
0
        public IPromise <PromisedT> Finally(Action onComplete)
        {
            if (CurState == PromiseState.Resolved)
            {
                try
                {
                    onComplete();
                    return(this);
                }
                catch (Exception ex)
                {
                    return(Rejected(ex));
                }
            }

            var promise = new Promise <PromisedT>();

            promise.WithName(Name);

            this.Then((Action <PromisedT>)promise.Resolve);
            this.Catch(e => {
                try
                {
                    onComplete();
                    promise.Reject(e);
                }
                catch (Exception ne)
                {
                    promise.Reject(ne);
                }
            });

            return(promise.Then(v =>
            {
                onComplete();
                return v;
            }));
        }
Esempio n. 2
0
        /// <summary>
        /// Add a resolved callback, a rejected callback and a progress callback.
        /// The resolved callback chains a value promise (optionally converting to a different value type).
        /// </summary>
        public IPromise <ConvertedT> Then <ConvertedT>(
            Func <PromisedT, IPromise <ConvertedT> > onResolved,
            Func <Exception, IPromise <ConvertedT> > onRejected,
            Action <float> onProgress
            )
        {
            if (CurState == PromiseState.Resolved)
            {
                try
                {
                    return(onResolved(resolveValue));
                }
                catch (Exception ex)
                {
                    return(Promise <ConvertedT> .Rejected(ex));
                }
            }

            // This version of the function must supply an onResolved.
            // Otherwise there is now way to get the converted value to pass to the resulting promise.
            //            Argument.NotNull(() => onResolved);

            var resultPromise = new Promise <ConvertedT>();

            resultPromise.WithName(Name);

            Action <PromisedT> resolveHandler = v =>
            {
                onResolved(v)
                .Progress(progress => resultPromise.ReportProgress(progress))
                .Then(
                    // Should not be necessary to specify the arg type on the next line, but Unity (mono) has an internal compiler error otherwise.
                    chainedValue => resultPromise.Resolve(chainedValue),
                    ex => resultPromise.Reject(ex)
                    );
            };

            Action <Exception> rejectHandler = ex =>
            {
                if (onRejected == null)
                {
                    resultPromise.Reject(ex);
                    return;
                }

                try
                {
                    onRejected(ex)
                    .Then(
                        chainedValue => resultPromise.Resolve(chainedValue),
                        callbackEx => resultPromise.Reject(callbackEx)
                        );
                }
                catch (Exception callbackEx)
                {
                    resultPromise.Reject(callbackEx);
                }
            };

            ActionHandlers(resultPromise, resolveHandler, rejectHandler);
            if (onProgress != null)
            {
                ProgressHandlers(this, onProgress);
            }

            return(resultPromise);
        }
Esempio n. 3
0
        /// <summary>
        /// Add a resolved callback, a rejected callback and a progress callback.
        /// </summary>
        public IPromise Then(Action onResolved, Action <Exception> onRejected, Action <float> onProgress)
        {
            if (CurState == PromiseState.Resolved)
            {
                try
                {
                    onResolved();
                    return(this);
                }
                catch (Exception ex)
                {
                    return(Rejected(ex));
                }
            }

            var resultPromise = new Promise();

            resultPromise.WithName(Name);

            Action resolveHandler;

            if (onResolved != null)
            {
                resolveHandler = () =>
                {
                    onResolved();
                    resultPromise.Resolve();
                };
            }
            else
            {
                resolveHandler = resultPromise.Resolve;
            }

            Action <Exception> rejectHandler;

            if (onRejected != null)
            {
                rejectHandler = ex =>
                {
                    if (onRejected != null)
                    {
                        onRejected(ex);
                        resultPromise.Resolve();
                        return;
                    }

                    resultPromise.Reject(ex);
                };
            }
            else
            {
                rejectHandler = resultPromise.Reject;
            }

            ActionHandlers(resultPromise, resolveHandler, rejectHandler);
            if (onProgress != null)
            {
                ProgressHandlers(this, onProgress);
            }

            return(resultPromise);
        }