Skip to content

Commit

Permalink
refactor global screen implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 4, 2021
1 parent 9bf6961 commit 5c3af58
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/lib/sandbox/read-main-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const readMainInterfaces = (win: Window, doc: Document) => {
[InterfaceType.MutationObserver, new MutationObserver(noop)],
[InterfaceType.NamedNodeMap, docHead.attributes],
[InterfaceType.NodeList, docHead.childNodes],
[InterfaceType.Screen, win.screen],
[InterfaceType.Storage, win.localStorage],
[InterfaceType.TextNode, docImpl.createTextNode('')],
].map((i) => [...i, getConstructorName(i[1] as any)]) as any;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export const enum InterfaceType {
History = 15,
MutationObserver = 16,
NamedNodeMap = 17,
Storage = 18,
Object = 19,
Screen = 18,
Storage = 19,
}

export const enum PlatformInstanceId {
Expand Down
24 changes: 13 additions & 11 deletions src/lib/web-worker/worker-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,29 @@ export const initWebWorkerGlobal = (
self[InstanceIdKey] = PlatformInstanceId.window;

Object.keys(windowMemberTypeInfo).map((memberName) => {
if (!self[memberName] && windowMemberTypeInfo[memberName] === InterfaceType.Function) {
self[memberName] = (...args: any[]) => callMethod(self, [memberName], args);
const interfaceType = windowMemberTypeInfo[memberName];

if (!self[memberName] && interfaceType > InterfaceType.Window) {
// this globa doesn't already exist in the worker
// and the interface type isn't a Window object
if (interfaceType === InterfaceType.Function) {
// this is a global function, like alert()
self[memberName] = (...args: any[]) => callMethod(self, [memberName], args);
} else if (interfaceType > InterfaceType.DocumentFragmentNode) {
// this is a global implementation, like localStorage
self[memberName] = proxy(interfaceType, self, [memberName]);
}
}
});

interfaces.map((i) => createGlobalConstructorProxy(self, i[0], i[1]));

self.requestAnimationFrame = (cb: (t: number) => void) => nextTick(() => cb(Date.now()), 9);
self.cancelAnimationFrame = clearTimeout;

Object.defineProperty(self, 'location', {
get: () => webWorkerCtx.$location$,
set: (href) => (webWorkerCtx.$location$.href = href + ''),
});

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

navigator.sendBeacon = sendBeacon;

Expand All @@ -68,11 +71,10 @@ export const initWebWorkerGlobal = (
self.top = constructInstance(InterfaceType.Window, PlatformInstanceId.window, TOP_WIN_ID);
}

self.HTMLDocument = self.Document = HTMLDocument;
self.Document = HTMLDocument;
self.HTMLElement = self.Element = HTMLElement;
self.Image = HTMLImageElement;
self.Node = Node;
self.Window = Window;

htmlCstrNames.map((htmlCstrName) => {
if (!self[htmlCstrName]) {
Expand Down

1 comment on commit 5c3af58

@vercel
Copy link

@vercel vercel bot commented on 5c3af58 Oct 4, 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.