Skip to content

fix(zone.js): correctly patch es6 classes #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/zone-js/dist/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import { patchNativeScriptEventTarget } from './utils';
import { patchClass, patchNativeScriptEventTarget } from './utils';

function isPropertyWritable(propertyDesc: any) {
if (!propertyDesc) {
Expand Down Expand Up @@ -49,3 +49,7 @@ Zone.__load_patch('nativescript_patchMethod', (global, Zone, api) => {
Zone.__load_patch('nativescript_event_target_api', (g, z, api: any) => {
api.patchNativeScriptEventTarget = patchNativeScriptEventTarget;
});

Zone.__load_patch('nativescript_patch_class_api', (g, z, api) => {
api.patchClass = (className: string) => patchClass(className, api);
});
1 change: 1 addition & 0 deletions packages/zone-js/dist/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './core';
import './nativescript-globals';
import './events';
import './xhr';
import './connectivity';
12 changes: 12 additions & 0 deletions packages/zone-js/dist/nativescript-globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Zone.__load_patch('nativescript_MutationObserver', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// api.patchClass('MutationObserver');
// api.patchClass('WebKitMutationObserver');
// });

// Zone.__load_patch('nativescript_IntersectionObserver', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
// api.patchClass('IntersectionObserver');
// });

Zone.__load_patch('nativescript_FileReader', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
api.patchClass('FileReader');
});
8 changes: 5 additions & 3 deletions packages/zone-js/dist/pre-zone-polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
global['__Zone_disable_legacy'] = true;
global['__Zone_disable_EventTarget'] = true;
global['__Zone_disable_XHR'] = true;
export const disabledPatches = ['legacy', 'EventTarget', 'XHR', 'MutationObserver', 'IntersectionObserver', 'FileReader'];

for (const patch of disabledPatches) {
global[`__Zone_disable_${patch}`] = true;
}
90 changes: 90 additions & 0 deletions packages/zone-js/dist/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,93 @@ export function patchNativeScriptEventTarget(global: any, api: _ZonePrivate, api

return results;
}

const _global = global;

const zoneSymbol = Zone.__symbol__;

const originalInstanceKey = zoneSymbol('originalInstance');

function getAllPropertyNames(obj: unknown) {
const props = new Set<string>();

do {
Object.getOwnPropertyNames(obj).forEach((prop) => {
props.add(prop);
});
} while ((obj = Object.getPrototypeOf(obj)) && obj !== Object.prototype);

return Array.from(props);
}

// wrap some native API on `window`
export function patchClass(className: string, api: _ZonePrivate) {
const OriginalClass = _global[className];
if (!OriginalClass) return;
// keep original class in global
_global[zoneSymbol(className)] = OriginalClass;

_global[className] = function () {
const a = api.bindArguments(<any>arguments, className);
switch (a.length) {
case 0:
this[originalInstanceKey] = new OriginalClass();
break;
case 1:
this[originalInstanceKey] = new OriginalClass(a[0]);
break;
case 2:
this[originalInstanceKey] = new OriginalClass(a[0], a[1]);
break;
case 3:
this[originalInstanceKey] = new OriginalClass(a[0], a[1], a[2]);
break;
case 4:
this[originalInstanceKey] = new OriginalClass(a[0], a[1], a[2], a[3]);
break;
default:
throw new Error('Arg list too long.');
}
};

// attach original delegate to patched function
api.attachOriginToPatched(_global[className], OriginalClass);

const instance = new OriginalClass(function () {});

let prop;
for (prop of Object.getOwnPropertyNames(instance)) {
// https://bugs.webkit.org/show_bug.cgi?id=44721
if (className === 'XMLHttpRequest' && prop === 'responseBlob') continue;
(function (prop) {
if (typeof instance[prop] === 'function') {
_global[className].prototype[prop] = function () {
return this[originalInstanceKey][prop].apply(this[originalInstanceKey], arguments);
};
} else {
api.ObjectDefineProperty(_global[className].prototype, prop, {
set: function (fn) {
if (typeof fn === 'function') {
this[originalInstanceKey][prop] = api.wrapWithCurrentZone(fn, className + '.' + prop);
// keep callback in wrapped function so we can
// use it in Function.prototype.toString to return
// the native one.
api.attachOriginToPatched(this[originalInstanceKey][prop], fn);
} else {
this[originalInstanceKey][prop] = fn;
}
},
get: function () {
return this[originalInstanceKey][prop];
},
});
}
})(prop);
}

for (prop in Object.getOwnPropertyNames(OriginalClass)) {
if (prop !== 'prototype' && OriginalClass.hasOwnProperty(prop)) {
_global[className][prop] = OriginalClass[prop];
}
}
}