Beispiel #1
0
        /// <summary>
        /// Resolves when any in IPromise array is resolved
        /// Fails if all in IPromise array are failed
        /// </summary>
        /// <param name="collection">promises to complete</param>
        /// <returns>IPromise</returns>
        public static IPromise Race(params IPromise[] collection)
        {
            Deferred deferred = Deferred.GetFromPool();

            if (collection.Length == 0)
            {
                deferred.Reject(new Exception("Deferred.Race called with empty array - no winner"));
            }
            else
            {
                int promisesToWait = collection.Length;

                for (int i = 0, maxi = collection.Length - 1; i <= maxi; i++)
                {
                    collection[i].Done(() =>
                    {
                        if (deferred.CurrentState == States.Pending)
                        {
                            deferred.Resolve();
                        }
                    });

                    collection[i].Fail(ex =>
                    {
                        promisesToWait--;
                        if (deferred.CurrentState == States.Pending && promisesToWait == 0)
                        {
                            deferred.Reject(ex);
                        }
                    });
                }
            }

            return(deferred);
        }
Beispiel #2
0
        private static IPromise AllInternal(ICollection <IPromise> collection)
        {
            Deferred deferred = Deferred.GetFromPool();

            if (collection.Count == 0)
            {
                deferred.Resolve();
            }
            else
            {
                int promisesToComplete = collection.Count;

                foreach (IPromise element in collection)
                {
                    element.Done(() =>
                    {
                        promisesToComplete--;
                        if (deferred.CurrentState == States.Pending && promisesToComplete == 0)
                        {
                            deferred.Resolve();
                        }
                    });

                    element.Fail(ex =>
                    {
                        if (deferred.CurrentState == States.Pending)
                        {
                            deferred.Reject(ex);
                        }
                    });
                }
            }

            return(deferred);
        }
Beispiel #3
0
        /// <summary>
        /// Starts all IPromise constructors step-by-step until one of IPromise failed.
        /// Resolved with last IPromise
        /// Fails if any of IPromise failed
        /// </summary>
        /// <param name="collection">promises contructor array</param>
        /// <returns>IPromise</returns>
        public static IPromise Sequence(params Func <IPromise>[] collection)
        {
            IPromise last = Deferred.GetFromPool().Resolve();

            for (int i = 0, maxi = collection.Length - 1; i <= maxi; i++)
            {
                last = last.Then(collection[i]);
            }

            return(last);
        }
        public IPromise Wait(float seconds, Action <float> progressCallback = null)
        {
            Deferred deferred = Deferred.GetFromPool();

            awaiters.Add(new Awaiter
            {
                duration            = seconds,
                finishTime          = Time.time + seconds,
                unscaledTime        = false,
                additionalCondition = null,
                progressCallback    = progressCallback,
                resolver            = deferred,
            });
            return(deferred);
        }
        public IPromise WaitForTrue(Func <bool> condition)
        {
            Deferred deferred = Deferred.GetFromPool();

            awaiters.Add(new Awaiter
            {
                duration            = 0f,
                finishTime          = Time.unscaledTime,
                unscaledTime        = true,
                additionalCondition = condition,
                progressCallback    = null,
                resolver            = deferred,
            });
            return(deferred);
        }
Beispiel #6
0
        /// <summary>
        /// adds new promise constructor to current promise and returns wrapper that resolves with added promise
        /// A.Then(B) - returns promise that resolves when promise from B is resolved
        /// </summary>
        /// <param name="next">constructor for next promise</param>
        /// <returns>Promise</returns>
        public IPromise Then(Func <IPromise> next)
        {
            Deferred deferred = Deferred.GetFromPool();

            Done(() =>
            {
                IPromise promise = next();

                promise.Done(() => deferred.Resolve());
                promise.Fail(ex => deferred.Reject(ex));
            });

            Fail(ex => deferred.Reject(ex));

            return(deferred);
        }