Skip to content

Commit

Permalink
replace proxy wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
IzaacAyelin committed Apr 14, 2024
1 parent 56273a6 commit 93b45b9
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions packages/lib/src/common/window/windowWrapper.js
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

0 comments on commit 93b45b9

Please sign in to comment.