OLD | NEW |
1 library catch_errors; | 1 library catch_errors; |
2 | 2 |
3 import 'dart:async'; | 3 import 'dart:async'; |
4 | 4 |
5 Stream catchErrors(void body()) { | 5 Stream catchErrors(void body()) { |
6 StreamController controller; | 6 StreamController controller; |
7 | 7 |
8 bool onError(e, st) { | 8 bool onError(e, st) { |
9 controller.add(e); | 9 controller.add(e); |
10 return true; | 10 return true; |
11 } | 11 } |
12 | 12 |
13 void onListen() { | 13 void onListen() { |
14 runZoned(body, onError: onError); | 14 runZoned(body, onError: onError); |
15 } | 15 } |
16 | 16 |
17 controller = new StreamController(onListen: onListen); | 17 controller = new StreamController(onListen: onListen); |
18 return controller.stream; | 18 return controller.stream; |
19 } | 19 } |
20 | 20 |
21 runZonedScheduleMicrotask(body(), | 21 runZonedScheduleMicrotask(body(), |
22 { void onScheduleMicrotask(void callback()), | 22 {void onScheduleMicrotask(void callback()), Function onError}) { |
23 Function onError }) { | |
24 if (onScheduleMicrotask == null) { | 23 if (onScheduleMicrotask == null) { |
25 return runZoned(body, onError: onError); | 24 return runZoned(body, onError: onError); |
26 } | 25 } |
27 HandleUncaughtErrorHandler errorHandler; | 26 HandleUncaughtErrorHandler errorHandler; |
28 if (onError != null) { | 27 if (onError != null) { |
29 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, | 28 errorHandler = (Zone self, ZoneDelegate parent, Zone zone, error, |
30 error, StackTrace stackTrace) { | 29 StackTrace stackTrace) { |
31 try { | 30 try { |
32 return self.parent.runUnary(onError, error); | 31 return self.parent.runUnary(onError, error); |
33 } catch(e, s) { | 32 } catch (e, s) { |
34 if (identical(e, error)) { | 33 if (identical(e, error)) { |
35 return parent.handleUncaughtError(zone, error, stackTrace); | 34 return parent.handleUncaughtError(zone, error, stackTrace); |
36 } else { | 35 } else { |
37 return parent.handleUncaughtError(zone, e, s); | 36 return parent.handleUncaughtError(zone, e, s); |
38 } | 37 } |
39 } | 38 } |
40 }; | 39 }; |
41 } | 40 } |
42 ScheduleMicrotaskHandler asyncHandler; | 41 ScheduleMicrotaskHandler asyncHandler; |
43 if (onScheduleMicrotask != null) { | 42 if (onScheduleMicrotask != null) { |
44 asyncHandler = (Zone self, ZoneDelegate parent, Zone zone, f()) { | 43 asyncHandler = (Zone self, ZoneDelegate parent, Zone zone, f()) { |
45 self.parent.runUnary(onScheduleMicrotask, () => zone.runGuarded(f)); | 44 self.parent.runUnary(onScheduleMicrotask, () => zone.runGuarded(f)); |
46 }; | 45 }; |
47 } | 46 } |
48 ZoneSpecification specification = | 47 ZoneSpecification specification = new ZoneSpecification( |
49 new ZoneSpecification(handleUncaughtError: errorHandler, | 48 handleUncaughtError: errorHandler, scheduleMicrotask: asyncHandler); |
50 scheduleMicrotask: asyncHandler); | |
51 Zone zone = Zone.current.fork(specification: specification); | 49 Zone zone = Zone.current.fork(specification: specification); |
52 if (onError != null) { | 50 if (onError != null) { |
53 return zone.runGuarded(body); | 51 return zone.runGuarded(body); |
54 } else { | 52 } else { |
55 return zone.run(body); | 53 return zone.run(body); |
56 } | 54 } |
57 } | 55 } |
OLD | NEW |