Skip to content

fix: correctly zone patch FileReader #67

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
May 31, 2022
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
9 changes: 9 additions & 0 deletions packages/zone-js/dist/nativescript-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@
// });

Zone.__load_patch('nativescript_FileReader', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
const reader = global['FileReader'];
if (reader) {
reader.prototype.onload = reader.prototype.onload || null;
reader.prototype.onerror = reader.prototype.onerror || null;
reader.prototype.onabort = reader.prototype.onabort || null;
reader.prototype.onloadend = reader.prototype.onloadend || null;
reader.prototype.onloadstart = reader.prototype.onloadstart || null;
reader.prototype.onprogress = reader.prototype.onprogress || null;
}
api.patchClass('FileReader');
});
20 changes: 17 additions & 3 deletions packages/zone-js/dist/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export function patchClass(className: string, api: _ZonePrivate) {
const instance = new OriginalClass(function () {});

let prop;
for (prop of Object.getOwnPropertyNames(instance)) {
for (prop of getAllProperties(instance)) {
// https://bugs.webkit.org/show_bug.cgi?id=44721
if (className === 'XMLHttpRequest' && prop === 'responseBlob') continue;
(function (prop) {
Expand Down Expand Up @@ -415,9 +415,23 @@ export function patchClass(className: string, api: _ZonePrivate) {
})(prop);
}

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

function getAllProperties(toCheck: any, lastProto = Object.prototype) {
const props = [];
let obj = toCheck;
do {
props.push(...Object.getOwnPropertyNames(obj));
} while ((obj = Object.getPrototypeOf(obj)) && obj !== lastProto);

return Array.from(new Set(props));
}

function isWritable<T extends Object>(obj: T, key: keyof T) {
return Object.getOwnPropertyDescriptor(obj, key)?.writable ?? true;
}