Skip to content

Commit 8637253

Browse files
JiaLiPassionalxhub
authored andcommitted
refactor(zone.js): remove zone-async-tagging from zone.js (#47416)
1. Remove `zone-async-tagging` implementation from zone.js and move the implementation to `@angular/core`, so `@angular/core` can import this package more easily for better treeshaking. 2. Add `async tagging zone` implemenation into `@angular/core` package. So we don't need to get the `AsyncStackTaggingZoneSpec` from `global` instance, we can import the `class` directly for better treeshaking. 3. Only load this ZoneSpec when `ngDevMode` is `true`. PR Close #47416
1 parent 291a5b3 commit 8637253

File tree

15 files changed

+65
-93
lines changed

15 files changed

+65
-93
lines changed

goldens/size-tracking/integration-payloads.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"cli-hello-world-lazy": {
3434
"uncompressed": {
3535
"runtime": 2835,
36-
"main": 226893,
36+
"main": 226324,
3737
"polyfills": 33842,
3838
"src_app_lazy_lazy_routes_ts": 487
3939
}

packages/zone.js/lib/zone-spec/async-stack-tagging.ts renamed to packages/core/src/zone/async-stack-tagging.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,39 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
interface Console {
9+
interface ConsoleWithAsyncTagging {
1010
createTask(name: string): ConsoleTask;
1111
}
1212

1313
interface ConsoleTask {
1414
run<T>(f: () => T): T;
1515
}
1616

17-
interface Task {
17+
interface ZoneConsoleTask extends Task {
1818
consoleTask?: ConsoleTask;
1919
}
2020

21-
class AsyncStackTaggingZoneSpec implements ZoneSpec {
22-
createTask: Console['createTask'];
21+
export class AsyncStackTaggingZoneSpec implements ZoneSpec {
22+
createTask: ConsoleWithAsyncTagging['createTask'];
2323

24-
constructor(namePrefix: string, consoleAsyncStackTaggingImpl: Console = console) {
24+
constructor(
25+
namePrefix: string, consoleAsyncStackTaggingImpl: ConsoleWithAsyncTagging = console as any) {
2526
this.name = 'asyncStackTagging for ' + namePrefix;
26-
this.createTask = consoleAsyncStackTaggingImpl?.createTask ?? (() => {});
27+
this.createTask = consoleAsyncStackTaggingImpl?.createTask ?? (() => null);
2728
}
2829

2930
// ZoneSpec implementation below.
30-
3131
name: string;
3232

33-
onScheduleTask(delegate: ZoneDelegate, _current: Zone, target: Zone, task: Task): Task {
33+
onScheduleTask(delegate: ZoneDelegate, _current: Zone, target: Zone, task: ZoneConsoleTask):
34+
Task {
3435
task.consoleTask = this.createTask(`Zone - ${task.source || task.type}`);
3536
return delegate.scheduleTask(target, task);
3637
}
3738

3839
onInvokeTask(
39-
delegate: ZoneDelegate, _currentZone: Zone, targetZone: Zone, task: Task, applyThis: any,
40-
applyArgs?: any[]) {
40+
delegate: ZoneDelegate, _currentZone: Zone, targetZone: Zone, task: ZoneConsoleTask,
41+
applyThis: any, applyArgs?: any[]) {
4142
let ret;
4243
if (task.consoleTask) {
4344
ret = task.consoleTask.run(() => delegate.invokeTask(targetZone, task, applyThis, applyArgs));
@@ -47,7 +48,3 @@ class AsyncStackTaggingZoneSpec implements ZoneSpec {
4748
return ret;
4849
}
4950
}
50-
51-
// Export the class so that new instances can be created with proper
52-
// constructor params.
53-
(Zone as any)['AsyncStackTaggingZoneSpec'] = AsyncStackTaggingZoneSpec;

packages/core/src/zone/ng_zone.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {global} from '../util/global';
1212
import {noop} from '../util/noop';
1313
import {getNativeRequestAnimationFrame} from '../util/raf';
1414

15+
import {AsyncStackTaggingZoneSpec} from './async-stack-tagging';
1516

1617
/**
1718
* An injectable service for executing work inside or outside of the Angular zone.
@@ -138,8 +139,12 @@ export class NgZone {
138139

139140
self._outer = self._inner = Zone.current;
140141

141-
if ((Zone as any)['AsyncStackTaggingZoneSpec']) {
142-
const AsyncStackTaggingZoneSpec = (Zone as any)['AsyncStackTaggingZoneSpec'];
142+
// AsyncStackTaggingZoneSpec provides `linked stack traces` to show
143+
// where the async operation is scheduled. For more details, refer
144+
// to this article, https://developer.chrome.com/blog/devtools-better-angular-debugging/
145+
// And we only import this AsyncStackTaggingZoneSpec in development mode,
146+
// in the production mode, the AsyncStackTaggingZoneSpec will be tree shaken away.
147+
if (ngDevMode) {
143148
self._inner = self._inner.fork(new AsyncStackTaggingZoneSpec('Angular'));
144149
}
145150

packages/zone.js/test/zone-spec/async-tagging-console.spec.ts renamed to packages/core/test/zone/async-tagging-console.spec.ts

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
9-
import {ifEnvSupports, ifEnvSupportsWithDone} from '../test-util';
8+
import {AsyncStackTaggingZoneSpec} from '../../src/zone/async-stack-tagging';
109

1110
describe('AsyncTaggingConsoleTest', () => {
12-
const AsyncStackTaggingZoneSpec = (Zone as any)['AsyncStackTaggingZoneSpec'];
13-
1411
describe('should call console async stack tagging API', () => {
1512
const startAsyncTaskSpy = jasmine.createSpy('startAsyncTask');
1613
const finishAsyncTaskSpy = jasmine.createSpy('finishAsyncTask');
@@ -91,45 +88,51 @@ describe('AsyncTaggingConsoleTest', () => {
9188
});
9289
});
9390

94-
it('XMLHttpRequest', ifEnvSupportsWithDone('XMLHttpRequest', (done: DoneFn) => {
95-
asyncStackTaggingZone.run(() => {
96-
const req = new XMLHttpRequest();
97-
req.onload = () => {
98-
Zone.root.run(() => {
99-
setTimeout(() => {
100-
expect(scheduleAsyncTaskSpy.calls.all()[0].args).toEqual([
101-
'Zone - XMLHttpRequest.addEventListener:load',
102-
]);
103-
expect(scheduleAsyncTaskSpy.calls.all()[1].args).toEqual([
104-
'Zone - XMLHttpRequest.send',
105-
]);
106-
expect(startAsyncTaskSpy.calls.count()).toBe(2);
107-
expect(finishAsyncTaskSpy.calls.count()).toBe(2);
108-
done();
109-
});
110-
});
111-
};
112-
req.open('get', '/', true);
113-
req.send();
114-
});
115-
}));
91+
if (global.XMLHttpRequest) {
92+
it('XMLHttpRequest', (done: DoneFn) => {
93+
asyncStackTaggingZone.run(() => {
94+
const req = new XMLHttpRequest();
95+
req.onload = () => {
96+
Zone.root.run(() => {
97+
setTimeout(() => {
98+
expect(scheduleAsyncTaskSpy.calls.all()[0].args).toEqual([
99+
'Zone - XMLHttpRequest.addEventListener:load',
100+
]);
101+
expect(scheduleAsyncTaskSpy.calls.all()[1].args).toEqual([
102+
'Zone - XMLHttpRequest.send',
103+
]);
104+
expect(startAsyncTaskSpy.calls.count()).toBe(2);
105+
expect(finishAsyncTaskSpy.calls.count()).toBe(2);
106+
done();
107+
});
108+
});
109+
};
110+
req.open('get', '/', true);
111+
req.send();
112+
});
113+
});
114+
}
116115

117-
it('button click', ifEnvSupports('document', () => {
118-
asyncStackTaggingZone.run(() => {
119-
const button = document.createElement('button');
120-
const clickEvent = document.createEvent('Event');
121-
clickEvent.initEvent('click', true, true);
122-
document.body.appendChild(button);
123-
const handler = () => {};
124-
button.addEventListener('click', handler);
125-
button.dispatchEvent(clickEvent);
126-
button.dispatchEvent(clickEvent);
127-
button.removeEventListener('click', handler);
128-
expect(scheduleAsyncTaskSpy)
129-
.toHaveBeenCalledWith('Zone - HTMLButtonElement.addEventListener:click');
130-
expect(startAsyncTaskSpy.calls.count()).toBe(2);
131-
expect(finishAsyncTaskSpy.calls.count()).toBe(2);
132-
});
133-
}));
116+
// Only run test when addEventListener is patched by zone.js
117+
if (document && document.addEventListener &&
118+
(document.addEventListener as any)[Zone.__symbol__('OriginalDelegate')]) {
119+
it('button click', () => {
120+
asyncStackTaggingZone.run(() => {
121+
const button = document.createElement('button');
122+
const clickEvent = document.createEvent('Event');
123+
clickEvent.initEvent('click', true, true);
124+
document.body.appendChild(button);
125+
const handler = () => {};
126+
button.addEventListener('click', handler);
127+
button.dispatchEvent(clickEvent);
128+
button.dispatchEvent(clickEvent);
129+
button.removeEventListener('click', handler);
130+
expect(scheduleAsyncTaskSpy)
131+
.toHaveBeenCalledWith('Zone - HTMLButtonElement.addEventListener:click');
132+
expect(startAsyncTaskSpy.calls.count()).toBe(2);
133+
expect(finishAsyncTaskSpy.calls.count()).toBe(2);
134+
});
135+
});
136+
}
134137
});
135138
});

packages/zone.js/bundles.bzl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ BUNDLES_ENTRY_POINTS = {
1919
"async-test": {
2020
"entrypoint": _DIR + "testing/async-testing",
2121
},
22-
"async-stack-tagging": {
23-
"entrypoint": _DIR + "zone-spec/async-stack-tagging",
24-
},
2522
"fake-async-test": {
2623
"entrypoint": _DIR + "testing/fake-async",
2724
},

packages/zone.js/dist/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ js_library(
4545
filegroup(
4646
name = "dist_bundle_group",
4747
srcs = [
48-
":async-stack-tagging.js",
49-
":async-stack-tagging.min.js",
5048
":async-test.js",
5149
":async-test.min.js",
5250
":fake-async-test.js",

packages/zone.js/dist/tools.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ ES5_BUNDLES = [
44
"zone-node",
55
"zone-testing-node-bundle",
66
"async-test",
7-
"async-stack-tagging",
87
"fake-async-test",
98
"long-stack-trace-zone",
109
"proxy",

packages/zone.js/plugins/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package(default_visibility = ["//visibility:public"])
33
filegroup(
44
name = "plugin_bundle_group",
55
srcs = [
6-
"//packages/zone.js/plugins:async-stack-tagging.min/package.json",
7-
"//packages/zone.js/plugins:async-stack-tagging/package.json",
86
"//packages/zone.js/plugins:async-test.min/package.json",
97
"//packages/zone.js/plugins:async-test/package.json",
108
"//packages/zone.js/plugins:fake-async-test.min/package.json",

packages/zone.js/plugins/async-stack-tagging.min/package.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/zone.js/plugins/async-stack-tagging/package.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)