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

Replace proxy wrapper #1208

Draft
wants to merge 1 commit into
base: V4_Final
Choose a base branch
from
Draft
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
42 changes: 15 additions & 27 deletions packages/lib/src/common/window/windowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import WindowMock, { hydrateMockMap } from './window.mock';
class WindowWrapper {
constructor() {
this.shouldUseMock = true;
this.initProxyWindow = this.initProxyWindow.bind(this);
this.initPartialWindoMock = this.initPartialWindoMock.bind(this);
if (this.windowIsAvailable()) {
// this will wrap the real window with partial mock for the dimensions
// once the gallery is mounted we will switch from the mocked properties to the real values
this.initProxyWindow();
this.initPartialWindoMock();
} else {
this.initMockWindow();
}
Expand All @@ -21,31 +21,19 @@ class WindowWrapper {
}
}

initProxyWindow() {
const customWindowPropsSet = new Set();
const handler = {
// here the proxy target is the global window object
get: function (target, property) {
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
// reflect just assigns the proprty and returns boolean if the assign was successfull
set: function (target, property, value) {
customWindowPropsSet.add(property);
return Reflect.set(target, property, value);
},
};
// eslint-disable-next-line no-undef
this.window = new Proxy(window, handler);
initPartialWindoMock() {
hydrateMockMap.forEach((value, key) => {
const windowPropValue = window[key];
Object.defineProperty(window, key, {
get: () => {
if (this.shouldUseMock) {
return hydrateMockMap.get(key);
}
return windowPropValue;
},
});
});
this.window = window;
}
initMockWindow() {
this.window = WindowMock;
Expand Down
Loading