Skip to content

Commit

Permalink
feat(patch): fix angular#1011, patch ResizeObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed Feb 3, 2018
1 parent fd91152 commit c195a7d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ gulp.task('build/zone-patch-user-media.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-user-media.ts', 'zone-patch-user-media.min.js', true, cb);
});

gulp.task('build/zone-patch-resize-observer.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-resize-observer.ts', 'zone-patch-resize-observer.js', false, cb);
});

gulp.task('build/zone-patch-resize-observer.min.js', ['compile-esm'], function(cb) {
return generateScript('./lib/browser/webapis-resize-observer.ts', 'zone-patch-resize-observer.min.js', true, cb);
});

gulp.task('build/bluebird.js', ['compile-esm'], function(cb) {
return generateScript('./lib/extra/bluebird.ts', 'zone-bluebird.js', false, cb);
});
Expand Down Expand Up @@ -297,6 +305,8 @@ gulp.task('build', [
'build/zone-patch-electron.min.js',
'build/zone-patch-user-media.js',
'build/zone-patch-user-media.min.js',
'build/zone-patch-resize-observer.js',
'build/zone-patch-resize-observer.min.js',
'build/zone-mix.js',
'build/bluebird.js',
'build/bluebird.min.js',
Expand Down
88 changes: 88 additions & 0 deletions lib/browser/webapis-resize-observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
Zone.__load_patch('ResizeObserver', (global: any, Zone: any, api: _ZonePrivate) => {
const ResizeObserver = global['ResizeObserver'];
if (!ResizeObserver) {
return;
}

const resizeObserverSymbol = api.symbol('ResizeObserver');

api.patchMethod(global, 'ResizeObserver', (delegate: Function) => (self: any, args: any[]) => {
const callback = args.length > 0 ? args[0] : null;
if (callback) {
args[0] = function(entries: any, observer: any) {
const zones: {[zoneName: string]: any} = {};
const currZone = Zone.current;
for (let entry of entries) {
let zone = entry.target[resizeObserverSymbol];
if (!zone) {
zone = currZone;
}
let zoneEntriesInfo = zones[zone.name];
if (!zoneEntriesInfo) {
zones[zone.name] = zoneEntriesInfo = {entries: [], zone: zone};
}
zoneEntriesInfo.entries.push(entry);
}

Object.keys(zones).forEach(zoneName => {
const zoneEntriesInfo = zones[zoneName];
if (zoneEntriesInfo.zone !== Zone.current) {
zoneEntriesInfo.zone.run(
callback, this, [zoneEntriesInfo.entries, observer], 'ResizeObserver');
} else {
callback.call(this, zoneEntriesInfo.entries, observer);
}
});
};
}
return delegate.apply(self, args);
});

api.patchMethod(ResizeObserver.prototype, 'observe', (delegate: Function) => (self: any, args: any[]) => {
const target = args.length > 0 ? args[0] : null;
if (!target) {
return delegate.apply(self, args);
}
let targets = self[resizeObserverSymbol];
if (!targets) {
targets = self[resizeObserverSymbol] = [];
}
targets.push(target);
target[resizeObserverSymbol] = Zone.current;
return delegate.apply(self, args);
});

api.patchMethod(ResizeObserver.prototype, 'unobserve', (delegate: Function) => (self: any, args: any[]) => {
const target = args.length > 0 ? args[0] : null;
if (!target) {
return delegate.apply(self, args);
}
let targets = self[resizeObserverSymbol];
if (targets) {
for (let i = 0; i < targets.length; i ++) {
if (targets[i] === target) {
targets.splice(i, 1);
break;
}
}
}
target[resizeObserverSymbol] = undefined;
return delegate.apply(self, args);
});

api.patchMethod(ResizeObserver.prototype, 'disconnect', (delegate: Function) => (self: any, args: any[]) => {
const targets = self[resizeObserverSymbol];
if (targets) {
targets.forEach((target: any) => {target[resizeObserverSymbol] = undefined;});
self[resizeObserverSymbol] = undefined;
}
return delegate.apply(self, args);
});
});

0 comments on commit c195a7d

Please sign in to comment.