Skip to content

Commit

Permalink
fix: move Location within env
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Feb 25, 2022
1 parent ec16049 commit a728448
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
1 change: 1 addition & 0 deletions scripts/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function managlePropsPlugin(): Plugin {
const mangleProps: { [key: string]: string } = {
$applyPath$: '',
$args$: '',
$asyncMsgTimer$: '',
$assignInstanceId$: '',
$body$: '',
$bridgeFromMedia$: '',
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export type InterfaceMember =
| [string, InterfaceType.Property, string | number | boolean];

export interface WebWorkerContext {
$asyncMsgTimer$?: any;
$config$: PartytownConfig;
$importScripts$: (...urls: string[]) => void;
$initWindowMedia$?: InitWindowMedia;
Expand Down
13 changes: 0 additions & 13 deletions src/lib/web-worker/worker-location.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/lib/web-worker/worker-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ import syncSendMessageToMain from '../build-modules/sync-send-message-to-main';

const taskQueue: MainAccessTask[] = [];

let asyncMsgTimer: any = 0;

const queue = (
instance: WorkerInstance,
$applyPath$: ApplyPath,
Expand Down Expand Up @@ -91,11 +89,11 @@ const queue = (
}

// task is not blocking, so just update the async timer
asyncMsgTimer = setTimeout(sendToMain, 20);
webWorkerCtx.$asyncMsgTimer$ = setTimeout(sendToMain, 20);
};

export const sendToMain = (isBlocking?: boolean) => {
clearTimeout(asyncMsgTimer);
clearTimeout(webWorkerCtx.$asyncMsgTimer$);

if (len(taskQueue)) {
if (debug && webWorkerCtx.$config$.logMainAccess) {
Expand Down
28 changes: 22 additions & 6 deletions src/lib/web-worker/worker-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ import {
setInstanceStateValue,
} from './worker-state';
import { getInitWindowMedia, htmlMedia, windowMediaConstructors } from './worker-media';
import { Location } from './worker-location';
import { normalizedWinId } from '../log';
import { logWorker, normalizedWinId } from '../log';
import {
patchDocument,
patchDocumentElementChild,
Expand Down Expand Up @@ -105,6 +104,23 @@ export const createWindow = (
}
};

const WorkerLocation = defineConstructorName(
class extends URL {
assign() {
logWorker(`location.assign(), noop`);
}
reload() {
logWorker(`location.reload(), noop`);
}
replace() {
logWorker(`location.replace(), noop`);
}
},
'Location'
);

const $location$ = new WorkerLocation(url);

// window global eveything will live within
const WorkerWindow = defineConstructorName(
class extends WorkerBase implements WorkerWindow {
Expand Down Expand Up @@ -327,7 +343,7 @@ export const createWindow = (
) as any,
$head$: $createNode$(NodeName.Head, PlatformInstanceId.head) as any,
$body$: $createNode$(NodeName.Body, PlatformInstanceId.body) as any,
$location$: new Location(url) as any,
$location$,
$createNode$,
};

Expand Down Expand Up @@ -429,10 +445,10 @@ export const createWindow = (
}

get location() {
return getEnv(this).$location$;
return $location$;
}
set location(loc: any) {
getEnv(this).$location$.href = loc + '';
$location$.href = loc + '';
}

get Image() {
Expand All @@ -453,7 +469,7 @@ export const createWindow = (
}

get origin() {
return getEnv(this).$location$.origin;
return $location$.origin;
}
set origin(_) {}

Expand Down

1 comment on commit a728448

@vercel
Copy link

@vercel vercel bot commented on a728448 Feb 25, 2022

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.