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

fix(patch): fix #708, modify the canPatchDescriptor logic when browser has no onreadystatechange #711

Merged
merged 1 commit into from
Apr 21, 2017
Merged
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
47 changes: 34 additions & 13 deletions lib/browser/property-descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,40 @@ function canPatchViaPropertyDescriptor() {
// by default XMLHttpRequest.prototype.onreadystatechange is undefined
// without adding enumerable and configurable will cause onreadystatechange
// non-configurable
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {
enumerable: true,
configurable: true,
get: function() {
return true;
}
});
const req = new XMLHttpRequest();
const result = !!req.onreadystatechange;
// restore original desc
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
return result;
}
// and if XMLHttpRequest.prototype.onreadystatechange is undefined,
// we should set a real desc instead a fake one
if (xhrDesc) {
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {
enumerable: true,
configurable: true,
get: function() {
return true;
}
});
const req = new XMLHttpRequest();
const result = !!req.onreadystatechange;
// restore original desc
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
return result;
} else {
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', {
enumerable: true,
configurable: true,
get: function() {
return this[zoneSymbol('fakeonreadystatechange')];
},
set: function(value) {
this[zoneSymbol('fakeonreadystatechange')] = value;
}
});
const req = new XMLHttpRequest();
const detectFunc = () => {};
req.onreadystatechange = detectFunc;
const result = (req as any)[zoneSymbol('fakeonreadystatechange')] === detectFunc;
req.onreadystatechange = null;
return result;
}
};

const unboundKey = zoneSymbol('unbound');

Expand Down