From 01651ddfac5806dd14f6082b48f5de4760906e2b Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Tue, 7 Dec 2021 21:50:53 -0600 Subject: [PATCH] fix: update location --- src/lib/sandbox/main-register-window.ts | 27 ++++++++-- src/lib/types.ts | 5 +- src/lib/web-worker/index.ts | 2 + tests/platform/history/history.spec.ts | 26 ++++++++- tests/platform/history/index.html | 71 ++++++++++++++++++++++--- 5 files changed, 118 insertions(+), 13 deletions(-) diff --git a/src/lib/sandbox/main-register-window.ts b/src/lib/sandbox/main-register-window.ts index 0b053dbc..8400cc40 100644 --- a/src/lib/sandbox/main-register-window.ts +++ b/src/lib/sandbox/main-register-window.ts @@ -16,22 +16,43 @@ export const registerWindow = ( windowIds.set($window$, $winId$); const doc = $window$.document; - const $url$ = doc.baseURI; + const history = $window$.history; const envData: InitializeEnvironmentData = { $winId$, $parentWinId$: windowIds.get($window$.parent)!, - $url$, + $url$: doc.baseURI, }; const sendInitEnvData = () => worker.postMessage([WorkerMessageType.InitializeEnvironment, envData]); + const pushState = history.pushState.bind(history); + const replaceState = history.replaceState.bind(history); + + const onLocationChange = () => + setTimeout(() => + worker.postMessage([WorkerMessageType.LocationUpdate, $winId$, doc.baseURI]) + ); + + history.pushState = (data, _, url) => { + pushState(data, _, url); + onLocationChange(); + }; + + history.replaceState = (data, _, url) => { + replaceState(data, _, url); + onLocationChange(); + }; + + $window$.addEventListener('popstate', onLocationChange); + $window$.addEventListener('hashchange', onLocationChange); + winCtxs[$winId$] = { $winId$, $window$, - $url$, }; + if (debug) { winCtxs[$winId$]!.$startTime$ = performance.now(); } diff --git a/src/lib/types.ts b/src/lib/types.ts index 4b2a7cf0..7de13a82 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -34,7 +34,8 @@ export type MessageFromSandboxToWorker = | [WorkerMessageType.InitializedEnvironment, WinId] | [WorkerMessageType.InitializeNextScript, InitializeScriptData] | [WorkerMessageType.RefHandlerCallback, RefHandlerCallbackData] - | [WorkerMessageType.ForwardMainTrigger, ForwardMainTriggerData]; + | [WorkerMessageType.ForwardMainTrigger, ForwardMainTriggerData] + | [WorkerMessageType.LocationUpdate, number, string]; export const enum WorkerMessageType { MainDataRequestFromWorker, @@ -48,6 +49,7 @@ export const enum WorkerMessageType { ForwardMainTrigger, ForwardWorkerAccessRequest, AsyncAccessRequest, + LocationUpdate, } export interface ForwardMainTriggerData { @@ -70,7 +72,6 @@ export interface MainWindowContext { $winId$: number; $isInitialized$?: number; $startTime$?: number; - $url$: string; $window$: MainWindow; } diff --git a/src/lib/web-worker/index.ts b/src/lib/web-worker/index.ts index b709cb4f..45d73a4a 100644 --- a/src/lib/web-worker/index.ts +++ b/src/lib/web-worker/index.ts @@ -36,6 +36,8 @@ const receiveMessageFromSandboxToWorker = (ev: MessageEvent { const testPushStateStr = page.locator('#testPushStateStr'); await expect(testPushStateStr).toHaveText('88'); + const buttonPushState = page.locator('#buttonPushState'); + await buttonPushState.click(); const testPushStateObj = page.locator('#testPushStateObj'); await expect(testPushStateObj).toHaveText(JSON.stringify({ mph: 88 })); + await page.waitForSelector('.testPushStateUrl'); + const testPushStateUrl = page.locator('#testPushStateUrl'); + await expect(testPushStateUrl).toHaveText('/platform/history/push-state'); + + const buttonGoBack = page.locator('#buttonGoBack'); + await buttonGoBack.click(); + await page.waitForSelector('.testGoBack'); + const testGoBack = page.locator('#testGoBack'); + await expect(testGoBack).toHaveText('/platform/history/'); + + const buttonGoForward = page.locator('#buttonGoForward'); + await buttonGoForward.click(); + await page.waitForSelector('.testGoForward'); + const testGoForward = page.locator('#testGoForward'); + await expect(testGoForward).toHaveText('/platform/history/push-state'); + const testReplaceState = page.locator('#testReplaceState'); const buttonReplaceState121 = page.locator('#buttonReplaceState121'); await buttonReplaceState121.click(); - const expected121 = new URL('/history/1.21gw', page.url()); + const expected121 = new URL('/platform/history/1.21gw', page.url()); await expect(page).toHaveURL(expected121.href); await expect(testReplaceState).toHaveText('1955'); + await page.waitForSelector('.testReplaceStateUrl'); + const testReplaceStateUrl = page.locator('#testReplaceStateUrl'); + await expect(testReplaceStateUrl).toHaveText('/platform/history/1.21gw'); + const buttonReplaceState = page.locator('#buttonReplaceState'); await buttonReplaceState.click(); - const expectedHistory = new URL('/history/', page.url()); + const expectedHistory = new URL('/platform/history/', page.url()); await expect(page).toHaveURL(expectedHistory.href); await expect(testReplaceState).toHaveText('1985'); diff --git a/tests/platform/history/index.html b/tests/platform/history/index.html index 3e2013f7..61ad2f6a 100644 --- a/tests/platform/history/index.html +++ b/tests/platform/history/index.html @@ -77,13 +77,64 @@

History

  • - pushState({ mph: 88 }, '') + pushState({ mph: 88 }) + + +
  • + +
  • + back() + + + +
  • + +
  • + forward() + + +
  • @@ -93,17 +144,24 @@

    History

    +