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

Commit

Permalink
merge detectZone
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Mar 23, 2017
1 parent b5d2e9e commit aec8cff
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1777,17 +1777,11 @@ const Zone: ZoneType = (function(global: any) {
});

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

const parentDetectSpec = {name: 'parent'};
const childDetectZone = Zone.current.fork(parentDetectSpec).fork({
const childDetectZone = detectZone.fork({
name: 'child',
onScheduleTask: function(delegate, curr, target, task) {
return delegate.scheduleTask(target, task);
Expand All @@ -1853,26 +1846,28 @@ const Zone: ZoneType = (function(global: any) {
}
});

// we need to detect all zone related frames, it will
// exceed default stackTraceLimit, so we set it to
// larger number here, and restore it after detect finish.
const originalStackTraceLimit = Error.stackTraceLimit;
Error.stackTraceLimit = 100;
// we schedule event/micro/macro task, and invoke them
// when onSchedule, so we can get all stack traces for
// all kinds of tasks with one error thrown.
childDetectZone.run(() => {
childDetectZone.runGuarded(() => {
const fakeTransitionTo =
(toState: TaskState, fromState1: TaskState, fromState2: TaskState) => {};
childDetectZone.scheduleEventTask(
'detect',
blacklistedStackFramesSymbol,
() => {
childDetectZone.scheduleMacroTask(
'detect',
blacklistedStackFramesSymbol,
() => {
childDetectZone.scheduleMicroTask(
'detect',
blacklistedStackFramesSymbol,
() => {
const error = new Error('detect');
const frames = error.stack.split('\n');
while (frames.length) {
let frame = frames.shift();
blackListedStackFrames[frame] = FrameType.blackList;
}
throw new (ZoneAwareError as any)(ZoneAwareError, NativeError);
},
null,
(t: Task) => {
Expand All @@ -1895,19 +1890,7 @@ const Zone: ZoneType = (function(global: any) {
() => {});
});
});
Error.stackTraceLimit = 15;

// carefully constructor a stack frame which contains all of the frames of interest which
// need to be detected and blacklisted.
let detectRunFn = () => {
detectZone.run(() => {
detectZone.runGuarded(() => {
throw new (ZoneAwareError as any)(ZoneAwareError, NativeError);
});
});
};
// Cause the error to extract the stack frames.
detectZone.runTask(detectZone.scheduleMacroTask('detect', detectRunFn, null, () => null, null));
Error.stackTraceLimit = originalStackTraceLimit;

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

0 comments on commit aec8cff

Please sign in to comment.