Example #1
0
        protected void SubscribeProgress(Action <float> onProgress, CancelationToken cancelationToken)
        {
            ValidateOperation(this, 2);
            ValidateArgument(onProgress, "onProgress", 2);

            if (_state == State.Pending)
            {
                InternalProtected.IProgressListener progressListener = InternalProtected.ProgressDelegate.GetOrCreate(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.ProgressDelegate.GetOrCreate(onProgress, this));
            }

            // Don't report progress if the promise is canceled or rejected.
        }
        /// <summary>
        /// Create a new <see cref="CancelationSource"/> that will be canceled either when you cancel it, or when the given token is canceled (with the same value), whichever is first.
        /// <para/>Note: the new <see cref="CancelationSource"/> still must be disposed when you are finished with it.
        /// </summary>
        /// <param name="token">The cancelation token to observe.</param>
        /// <returns>A new <see cref="CancelationSource"/> that is linked to the source token.</returns>
        public static CancelationSource New(CancelationToken token)
        {
            ValidateThreadAccess(1);
            CancelationSource newCancelationSource = New();

            token.MaybeLinkSourceInternal(newCancelationSource._ref);
            return(newCancelationSource);
        }
        /// <summary>
        /// Create a new <see cref="CancelationSource"/> that will be canceled either when you cancel it, or when any of the given tokens are canceled (with the same value), whichever is first.
        /// <para/>Note: the new <see cref="CancelationSource"/> still must be disposed when you are finished with it.
        /// </summary>
        /// <param name="token1">The first cancelation token to observe.</param>
        /// <param name="token2">The second cancelation token to observe.</param>
        /// <returns>A new <see cref="CancelationSource"/> that is linked to the source token.</returns>
        public static CancelationSource New(CancelationToken token1, CancelationToken token2)
        {
            ValidateThreadAccess(1);
            CancelationSource newCancelationSource = New(token1);

            if (!newCancelationSource._ref.IsCanceled)
            {
                token2.MaybeLinkSourceInternal(newCancelationSource._ref);
            }
            return(newCancelationSource);
        }
Example #4
0
 protected void Reset(Promise owner, CancelationToken cancelationToken)
 {
     _owner     = owner;
     _handling  = false;
     _done      = false;
     _suspended = false;
     _canceled  = false;
     _current   = default(UnsignedFixed32);
     SetCreatedStacktrace(this, 4);
     if (cancelationToken.CanBeCanceled)
     {
         _cancelationRegistration = cancelationToken.RegisterInternal(this);
     }
 }
            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);
            }
Example #6
0
                public static ProgressDelegateCapture <TCapture> GetOrCreate(TCapture capturedValue, Action <TCapture, float> onProgress, Promise owner, CancelationToken cancelationToken = default(CancelationToken))
                {
                    var progress = _pool.IsNotEmpty ? (ProgressDelegateCapture <TCapture>)_pool.Pop() : new ProgressDelegateCapture <TCapture>();

                    progress._capturedValue = capturedValue;
                    progress._onProgress    = onProgress;
                    progress.Reset(owner, cancelationToken);
                    return(progress);
                }
Example #7
0
                public static ProgressDelegate GetOrCreate(Action <float> onProgress, Promise owner, CancelationToken cancelationToken = default(CancelationToken))
                {
                    var progress = _pool.IsNotEmpty ? (ProgressDelegate)_pool.Pop() : new ProgressDelegate();

                    progress._onProgress = onProgress;
                    progress.Reset(owner, cancelationToken);
                    return(progress);
                }