Tighten types on error stream handler to avoid dynamic calls.

Make a private field typed to avoid an unnecessary dynamic call in the error handler for a stream.  Also remove an unused private member found in passing.

Change-Id: I29ebba5a3818a4c24df436e504d159731fd6531e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241243
Reviewed-by: Lasse Nielsen <lrn@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
This commit is contained in:
Leaf Petersen 2022-05-07 00:24:21 +00:00 committed by Commit Bot
parent 9a8fc2ae92
commit 240719eb86
4 changed files with 11 additions and 21 deletions

View file

@ -31,17 +31,3 @@ class AsyncError implements Error {
String toString() => '$error';
}
// Helper function used by stream method implementations.
_invokeErrorHandler(
Function errorHandler, Object error, StackTrace stackTrace) {
var handler = errorHandler; // Rename to avoid promotion.
if (handler is ZoneBinaryCallback<dynamic, Never, Never>) {
// Dynamic invocation because we don't know the actual type of the
// first argument or the error object, but we should successfully call
// the handler if they match up.
return errorHandler(error, stackTrace);
} else {
return errorHandler(error);
}
}

View file

@ -870,15 +870,21 @@ abstract class Stream<T> {
/// // 4
/// ```
Stream<T> handleError(Function onError, {bool test(error)?}) {
if (onError is! void Function(Object, StackTrace) &&
onError is! void Function(Object)) {
final void Function(Object, StackTrace) callback;
if (onError is void Function(Object, StackTrace)) {
callback = onError;
} else if (onError is void Function(Object)) {
callback = (Object error, StackTrace _) {
onError(error);
};
} else {
throw ArgumentError.value(
onError,
"onError",
"Error handler must accept one Object or one Object and a StackTrace"
" as arguments.");
}
return new _HandleErrorStream<T>(this, onError, test);
return new _HandleErrorStream<T>(this, callback, test);
}
/// Transforms each element of this stream into a sequence of elements.

View file

@ -243,7 +243,7 @@ class _ExpandStream<S, T> extends _ForwardingStream<S, T> {
/// A stream pipe that converts or disposes error events
/// before passing them on.
class _HandleErrorStream<T> extends _ForwardingStream<T, T> {
final Function _onError;
final void Function(Object, StackTrace) _onError;
final bool Function(Object)? _test;
_HandleErrorStream(Stream<T> source, this._onError, this._test)
@ -266,7 +266,7 @@ class _HandleErrorStream<T> extends _ForwardingStream<T, T> {
}
if (matches) {
try {
_invokeErrorHandler(_onError, error, stackTrace);
_onError(error, stackTrace);
} catch (e, s) {
if (identical(e, error)) {
sink._addError(error, stackTrace);

View file

@ -186,8 +186,6 @@ class CastSet<S, T> extends _CastIterableBase<S, T> implements Set<T> {
CastSet(this._source, this._emptySet);
static Set<R> _defaultEmptySet<R>() => new Set<R>();
Set<R> cast<R>() => new CastSet<S, R>(_source, _emptySet);
bool add(T value) => _source.add(value as S);