|
| 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