Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit fa1e9ec

Browse files
committed
merge detectZone
1 parent b5d2e9e commit fa1e9ec

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

lib/zone.ts

+14-32
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,6 @@ const Zone: ZoneType = (function(global: any) {
16641664
case FrameType.transition:
16651665
if (zoneFrame.parent) {
16661666
// This is the special frame where zone changed. Print and process it accordingly
1667-
// frames[i] += ` [${zoneFrame.parent.zone.name} => ${zoneFrame.zone.name}]`;
16681667
zoneFrame = zoneFrame.parent;
16691668
} else {
16701669
zoneFrame = null;
@@ -1777,17 +1776,11 @@ const Zone: ZoneType = (function(global: any) {
17771776
});
17781777

17791778
// Now we need to populate the `blacklistedStackFrames` as well as find the
1780-
// run/runGuraded/runTask frames. This is done by creating a detect zone and then threading
1779+
// run/runGuarded/runTask frames. This is done by creating a detect zone and then threading
17811780
// the execution through all of the above methods so that we can look at the stack trace and
17821781
// find the frames of interest.
17831782
let detectZone: Zone = Zone.current.fork({
17841783
name: 'detect',
1785-
onInvoke: function(
1786-
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, delegate: Function,
1787-
applyThis: any, applyArgs: any[], source: string): any {
1788-
// Here only so that it will show up in the stack frame so that it can be black listed.
1789-
return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source);
1790-
},
17911784
onHandleError: function(parentZD: ZoneDelegate, current: Zone, target: Zone, error: any):
17921785
boolean {
17931786
if (error.originalStack && Error === ZoneAwareError) {
@@ -1836,8 +1829,7 @@ const Zone: ZoneType = (function(global: any) {
18361829
// carefully constructor a stack frame which contains all of the frames of interest which
18371830
// need to be detected and blacklisted.
18381831

1839-
const parentDetectSpec = {name: 'parent'};
1840-
const childDetectZone = Zone.current.fork(parentDetectSpec).fork({
1832+
const childDetectZone = detectZone.fork({
18411833
name: 'child',
18421834
onScheduleTask: function(delegate, curr, target, task) {
18431835
return delegate.scheduleTask(target, task);
@@ -1853,26 +1845,28 @@ const Zone: ZoneType = (function(global: any) {
18531845
}
18541846
});
18551847

1848+
// we need to detect all zone related frames, it will
1849+
// exceed default stackTraceLimit, so we set it to
1850+
// larger number here, and restore it after detect finish.
1851+
const originalStackTraceLimit = Error.stackTraceLimit;
18561852
Error.stackTraceLimit = 100;
1853+
// we schedule event/micro/macro task, and invoke them
1854+
// when onSchedule, so we can get all stack traces for
1855+
// all kinds of tasks with one error thrown.
18571856
childDetectZone.run(() => {
18581857
childDetectZone.runGuarded(() => {
18591858
const fakeTransitionTo =
18601859
(toState: TaskState, fromState1: TaskState, fromState2: TaskState) => {};
18611860
childDetectZone.scheduleEventTask(
1862-
'detect',
1861+
blacklistedStackFramesSymbol,
18631862
() => {
18641863
childDetectZone.scheduleMacroTask(
1865-
'detect',
1864+
blacklistedStackFramesSymbol,
18661865
() => {
18671866
childDetectZone.scheduleMicroTask(
1868-
'detect',
1867+
blacklistedStackFramesSymbol,
18691868
() => {
1870-
const error = new Error('detect');
1871-
const frames = error.stack.split('\n');
1872-
while (frames.length) {
1873-
let frame = frames.shift();
1874-
blackListedStackFrames[frame] = FrameType.blackList;
1875-
}
1869+
throw new (ZoneAwareError as any)(ZoneAwareError, NativeError);
18761870
},
18771871
null,
18781872
(t: Task) => {
@@ -1895,19 +1889,7 @@ const Zone: ZoneType = (function(global: any) {
18951889
() => {});
18961890
});
18971891
});
1898-
Error.stackTraceLimit = 15;
1899-
1900-
// carefully constructor a stack frame which contains all of the frames of interest which
1901-
// need to be detected and blacklisted.
1902-
let detectRunFn = () => {
1903-
detectZone.run(() => {
1904-
detectZone.runGuarded(() => {
1905-
throw new (ZoneAwareError as any)(ZoneAwareError, NativeError);
1906-
});
1907-
});
1908-
};
1909-
// Cause the error to extract the stack frames.
1910-
detectZone.runTask(detectZone.scheduleMacroTask('detect', detectRunFn, null, () => null, null));
1892+
Error.stackTraceLimit = originalStackTraceLimit;
19111893

19121894
return global['Zone'] = Zone;
19131895
})(typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global);

0 commit comments

Comments
 (0)