internal static Func <object, FutureOr> _registerUnaryHandler(Func <object, FutureOr> handler, Zone zone) { var callback = zone.registerUnaryCallback(arg => handler(arg)); return(arg => (FutureOr)callback(arg)); }
internal static Func <Exception, bool> _registerUnaryHandler(Func <Exception, bool> handler, Zone zone) { var callback = zone.registerUnaryCallback(arg => handler((Exception)arg)); return(arg => (bool)callback(arg)); }
internal static Func <FutureOr> _registerHandler(Func <FutureOr> handler, Zone zone) { var callback = zone.registerCallback(() => handler()); return(() => (FutureOr)callback()); }
internal static Func <object> _registerHandler(Func <object> handler, Zone zone) { var callback = zone.registerCallback(() => handler()); return(() => callback()); }
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; } }
internal _Future(Zone zone) { _zone = zone; }
internal _Future() { _zone = Zone.current; }