Пример #1
0
        protected void SubscribeProgress <TCapture>(TCapture capturedValue, Action <TCapture, float> onProgress, CancelationToken cancelationToken)
        {
            ValidateOperation(this, 2);
            ValidateArgument(onProgress, "onProgress", 2);

            if (_state == State.Pending)
            {
                InternalProtected.IProgressListener progressListener = InternalProtected.ProgressDelegateCapture <TCapture> .GetOrCreate(capturedValue, onProgress, this, cancelationToken);

                // 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.SetInitialProgress(_waitDepthAndProgress);
                    return;
                }

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

            // Don't report progress if the promise is canceled or rejected.
        }
Пример #2
0
            public static Promise CreateSequence <TEnumerator>(TEnumerator promiseFuncs, CancelationToken cancelationToken = default(CancelationToken)) where TEnumerator : IEnumerator <Func <Promise> >
            {
                ValidateArgument(promiseFuncs, "promiseFuncs", 2);

                if (!promiseFuncs.MoveNext())
                {
                    return(Resolved());
                }

                // Invoke funcs async and normalize the progress.
                Promise rootPromise;

                if (cancelationToken.CanBeCanceled)
                {
                    var newPromise = PromiseResolvePromise <DelegateVoidPromiseCancel> .GetOrCreate();

                    newPromise.resolver = new DelegateVoidPromiseCancel(promiseFuncs.Current);
                    newPromise.resolver.cancelationRegistration = cancelationToken.RegisterInternal(newPromise);
                    // Set resolved value only if cancelation token wasn't already canceled (_valueOrPrevious will be a cancel value from being invoked synchronously).
                    if (newPromise._valueOrPrevious == null)
                    {
                        newPromise._valueOrPrevious = Internal.ResolveContainerVoid.GetOrCreate();
                    }
                    rootPromise = newPromise;
                }
                else
                {
                    var newPromise = PromiseResolvePromise <DelegateVoidPromise> .GetOrCreate();

                    newPromise.resolver         = new DelegateVoidPromise(promiseFuncs.Current);
                    newPromise._valueOrPrevious = Internal.ResolveContainerVoid.GetOrCreate();
                    rootPromise = newPromise;
                }
                rootPromise.ResetDepth();

                Promise promise = rootPromise;

                while (promiseFuncs.MoveNext())
                {
                    promise = promise.Then(promiseFuncs.Current, cancelationToken);
                }
                Internal.AddToHandleQueueBack(rootPromise); return(promise);
            }