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

Commit cd6b95c

Browse files
committed
feat(patch): fix #1011, patch ResizeObserver
1 parent 42b9edb commit cd6b95c

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

NON-STANDARD-APIS.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ In electron, we patched the following APIs with `zone.js`
170170

171171
## Usage.
172172

173-
add/update following line into `polyfill.ts`.
173+
add following line into `polyfill.ts` after loading zone-mix.
174174

175175
```
176176
//import 'zone.js/dist/zone'; // originally added by angular-cli, comment it out
@@ -194,7 +194,17 @@ user need to patch `io` themselves just like following code.
194194
</script>
195195
```
196196

197-
198197
please reference the sample repo [zone-socketio](https://github.com/JiaLiPassion/zone-socketio) about
199198
detail usage.
200199

200+
* ResizeObserver
201+
202+
Currently only `Chrome 64` native support this feature.
203+
you can add the following line into `polyfill.ts` after loading `zone.js`.
204+
205+
```
206+
import 'zone.js/dist/zone';
207+
import 'zone.js/dist/zone-patch-resize-observer';
208+
```
209+
210+
there is a sample repo [zone-resize-observer](https://github.com/JiaLiPassion/zone-resize-observer) here

gulpfile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ gulp.task('build/zone-patch-socket-io.min.js', ['compile-esm'], function(cb) {
200200
return generateScript('./lib/extra/socket-io.ts', 'zone-patch-socket-io.min.js', true, cb);
201201
});
202202

203+
gulp.task('build/zone-patch-resize-observer.js', ['compile-esm'], function(cb) {
204+
return generateScript('./lib/browser/webapis-resize-observer.ts', 'zone-patch-resize-observer.js', false, cb);
205+
});
206+
207+
gulp.task('build/zone-patch-resize-observer.min.js', ['compile-esm'], function(cb) {
208+
return generateScript('./lib/browser/webapis-resize-observer.ts', 'zone-patch-resize-observer.min.js', true, cb);
209+
});
210+
203211
gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
204212
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
205213
});
@@ -307,6 +315,8 @@ gulp.task('build', [
307315
'build/zone-patch-user-media.min.js',
308316
'build/zone-patch-socket-io.js',
309317
'build/zone-patch-socket-io.min.js',
318+
'build/zone-patch-resize-observer.js',
319+
'build/zone-patch-resize-observer.min.js',
310320
'build/zone-mix.js',
311321
'build/bluebird.js',
312322
'build/bluebird.min.js',
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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('ResizeObserver', (global: any, Zone: any, api: _ZonePrivate) => {
9+
const ResizeObserver = global['ResizeObserver'];
10+
if (!ResizeObserver) {
11+
return;
12+
}
13+
14+
const resizeObserverSymbol = api.symbol('ResizeObserver');
15+
16+
api.patchMethod(global, 'ResizeObserver', (delegate: Function) => (self: any, args: any[]) => {
17+
const callback = args.length > 0 ? args[0] : null;
18+
if (callback) {
19+
args[0] = function(entries: any, observer: any) {
20+
const zones: {[zoneName: string]: any} = {};
21+
const currZone = Zone.current;
22+
for (let entry of entries) {
23+
let zone = entry.target[resizeObserverSymbol];
24+
if (!zone) {
25+
zone = currZone;
26+
}
27+
let zoneEntriesInfo = zones[zone.name];
28+
if (!zoneEntriesInfo) {
29+
zones[zone.name] = zoneEntriesInfo = {entries: [], zone: zone};
30+
}
31+
zoneEntriesInfo.entries.push(entry);
32+
}
33+
34+
Object.keys(zones).forEach(zoneName => {
35+
const zoneEntriesInfo = zones[zoneName];
36+
if (zoneEntriesInfo.zone !== Zone.current) {
37+
zoneEntriesInfo.zone.run(
38+
callback, this, [zoneEntriesInfo.entries, observer], 'ResizeObserver');
39+
} else {
40+
callback.call(this, zoneEntriesInfo.entries, observer);
41+
}
42+
});
43+
};
44+
}
45+
return args.length > 0 ? new ResizeObserver(args[0]) : new ResizeObserver();
46+
});
47+
48+
api.patchMethod(ResizeObserver.prototype, 'observe', (delegate: Function) => (self: any, args: any[]) => {
49+
const target = args.length > 0 ? args[0] : null;
50+
if (!target) {
51+
return delegate.apply(self, args);
52+
}
53+
let targets = self[resizeObserverSymbol];
54+
if (!targets) {
55+
targets = self[resizeObserverSymbol] = [];
56+
}
57+
targets.push(target);
58+
target[resizeObserverSymbol] = Zone.current;
59+
return delegate.apply(self, args);
60+
});
61+
62+
api.patchMethod(ResizeObserver.prototype, 'unobserve', (delegate: Function) => (self: any, args: any[]) => {
63+
const target = args.length > 0 ? args[0] : null;
64+
if (!target) {
65+
return delegate.apply(self, args);
66+
}
67+
let targets = self[resizeObserverSymbol];
68+
if (targets) {
69+
for (let i = 0; i < targets.length; i ++) {
70+
if (targets[i] === target) {
71+
targets.splice(i, 1);
72+
break;
73+
}
74+
}
75+
}
76+
target[resizeObserverSymbol] = undefined;
77+
return delegate.apply(self, args);
78+
});
79+
80+
api.patchMethod(ResizeObserver.prototype, 'disconnect', (delegate: Function) => (self: any, args: any[]) => {
81+
const targets = self[resizeObserverSymbol];
82+
if (targets) {
83+
targets.forEach((target: any) => {target[resizeObserverSymbol] = undefined;});
84+
self[resizeObserverSymbol] = undefined;
85+
}
86+
return delegate.apply(self, args);
87+
});
88+
});

0 commit comments

Comments
 (0)