Skip to content
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

use another proxy to bind functions to the window #1217

Merged
merged 1 commit into from
Apr 30, 2024
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
25 changes: 18 additions & 7 deletions packages/lib/src/common/window/windowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ class WindowWrapper {
if (hydrateMockMap.has(property) && this.shouldUseMock) {
return hydrateMockMap.get(property);
}
if (
typeof target[property] === 'function' &&
!customWindowPropsSet.has(property)
) {
return target[property].bind(target);
}
return target[property];
}.bind(this),
// here we push to the custom props Set to know later if we want to bind the prop
Expand All @@ -45,7 +39,24 @@ class WindowWrapper {
},
};
// eslint-disable-next-line no-undef
this.window = new Proxy(window, handler);
const windowProxy = new Proxy(window, handler);
const windowFuncHandler = {
get: function (target, property) {
if (
!customWindowPropsSet.has(property) &&
typeof windowProxy[property] === 'function'
) {
return windowProxy[property].bind(window);
}
return windowProxy[property];
},
set: function (target, property, value) {
return Reflect.set(windowProxy, property, value);
},
};
// this second proxy that returnes binded functions to avoid issues with non configurable proprties
// eslint-disable-next-line no-undef
this.window = new Proxy({}, windowFuncHandler);
}
initMockWindow() {
this.window = WindowMock;
Expand Down
Loading