Skip to content

Commit

Permalink
refactor window interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 3, 2021
1 parent ecc1147 commit 87e80aa
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 30 deletions.
3 changes: 0 additions & 3 deletions src/lib/sandbox/init-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ export const initSandbox = async (sandboxWindow: Window, winIds: number) => {
}

setInstanceId(winCtx, win, PlatformInstanceId.window);
setInstanceId(winCtx, win.history, PlatformInstanceId.history);
setInstanceId(winCtx, win.localStorage, PlatformInstanceId.localStorage);
setInstanceId(winCtx, win.sessionStorage, PlatformInstanceId.sessionStorage);

createWebWorker(winCtx);

Expand Down
4 changes: 1 addition & 3 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,11 @@ export const enum InterfaceType {
Method = 15,
NamedNodeMap = 16,
Storage = 17,
Object = 18,
}

export const enum PlatformInstanceId {
window,
history,
localStorage,
sessionStorage,
document,
documentElement,
head,
Expand Down
14 changes: 1 addition & 13 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ const logTargetProp = (target: any, accessType: AccessType, memberPath: string[]
n = 'document.head.';
} else if (instanceId === PlatformInstanceId.body) {
n = 'document.body.';
} else if (instanceId === PlatformInstanceId.history) {
n = 'history.';
} else if (instanceId === PlatformInstanceId.localStorage) {
n = 'localStorage.';
} else if (instanceId === PlatformInstanceId.sessionStorage) {
n = 'sessionStorage.';
} else if (target.nodeType === 1) {
n = toLower(target.nodeName) + '.';
} else if (target[InterfaceTypeKey] === InterfaceType.Element && target[NodeNameKey]) {
Expand Down Expand Up @@ -190,14 +184,8 @@ const logValue = (memberPath: string[], v: any): string => {
if (instanceId === PlatformInstanceId.head) {
return `<head>`;
}
if (instanceId === PlatformInstanceId.localStorage) {
return `[localStorage]`;
}
if (instanceId === PlatformInstanceId.sessionStorage) {
return `[sessionStorage]`;
}
if (instanceId === PlatformInstanceId.window) {
return `[window]`;
return `window`;
}
if (v[InterfaceTypeKey] === InterfaceType.Element && v[NodeNameKey]) {
return `<${toLower(v[NodeNameKey])}>`;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/web-worker/worker-global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { callMethod } from './worker-proxy';
import { callMethod, proxy } from './worker-proxy';
import {
constructInstance,
elementConstructors,
Expand Down Expand Up @@ -39,9 +39,9 @@ export const initWebWorkerGlobal = (
});

self.document = constructInstance(InterfaceType.Document, PlatformInstanceId.document);
self.history = constructInstance(InterfaceType.History, PlatformInstanceId.history);
self.localStorage = constructInstance(InterfaceType.Storage, PlatformInstanceId.localStorage);
self.sessionStorage = constructInstance(InterfaceType.Storage, PlatformInstanceId.sessionStorage);
self.history = proxy(InterfaceType.History, self, ['history']);
self.localStorage = proxy(InterfaceType.Storage, self, ['localStorage']);
self.sessionStorage = proxy(InterfaceType.Storage, self, ['sessionStorage']);

navigator.sendBeacon = sendBeacon;

Expand Down
8 changes: 1 addition & 7 deletions src/lib/web-worker/worker-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,7 @@ export const constructSerializedInstance = ({
$nodeName$,
$items$,
}: SerializedInstance): any => {
if ($instanceId$ === PlatformInstanceId.history) {
return history;
} else if ($instanceId$ === PlatformInstanceId.localStorage) {
return localStorage;
} else if ($instanceId$ === PlatformInstanceId.sessionStorage) {
return sessionStorage;
} else if ($instanceId$ === PlatformInstanceId.window) {
if ($instanceId$ === PlatformInstanceId.window) {
return self;
} else if ($interfaceType$ === InterfaceType.NodeList) {
return new NodeList($items$!.map(constructSerializedInstance));
Expand Down

1 comment on commit 87e80aa

@vercel
Copy link

@vercel vercel bot commented on 87e80aa Oct 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.