runZoned<R> функция
R runZoned<R>(Выполняет body в своей собственной зоне.
Если onError не равно null, зона считается зоной ошибок. Все неперехваченные ошибки, синхронные или асинхронные, в зоне перехватываются и обрабатываются обратным вызовом.
Ошибки никогда не могут пересекать границы зон ошибок. Это интуитивно понятно при выходе из зоны, но это также относится к ошибкам, которые могли бы войти в зону ошибок. Ошибки, пытающиеся пересечь границы зон ошибок, считаются неперехваченными.
var future = new Future.value(499);
runZoned(() {
future = future.then((_) { throw "error in first error-zone"; });
runZoned(() {
future = future.catchError((e) { print("Never reached!"); });
}, onError: (e) { print("unused error handler"); });
}, onError: (e) { print("catches error of first error-zone."); });
Пример:
runZoned(() {
new Future(() { throw "asynchronous error"; });
}, onError: print); // Will print "asynchronous error".
Исходный код
R runZoned<R>(R body(),
{Map zoneValues, ZoneSpecification zoneSpecification, Function onError}) {
HandleUncaughtErrorHandler errorHandler;
if (onError != null) {
errorHandler = (Zone self, ZoneDelegate parent, Zone zone, error,
StackTrace stackTrace) {
try {
// TODO(floitsch): the return type should be 'void'.
if (onError is ZoneBinaryCallback<dynamic, Object, StackTrace>) {
return self.parent.runBinary(onError, error, stackTrace);
}
return self.parent.runUnary(onError, error);
} catch (e, s) {
if (identical(e, error)) {
return parent.handleUncaughtError(zone, error, stackTrace);
} else {
return parent.handleUncaughtError(zone, e, s);
}
}
};
}
if (zoneSpecification == null) {
zoneSpecification =
new ZoneSpecification(handleUncaughtError: errorHandler);
} else if (errorHandler != null) {
zoneSpecification = new ZoneSpecification.from(zoneSpecification,
handleUncaughtError: errorHandler);
}
Zone zone = Zone.current
.fork(specification: zoneSpecification, zoneValues: zoneValues);
if (onError != null) {
return zone.runGuarded(body);
} else {
return zone.run(body);
}
}
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-async/runZoned.html