Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 7d4d07f

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(patch): fix #708, modify the canPatchDescriptor logic when browser don't provide onreadystatechange (#711)
1 parent 0a06874 commit 7d4d07f

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

Diff for: lib/browser/property-descriptor.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,40 @@ function canPatchViaPropertyDescriptor() {
6767
// by default XMLHttpRequest.prototype.onreadystatechange is undefined
6868
// without adding enumerable and configurable will cause onreadystatechange
6969
// non-configurable
70-
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {
71-
enumerable: true,
72-
configurable: true,
73-
get: function() {
74-
return true;
75-
}
76-
});
77-
const req = new XMLHttpRequest();
78-
const result = !!req.onreadystatechange;
79-
// restore original desc
80-
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
81-
return result;
82-
}
70+
// and if XMLHttpRequest.prototype.onreadystatechange is undefined,
71+
// we should set a real desc instead a fake one
72+
if (xhrDesc) {
73+
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {
74+
enumerable: true,
75+
configurable: true,
76+
get: function() {
77+
return true;
78+
}
79+
});
80+
const req = new XMLHttpRequest();
81+
const result = !!req.onreadystatechange;
82+
// restore original desc
83+
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
84+
return result;
85+
} else {
86+
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {
87+
enumerable: true,
88+
configurable: true,
89+
get: function() {
90+
return this[zoneSymbol('fakeonreadystatechange')];
91+
},
92+
set: function(value) {
93+
this[zoneSymbol('fakeonreadystatechange')] = value;
94+
}
95+
});
96+
const req = new XMLHttpRequest();
97+
const detectFunc = () => {};
98+
req.onreadystatechange = detectFunc;
99+
const result = (req as any)[zoneSymbol('fakeonreadystatechange')] === detectFunc;
100+
req.onreadystatechange = null;
101+
return result;
102+
}
103+
};
83104

84105
const unboundKey = zoneSymbol('unbound');
85106

0 commit comments

Comments
 (0)