Skip to content

Commit ff71723

Browse files
authored
fix: correctly zone patch FileReader (#67)
1 parent e023535 commit ff71723

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/zone-js/dist/nativescript-globals.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@
88
// });
99

1010
Zone.__load_patch('nativescript_FileReader', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
11+
const reader = global['FileReader'];
12+
if (reader) {
13+
reader.prototype.onload = reader.prototype.onload || null;
14+
reader.prototype.onerror = reader.prototype.onerror || null;
15+
reader.prototype.onabort = reader.prototype.onabort || null;
16+
reader.prototype.onloadend = reader.prototype.onloadend || null;
17+
reader.prototype.onloadstart = reader.prototype.onloadstart || null;
18+
reader.prototype.onprogress = reader.prototype.onprogress || null;
19+
}
1120
api.patchClass('FileReader');
1221
});

packages/zone-js/dist/utils.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export function patchClass(className: string, api: _ZonePrivate) {
386386
const instance = new OriginalClass(function () {});
387387

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

418-
for (prop in Object.getOwnPropertyNames(OriginalClass)) {
419-
if (prop !== 'prototype' && OriginalClass.hasOwnProperty(prop)) {
418+
for (prop of Object.getOwnPropertyNames(OriginalClass)) {
419+
if (prop !== 'prototype' && OriginalClass.hasOwnProperty(prop) && isWritable(_global[className], prop)) {
420420
_global[className][prop] = OriginalClass[prop];
421421
}
422422
}
423423
}
424+
425+
function getAllProperties(toCheck: any, lastProto = Object.prototype) {
426+
const props = [];
427+
let obj = toCheck;
428+
do {
429+
props.push(...Object.getOwnPropertyNames(obj));
430+
} while ((obj = Object.getPrototypeOf(obj)) && obj !== lastProto);
431+
432+
return Array.from(new Set(props));
433+
}
434+
435+
function isWritable<T extends Object>(obj: T, key: keyof T) {
436+
return Object.getOwnPropertyDescriptor(obj, key)?.writable ?? true;
437+
}

0 commit comments

Comments
 (0)