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

Commit 961f3a2

Browse files
committed
feat(core): fix #1024, #513, #995, add a helper lib to configure root zone
1 parent e1df4bc commit 961f3a2

File tree

3 files changed

+103
-28
lines changed

3 files changed

+103
-28
lines changed

NON-STANDARD-APIS.md

+54-28
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Zone.js's support for non standard apis
22

33
Zone.js patched most standard APIs such as DOM event listeners, XMLHttpRequest in Browser, EventEmitter and fs API in Node.js so they can be in zone.
4-
5-
But there are still a lot of non standard APIs that are not patched by default, such as MediaQuery, Notification,
6-
WebAudio and so on. We are adding support to those APIs, and our progress is updated here.
7-
8-
## Currently supported non standard Web APIs
4+
5+
But there are still a lot of non standard APIs that are not patched by default, such as MediaQuery, Notification,
6+
WebAudio and so on. We are adding support to those APIs, and our progress is updated here.
7+
8+
## Currently supported non standard Web APIs
99

1010
* MediaQuery
11-
* Notification
11+
* Notification
1212

1313
## Currently supported polyfills
1414

@@ -28,7 +28,7 @@ Usage:
2828

2929
* bluebird promise
3030

31-
Browser Usage:
31+
Browser Usage:
3232

3333
```
3434
<script src="zone.js"></script>
@@ -75,11 +75,11 @@ remove the comment of the following line
7575

7676
## Others
7777

78-
* Cordova
78+
* Cordova
7979

8080
patch `cordova.exec` API
8181

82-
`cordova.exec(success, error, service, action, args);`
82+
`cordova.exec(success, error, service, action, args);`
8383

8484
`success` and `error` will be patched with `Zone.wrap`.
8585

@@ -96,12 +96,12 @@ to load the patch, you should load in the following order.
9696
By default, those APIs' support will not be loaded in zone.js or zone-node.js,
9797
so if you want to load those API's support, you should load those files by yourself.
9898

99-
For example, if you want to add MediaQuery patch, you should do like this:
99+
For example, if you want to add MediaQuery patch, you should do like this:
100100

101101
```
102-
<script src="path/zone.js"></script>
103-
<script src="path/webapis-media-query.js"></script>
104-
```
102+
<script src="path/zone.js"></script>
103+
<script src="path/webapis-media-query.js"></script>
104+
```
105105

106106
* rxjs
107107

@@ -127,28 +127,28 @@ constructorZone.run(() => {
127127
128128
subscriptionZone.run(() => {
129129
observable.subscribe(() => {
130-
console.log('current zone when subscription next', Zone.current.name); // will output subscription.
130+
console.log('current zone when subscription next', Zone.current.name); // will output subscription.
131131
}, () => {
132-
console.log('current zone when subscription error', Zone.current.name); // will output subscription.
132+
console.log('current zone when subscription error', Zone.current.name); // will output subscription.
133133
}, () => {
134-
console.log('current zone when subscription complete', Zone.current.name); // will output subscription.
134+
console.log('current zone when subscription complete', Zone.current.name); // will output subscription.
135135
});
136136
});
137137
138138
operatorZone.run(() => {
139139
observable.map(() => {
140-
console.log('current zone when map operator', Zone.current.name); // will output operator.
140+
console.log('current zone when map operator', Zone.current.name); // will output operator.
141141
});
142142
});
143143
```
144144

145145
Currently basically everything the `rxjs` API includes
146146

147-
- Observable
148-
- Subscription
149-
- Subscriber
150-
- Operators
151-
- Scheduler
147+
* Observable
148+
* Subscription
149+
* Subscriber
150+
* Operators
151+
* Scheduler
152152

153153
is patched, so each asynchronous call will run in the correct zone.
154154

@@ -194,7 +194,7 @@ user need to patch `io` themselves just like following code.
194194
</script>
195195
```
196196

197-
please reference the sample repo [zone-socketio](https://github.com/JiaLiPassion/zone-socketio) about
197+
please reference the sample repo [zone-socketio](https://github.com/JiaLiPassion/zone-socketio) about
198198
detail usage.
199199

200200
* jsonp
@@ -208,14 +208,15 @@ there is a sampel repo [zone-jsonp](https://github.com/JiaLiPassion/test-zone-js
208208
sample usage is:
209209

210210
```javascript
211-
import 'zone.js/dist/zone-patch-jsonp';
212-
Zone['__zone_symbol__jsonp']({
211+
import "zone.js/dist/zone-patch-jsonp";
212+
Zone["__zone_symbol__jsonp"]({
213213
jsonp: getJSONP,
214-
sendFuncName: 'send',
215-
successFuncName: 'jsonpSuccessCallback',
216-
failedFuncName: 'jsonpFailedCallback'
214+
sendFuncName: "send",
215+
successFuncName: "jsonpSuccessCallback",
216+
failedFuncName: "jsonpFailedCallback"
217217
});
218218
```
219+
219220
* ResizeObserver
220221

221222
Currently only `Chrome 64` native support this feature.
@@ -227,3 +228,28 @@ import 'zone.js/dist/zone-patch-resize-observer';
227228
```
228229

229230
there is a sample repo [zone-resize-observer](https://github.com/JiaLiPassion/zone-resize-observer) here
231+
232+
* customize root zone
233+
234+
In some application, especially for performance test or async tracing, user may want a predefined root zone.
235+
For example,i when you include `google map` into `index.html` by adding `script tag` such as
236+
`<script src="https://maps.googleapis.com/maps/api/js?key=KEY"></script>`, `google map js` will add some event
237+
listeners or do some async operations during loading the js file, all those operations happens in `root zone`,
238+
if we want to intercept those async operations, we need to modify root zone.
239+
240+
You can do like this:
241+
242+
```javascript
243+
<script src="folder/zone.js" />
244+
<script>
245+
var customizeRootZone = Zone.current.fork({
246+
name: 'myRoot'
247+
});
248+
window.__zone_symbol__customizeRootZone = customizeRootZone;
249+
</script>
250+
<script src="folder/zone-customize-root-zone.js" />
251+
```
252+
253+
** Remind, this is not a public api or formal feature of zone.js, it will not be included in future tc39 proposal,
254+
and it is not by design, it is just a helper method to let you customize rootZone for some test and suppose to change
255+
in the future, please use it on your own risk**

gulpfile.js

+10
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ gulp.task('build/bluebird.min.js', ['compile-esm'], function(cb) {
244244
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.min.js', true, cb);
245245
});
246246

247+
gulp.task('build/zone-customize-root.js', ['compile-esm'], function(cb) {
248+
return generateScript('./lib/extra/customize-root.ts', 'zone-customize-root.js', false, cb);
249+
});
250+
251+
gulp.task('build/zone-customize-root.min.js', ['compile-esm'], function(cb) {
252+
return generateScript('./lib/extra/customize-root.ts', 'zone-customize-root.min.js', true, cb);
253+
});
254+
247255
gulp.task('build/zone-patch-jsonp.js', ['compile-esm'], function(cb) {
248256
return generateScript('./lib/extra/jsonp.ts', 'zone-patch-jsonp.js', false, cb);
249257
});
@@ -367,6 +375,8 @@ gulp.task('build', [
367375
'build/zone-mix.js',
368376
'build/bluebird.js',
369377
'build/bluebird.min.js',
378+
'build/zone-customize-root.js',
379+
'build/zone-customize-root.min.js',
370380
'build/zone-patch-jsonp.js',
371381
'build/zone-patch-jsonp.min.js',
372382
'build/jasmine-patch.js',

lib/extra/customize-root.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
Zone.__load_patch(
9+
"customize-root",
10+
(global: any, Zone: ZoneType, api: _ZonePrivate) => {
11+
// add a helper utility to let user to customize root zone
12+
// this is not by design, and will not be included in future
13+
// tc39 zone proposal, it just for test and may change in the future
14+
const customizeRootZone = global[api.symbol("customizeRootZone")];
15+
if (!customizeRootZone) {
16+
return;
17+
}
18+
const currentDesc = Object.getOwnPropertyDescriptor(Zone, "current");
19+
if (!currentDesc) {
20+
return;
21+
}
22+
const currentDescGet = currentDesc.get;
23+
if (!currentDescGet) {
24+
return;
25+
}
26+
const rootZone = Zone.root;
27+
Object.defineProperty(Zone, "current", {
28+
configurable: true,
29+
enumerable: true,
30+
get: function() {
31+
const currentZone = currentDescGet.apply(this, arguments);
32+
if (currentZone === rootZone) {
33+
return customizeRootZone;
34+
}
35+
return currentZone;
36+
}
37+
});
38+
}
39+
);

0 commit comments

Comments
 (0)