Пример #1
0
        internal void _addListener(_FutureListener listener)
        {
            D.assert(listener._nextListener == null);
            if (_mayAddListener)
            {
                listener._nextListener = (_FutureListener)_resultOrListeners;
                _resultOrListeners     = listener;
            }
            else
            {
                if (_isChained)
                {
                    // Delegate listeners to chained source future.
                    // If the source is complete, instead copy its values and
                    // drop the chaining.
                    _Future source = _chainSource;
                    if (!source._isComplete)
                    {
                        source._addListener(listener);
                        return;
                    }

                    _cloneResult(source);
                }

                D.assert(_isComplete);
                // Handle late listeners asynchronously.
                _zone.scheduleMicrotask(() => {
                    _propagateToListeners(this, listener);
                    return(null);
                });
            }
        }
Пример #2
0
        internal void _completeError(Exception error)
        {
            D.assert(!_isComplete);

            _FutureListener listeners = _removeListeners();

            _setError(error);
            _propagateToListeners(this, listeners);
        }
Пример #3
0
        internal void _completeWithValue(object value)
        {
            D.assert(!_isComplete);

            _FutureListener listeners = _removeListeners();

            _setValue(value);
            _propagateToListeners(this, listeners);
        }
Пример #4
0
        _FutureListener _removeListeners()
        {
            // Reverse listeners before returning them, so the resulting list is in
            // subscription order.
            D.assert(!_isComplete);
            _FutureListener current = (_FutureListener)_resultOrListeners;

            _resultOrListeners = null;
            return(_reverseListeners(current));
        }
Пример #5
0
        _FutureListener _reverseListeners(_FutureListener listeners)
        {
            _FutureListener prev    = null;
            _FutureListener current = listeners;

            while (current != null)
            {
                _FutureListener next = current._nextListener;
                current._nextListener = prev;
                prev    = current;
                current = next;
            }

            return(prev);
        }
Пример #6
0
        void _prependListeners(_FutureListener listeners)
        {
            if (listeners == null)
            {
                return;
            }
            if (_mayAddListener)
            {
                _FutureListener existingListeners = (_FutureListener)_resultOrListeners;
                _resultOrListeners = listeners;
                if (existingListeners != null)
                {
                    _FutureListener cursor = listeners;
                    while (cursor._nextListener != null)
                    {
                        cursor = cursor._nextListener;
                    }

                    cursor._nextListener = existingListeners;
                }
            }
            else
            {
                if (_isChained)
                {
                    // Delegate listeners to chained source future.
                    // If the source is complete, instead copy its values and
                    // drop the chaining.
                    _Future source = _chainSource;
                    if (!source._isComplete)
                    {
                        source._prependListeners(listeners);
                        return;
                    }

                    _cloneResult(source);
                }

                D.assert(_isComplete);
                listeners = _reverseListeners(listeners);
                _zone.scheduleMicrotask(() => {
                    _propagateToListeners(this, listeners);
                    return(null);
                });
            }
        }
Пример #7
0
 internal void _complete(FutureOr value = default)
 {
     D.assert(!_isComplete);
     if (value.isFuture)
     {
         if (value.f is _Future coreFuture)
         {
             _chainCoreFuture(coreFuture, this);
         }
         else
         {
             _chainForeignFuture(value.f, this);
         }
     }
     else
     {
         _FutureListener listeners = _removeListeners();
         _setValue(value.v);
         _propagateToListeners(this, listeners);
     }
 }
Пример #8
0
        static void _chainCoreFuture(_Future source, _Future target)
        {
            D.assert(target._mayAddListener); // Not completed, not already chained.
            while (source._isChained)
            {
                source = source._chainSource;
            }

            if (source._isComplete)
            {
                _FutureListener listeners = target._removeListeners();
                target._cloneResult(source);
                _propagateToListeners(target, listeners);
            }
            else
            {
                _FutureListener listeners = (_FutureListener)target._resultOrListeners;
                target._setChained(source);
                source._prependListeners(listeners);
            }
        }
Пример #9
0
        static void _propagateToListeners(_Future source, _FutureListener listeners)
        {
            while (true)
            {
                D.assert(source._isComplete);
                bool hasError = source._hasError;
                if (listeners == null)
                {
                    if (hasError)
                    {
                        AsyncError asyncError = source._error;
                        source._zone.handleUncaughtError(asyncError);
                    }

                    return;
                }

                // Usually futures only have one listener. If they have several, we
                // call handle them separately in recursive calls, continuing
                // here only when there is only one listener left.
                while (listeners._nextListener != null)
                {
                    _FutureListener currentListener = listeners;
                    listeners = currentListener._nextListener;
                    currentListener._nextListener = null;
                    _propagateToListeners(source, currentListener);
                }

                _FutureListener listener     = listeners;
                var             sourceResult = source._resultOrListeners;

                // Do the actual propagation.
                // Set initial state of listenerHasError and listenerValueOrError. These
                // variables are updated with the outcome of potential callbacks.
                // Non-error results, including futures, are stored in
                // listenerValueOrError and listenerHasError is set to false. Errors
                // are stored in listenerValueOrError as an [AsyncError] and
                // listenerHasError is set to true.
                bool listenerHasError     = hasError;
                var  listenerValueOrError = sourceResult;

                // Only if we either have an error or callbacks, go into this, somewhat
                // expensive, branch. Here we'll enter/leave the zone. Many futures
                // don't have callbacks, so this is a significant optimization.
                if (hasError || listener.handlesValue || listener.handlesComplete)
                {
                    Zone zone = listener._zone;
                    if (hasError && !source._zone.inSameErrorZone(zone))
                    {
                        // Don't cross zone boundaries with errors.
                        AsyncError asyncError = source._error;
                        source._zone.handleUncaughtError(asyncError);
                        return;
                    }

                    Zone oldZone = null;
                    if (!ReferenceEquals(Zone.current, zone))
                    {
                        // Change zone if it's not current.
                        oldZone = Zone._enter(zone);
                    }

                    // These callbacks are abstracted to isolate the try/catch blocks
                    // from the rest of the code to work around a V8 glass jaw.
                    Action handleWhenCompleteCallback = () => {
                        // The whenComplete-handler is not combined with normal value/error
                        // handling. This means at most one handleX method is called per
                        // listener.
                        D.assert(!listener.handlesValue);
                        D.assert(!listener.handlesError);
                        FutureOr completeResult;
                        try {
                            completeResult = listener.handleWhenComplete();
                        }
                        catch (Exception e) {
                            if (hasError && ReferenceEquals(source._error.InnerException, e))
                            {
                                listenerValueOrError = source._error;
                            }
                            else
                            {
                                listenerValueOrError = new AsyncError(e);
                            }

                            listenerHasError = true;
                            return;
                        }

                        if (completeResult.isFuture)
                        {
                            var completeResultFuture = completeResult.f;
                            if (completeResultFuture is _Future completeResultCoreFuture &&
                                completeResultCoreFuture._isComplete)
                            {
                                if (completeResultCoreFuture._hasError)
                                {
                                    listenerValueOrError = completeResultCoreFuture._error;
                                    listenerHasError     = true;
                                }

                                // Otherwise use the existing result of source.
                                return;
                            }

                            // We have to wait for the completeResult future to complete
                            // before knowing if it's an error or we should use the result
                            // of source.
                            var originalSource = source;
                            listenerValueOrError =
                                completeResultFuture.then((_) => FutureOr.future(originalSource));
                            listenerHasError = false;
                        }
                    };

                    Action handleValueCallback = () => {
                        try {
                            listenerValueOrError = listener.handleValue(sourceResult);
                        }
                        catch (Exception e) {
                            listenerValueOrError = new AsyncError(e);
                            listenerHasError     = true;
                        }
                    };

                    Action handleError = () => {
                        try {
                            AsyncError asyncError = source._error;
                            if (listener.matchesErrorTest(asyncError) &&
                                listener.hasErrorCallback)
                            {
                                listenerValueOrError = listener.handleError(asyncError);
                                listenerHasError     = false;
                            }
                        }
                        catch (Exception e) {
                            if (ReferenceEquals(source._error.InnerException, e))
                            {
                                listenerValueOrError = source._error;
                            }
                            else
                            {
                                listenerValueOrError = new AsyncError(e);
                            }

                            listenerHasError = true;
                        }
                    };

                    if (listener.handlesComplete)
                    {
                        handleWhenCompleteCallback();
                    }
                    else if (!hasError)
                    {
                        if (listener.handlesValue)
                        {
                            handleValueCallback();
                        }
                    }
                    else
                    {
                        if (listener.handlesError)
                        {
                            handleError();
                        }
                    }

                    // If we changed zone, oldZone will not be null.
                    if (oldZone != null)
                    {
                        Zone._leave(oldZone);
                    }

                    if (listenerValueOrError is FutureOr futureOr)
                    {
                        listenerValueOrError = futureOr.isFuture ? futureOr.f : futureOr.v;
                    }

                    // If the listener's value is a future we need to chain it. Note that
                    // this can only happen if there is a callback.
                    if (listenerValueOrError is Future chainSource)
                    {
                        // Shortcut if the chain-source is already completed. Just continue
                        // the loop.
                        _Future listenerResult = listener.result;
                        if (chainSource is _Future chainSourceCore)
                        {
                            if (chainSourceCore._isComplete)
                            {
                                listeners = listenerResult._removeListeners();
                                listenerResult._cloneResult(chainSourceCore);
                                source = chainSourceCore;
                                continue;
                            }
                            else
                            {
                                _chainCoreFuture(chainSourceCore, listenerResult);
                            }
                        }
                        else
                        {
                            _chainForeignFuture(chainSource, listenerResult);
                        }

                        return;
                    }
                }

                _Future result = listener.result;
                listeners = result._removeListeners();
                if (!listenerHasError)
                {
                    result._setValue(listenerValueOrError);
                }
                else
                {
                    AsyncError asyncError = (AsyncError)listenerValueOrError;
                    result._setErrorObject(asyncError);
                }

                // Prepare for next round.
                source = result;
            }
        }