@@ -6,25 +6,36 @@ library catch_errors;
66
77import 'dart:async' ;
88
9- Stream catchErrors (dynamic body ()) {
10- late StreamController controller;
11-
12- bool onError (e, st) {
13- controller.add (e);
14- return true ;
15- }
16-
17- void onListen () {
18- runZonedGuarded (body, onError);
19- }
20-
21- controller = new StreamController (onListen: onListen);
9+ /// Runs [body] inside [runZonedGuarded] .
10+ ///
11+ /// Runs [body] when the returned stream is listened to.
12+ /// Emits all errors, synchronous and asynchronous,
13+ /// as events on the returned stream.
14+ ///
15+ /// **Notice**: The stream never closes. The caller should stop
16+ /// listening when they're convinced there will be no later error events.
17+ Stream <Object > catchErrors (void Function () body) {
18+ var controller = StreamController <Object >();
19+ controller.onListen = () {
20+ runZonedGuarded (body, (e, s) {
21+ controller.add (e);
22+ });
23+ };
2224 return controller.stream;
2325}
2426
25- runZonedScheduleMicrotask (
26- body (), {
27- void onScheduleMicrotask (void callback ())? ,
27+ /// Runs [body] inside [runZoneGuarded] , [Zone.runGuarded] or [Zone.run] .
28+ ///
29+ /// If [onScheduleMicrotask] is provided, a custom zone is created
30+ /// with a [Zone.scheduleMicrotask] which calls [onScheduleMicrotask] with
31+ /// the provided callback.
32+ ///
33+ /// If [onError] is provided, it's used as argument to [runZonedGuarded]
34+ /// or as error handler of the custom zone, and the `body` is then run
35+ /// using `runGuarded` .
36+ R ? runZonedScheduleMicrotask <R >(
37+ R body (), {
38+ void Function (void Function () callback)? onScheduleMicrotask,
2839 Function ? onError,
2940}) {
3041 if (onScheduleMicrotask == null ) {
@@ -56,18 +67,20 @@ runZonedScheduleMicrotask(
5667 }
5768 ScheduleMicrotaskHandler ? asyncHandler;
5869 if (onScheduleMicrotask != null ) {
59- asyncHandler = (Zone self, ZoneDelegate parent, Zone zone, f () ) {
70+ void handle (Zone self, ZoneDelegate parent, Zone zone, Function () f ) {
6071 self.parent! .runUnary (onScheduleMicrotask, () => zone.runGuarded (f));
61- };
72+ }
73+
74+ asyncHandler = handle;
6275 }
63- ZoneSpecification specification = new ZoneSpecification (
76+ ZoneSpecification specification = ZoneSpecification (
6477 handleUncaughtError: errorHandler,
6578 scheduleMicrotask: asyncHandler,
6679 );
67- Zone zone = Zone .current.fork (specification: specification);
6880 if (onError != null ) {
69- return zone. runGuarded (body);
81+ return runZoned < R > (body, zoneSpecification : specification );
7082 } else {
71- return zone.run (body);
83+ Zone zone = Zone .current.fork (specification: specification);
84+ return zone.run <R >(body);
7285 }
7386}
0 commit comments