private static void SubscribeProgressToChain(Promise promise, Internal.IProgressListener progressListener, ref ValueLinkedStack <Internal.PromisePassThrough> passThroughs)
        {
            Promise next;

            // If the promise is not waiting on another promise (is the root), it sets next to null, does not add the listener, and returns false.
            // If the promise is waiting on another promise that is not its previous, it adds the listener, transforms progresslistener, sets next to the one it's waiting on, and returns true.
            // Otherwise, it sets next to its previous, adds the listener only if it is a WaitPromise, and returns true.
            while (promise.SubscribeProgressIfWaiterAndContinueLoop(ref progressListener, out next, ref passThroughs))
            {
                promise = next;
            }

            // promise is the root of the promise tree.
            switch (promise._state)
            {
            case State.Pending:
            {
                progressListener.SetInitialAmount(promise._waitDepthAndProgress);
                break;
            }

            case State.Resolved:
            {
                progressListener.SetInitialAmount(promise._waitDepthAndProgress.GetIncrementedWholeTruncated());
                break;
            }

            default:     // Rejected or Canceled:
            {
                progressListener.Retain();
                progressListener.CancelOrIncrementProgress(promise, promise._waitDepthAndProgress.GetIncrementedWholeTruncated().ToUInt32());
                break;
            }
            }
        }
        protected void SubscribeProgress <TCapture>(TCapture capturedValue, Action <TCapture, float> onProgress, int skipFrames)
        {
            ValidateOperation(this, skipFrames + 1);
            ValidateArgument(onProgress, "onProgress", skipFrames + 1);

            if (_state == State.Pending)
            {
                Internal.IProgressListener progressListener = Internal.ProgressDelegateCapture <TCapture> .GetOrCreate(capturedValue, onProgress, this, skipFrames + 1);

                // Directly add to listeners for this promise.
                // Sets promise to the one this is waiting on. Returns false if not waiting on another promise.
                Promise promise;
                if (!SubscribeProgressAndContinueLoop(ref progressListener, out promise))
                {
                    // This is the root of the promise tree.
                    progressListener.SetInitialAmount(_waitDepthAndProgress);
                    return;
                }

                SubscribeProgressToBranchesAndRoots(promise, progressListener);
            }
            else if (_state == State.Resolved)
            {
                AddToHandleQueueBack(Internal.ProgressDelegateCapture <TCapture> .GetOrCreate(capturedValue, onProgress, this, skipFrames + 1));
            }

            // Don't report progress if the promise is canceled or rejected.
        }
        private static void SubscribeProgressToBranchesAndRoots(Promise promise, Internal.IProgressListener progressListener)
        {
            // This allows us to subscribe progress to AllPromises and RacePromises iteratively instead of recursively
            ValueLinkedStack <Internal.PromisePassThrough> passThroughs = new ValueLinkedStack <Internal.PromisePassThrough>();

Repeat:
            SubscribeProgressToChain(promise, progressListener, ref passThroughs);

            if (passThroughs.IsNotEmpty)
            {
                // passThroughs are removed from their targets before adding to passThroughs. Add them back here.
                var passThrough = passThroughs.Pop();
                promise          = passThrough.Owner;
                progressListener = passThrough;
                passThrough.Target.ReAdd(passThrough);
                goto Repeat;
            }
        }
 protected virtual bool SubscribeProgressIfWaiterAndContinueLoop(ref Internal.IProgressListener progressListener, out Promise previous, ref ValueLinkedStack <Internal.PromisePassThrough> passThroughs)
 {
     return((previous = _valueOrPrevious as Promise) != null);
 }
 protected virtual bool SubscribeProgressAndContinueLoop(ref Internal.IProgressListener progressListener, out Promise previous)
 {
     progressListener.Retain();
     _progressListeners.Push(progressListener);
     return((previous = _valueOrPrevious as Promise) != null);
 }