From 811a2af32f41476601433cd10bc83d4d3c641287 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Mon, 20 Sep 2021 02:45:17 -0500 Subject: [PATCH] refactor w/ tasks array --- rollup.config.js | 5 +- src/lib/sandbox/main-access-handler.ts | 120 ++++++++++---------- src/lib/service-worker/sw-message.ts | 21 ++-- src/lib/types.ts | 11 +- src/lib/web-worker/worker-access-handler.ts | 60 +++++----- src/lib/web-worker/worker-proxy.ts | 26 +++-- tests/iframe/index.html | 4 +- 7 files changed, 130 insertions(+), 117 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 4f33e8af..c8e8e832 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -390,7 +390,7 @@ function managlePropsPlugin() { $documentReadyState$: '', $documentReferrer$: '', $documentTitle$: '', - $error$: '', + $errors$: '', $extraInstructions$: '', $firstScriptId$: '', $importScripts$: '', @@ -399,6 +399,7 @@ function managlePropsPlugin() { $instances$: '', $interfaces$: '', $interfaceType$: '', + $items: '', $isInitialized$: '', $isPromise$: '', $location$: '', @@ -410,6 +411,8 @@ function managlePropsPlugin() { $postMessage$: '', $rtnValue$: '', $scopePath$: '', + $stateProp$: '', + $tasks$: '', $url$: '', $window$: '', $winId$: '', diff --git a/src/lib/sandbox/main-access-handler.ts b/src/lib/sandbox/main-access-handler.ts index 50e8758b..ab478900 100644 --- a/src/lib/sandbox/main-access-handler.ts +++ b/src/lib/sandbox/main-access-handler.ts @@ -14,78 +14,80 @@ export const mainAccessHandler = async ( winCtx: MainWindowContext, accessReq: MainAccessRequest ) => { - let instanceId = accessReq.$instanceId$; - let accessType = accessReq.$accessType$; - let memberPath = accessReq.$memberPath$; - let memberPathLength = len(memberPath); - let lastMemberName = memberPath[memberPathLength - 1]; - let extraInstructions = accessReq.$extraInstructions$ || EMPTY_ARRAY; - let accessRsp: MainAccessResponse = { - $winId$: accessReq.$winId$, + if (accessReq.$forwardToWin$) { + return forwardToWinAccessHandler(winCtx.$worker$!, accessReq); + } + + const accessRsp: MainAccessResponse = { $msgId$: accessReq.$msgId$, - $instanceId$: instanceId, + $winId$: accessReq.$winId$, + $errors$: [], }; - let instance: any; - let rtnValue: any; - let data: any; - let i: number; - let count: number; - let tmr: any; - try { - if (accessReq.$forwardToWin$) { - return forwardToWinAccessHandler(winCtx.$worker$!, accessReq); - } + for (const accessReqTask of accessReq.$tasks$) { + let instanceId = accessReqTask.$instanceId$; + let accessType = accessReqTask.$accessType$; + let memberPath = accessReqTask.$memberPath$; + let memberPathLength = len(memberPath); + let lastMemberName = memberPath[memberPathLength - 1]; + let extraInstructions = accessReqTask.$extraInstructions$ || EMPTY_ARRAY; + let instance: any; + let rtnValue: any; + let data: any; + let i: number; + let count: number; + let tmr: any; - instance = getInstance(winCtx, instanceId); + try { + instance = getInstance(winCtx, instanceId); - if (instance) { - for (i = 0; i < memberPathLength - 1; i++) { - instance = instance[memberPath[i]]; - } + if (instance) { + for (i = 0; i < memberPathLength - 1; i++) { + instance = instance[memberPath[i]]; + } - data = deserializeFromWorker(winCtx, instanceId, accessReq.$data$); + data = deserializeFromWorker(winCtx, instanceId, accessReqTask.$data$); - if (accessType === AccessType.Get) { - if (extraInstructions.includes(ExtraInstruction.WAIT_FOR_INSTANCE_MEMBER)) { - await new Promise((resolve) => { - count = 0; - tmr = setInterval(() => { - if (isMemberInInstance(instance, memberPath) || count > 99) { - clearInterval(tmr); - resolve(); - } - count++; - }, 40); + if (accessType === AccessType.Get) { + if (extraInstructions.includes(ExtraInstruction.WAIT_FOR_INSTANCE_MEMBER)) { + await new Promise((resolve) => { + count = 0; + tmr = setInterval(() => { + if (isMemberInInstance(instance, memberPath) || count > 99) { + clearInterval(tmr); + resolve(); + } + count++; + }, 40); + }); + } + rtnValue = instance[lastMemberName]; + } else if (accessType === AccessType.Set) { + instance[lastMemberName] = data; + } else if (accessType === AccessType.CallMethod) { + rtnValue = instance[lastMemberName].apply(instance, data); + extraInstructions.forEach((extra, i) => { + if (extra === ExtraInstruction.SET_INERT_SCRIPT) { + (rtnValue as HTMLScriptElement).type = PT_SCRIPT_TYPE; + } + if (extra === ExtraInstruction.SET_IFRAME_SRCDOC) { + (rtnValue as HTMLIFrameElement).srcdoc = extraInstructions[i + 1] as any; + } }); } - rtnValue = instance[lastMemberName]; - } else if (accessType === AccessType.Set) { - instance[lastMemberName] = data; - } else if (accessType === AccessType.CallMethod) { - rtnValue = instance[lastMemberName].apply(instance, data); - extraInstructions.forEach((extra, i) => { - if (extra === ExtraInstruction.SET_INERT_SCRIPT) { - (rtnValue as HTMLScriptElement).type = PT_SCRIPT_TYPE; - } - if (extra === ExtraInstruction.SET_IFRAME_SRCDOC) { - (rtnValue as HTMLIFrameElement).srcdoc = extraInstructions[i + 1] as any; - } - }); - } - if (isPromise(rtnValue)) { - rtnValue = await rtnValue; - accessRsp.$isPromise$ = true; + if (isPromise(rtnValue)) { + rtnValue = await rtnValue; + accessRsp.$isPromise$ = true; + } + accessRsp.$rtnValue$ = serializeForWorker(winCtx, rtnValue, new Set()); + } else { + accessRsp.$errors$.push(`Instance ${instanceId} not found`); } - accessRsp.$rtnValue$ = serializeForWorker(winCtx, rtnValue, new Set()); - } else { - accessRsp.$error$ = `Instance ${instanceId} not found`; + } catch (e: any) { + accessRsp.$errors$.push(String(e.stack || e)); } - } catch (e: any) { - accessRsp.$error$ = String(e.stack || e); } - return accessRsp; }; diff --git a/src/lib/service-worker/sw-message.ts b/src/lib/service-worker/sw-message.ts index 239ba77c..001b713a 100644 --- a/src/lib/service-worker/sw-message.ts +++ b/src/lib/service-worker/sw-message.ts @@ -33,26 +33,23 @@ const sendMessageToSandboxFromServiceWorker = ( resolve, setTimeout(() => { resolves.delete(accessReq.$msgId$); - resolve({ - $winId$: accessReq.$winId$, - $msgId$: accessReq.$msgId$, - $instanceId$: accessReq.$instanceId$, - $error$: `Timeout`, - }); + resolve(swMessageError(accessReq, `Timeout`)); }, timeout), ]; resolves.set(accessReq.$msgId$, msgResolve); client.postMessage(accessReq); } else { - resolve({ - $winId$: accessReq.$winId$, - $msgId$: accessReq.$msgId$, - $instanceId$: accessReq.$instanceId$, - $error$: `No Partytown: ${accessReq}`, - }); + resolve(swMessageError(accessReq, `No Party`)); } }); +const swMessageError = (accessReq: MainAccessRequest, err: string) => ({ + $winId$: accessReq.$winId$, + $msgId$: accessReq.$msgId$, + $tasks$: [], + $errors$: [err], +}); + export const httpRequestFromWebWorker = (self: ServiceWorkerGlobalScope, req: Request) => new Promise(async (resolve) => { const accessReq: MainAccessRequest = await req.clone().json(); diff --git a/src/lib/types.ts b/src/lib/types.ts index 8dae5117..4ced7031 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -144,10 +144,14 @@ export const enum AccessType { export interface MainAccessRequest { $msgId$: number; $winId$: number; + $forwardToWin$: boolean; + $tasks$: MainAccessRequestTask[]; +} + +export interface MainAccessRequestTask { $instanceId$: number; $interfaceType$: InterfaceType; $nodeName$?: string; - $forwardToWin$: boolean; $accessType$: AccessType; $memberPath$: string[]; $data$?: SerializedTransfer; @@ -161,12 +165,11 @@ export const enum ExtraInstruction { } export interface MainAccessResponse { - $winId$: number; $msgId$: number; - $instanceId$: number; + $winId$: number; + $errors$: string[]; $rtnValue$?: SerializedTransfer; $isPromise$?: any; - $error$?: string; } export const enum SerializedType { diff --git a/src/lib/web-worker/worker-access-handler.ts b/src/lib/web-worker/worker-access-handler.ts index 8cbb7ca9..25a3a35e 100644 --- a/src/lib/web-worker/worker-access-handler.ts +++ b/src/lib/web-worker/worker-access-handler.ts @@ -8,42 +8,44 @@ import { len } from '../utils'; import { webWorkerCtx } from './worker-constants'; export const workerAccessHandler = (accessReq: MainAccessRequest) => { - let $winId$ = accessReq.$winId$; - let $instanceId$ = accessReq.$instanceId$; - let accessType = accessReq.$accessType$; - let memberPath = accessReq.$memberPath$; - let memberPathLength = len(memberPath); - let lastMemberName = memberPath[memberPathLength - 1]; - let accessRsp: MainAccessResponse = { + const accessRsp: MainAccessResponse = { $msgId$: accessReq.$msgId$, - $winId$, - $instanceId$, + $winId$: accessReq.$winId$, + $errors$: [], }; - let instance: any; - let rtnValue: any; - let data: any; - let i: number; - try { - instance = constructSerializedInstance(accessReq); + for (const accessReqTask of accessReq.$tasks$) { + let accessType = accessReqTask.$accessType$; + let memberPath = accessReqTask.$memberPath$; + let memberPathLength = len(memberPath); + let lastMemberName = memberPath[memberPathLength - 1]; - for (i = 0; i < memberPathLength - 1; i++) { - instance = instance[memberPath[i]]; - } + let instance: any; + let rtnValue: any; + let data: any; + let i: number; - data = deserializeFromMain(self, memberPath, accessReq.$data$); + try { + instance = constructSerializedInstance({ ...accessReqTask, $winId$: accessReq.$winId$ }); - if (accessType === AccessType.Get) { - rtnValue = instance[lastMemberName]; - } else if (accessType === AccessType.Set) { - instance[lastMemberName] = data; - } else if (accessType === AccessType.CallMethod) { - rtnValue = instance[lastMemberName].apply(instance, data); - } + for (i = 0; i < memberPathLength - 1; i++) { + instance = instance[memberPath[i]]; + } + + data = deserializeFromMain(self, memberPath, accessReqTask.$data$); - accessRsp.$rtnValue$ = serializeForMain(rtnValue, new Set()); - } catch (e: any) { - accessRsp.$error$ = String(e.stack || e); + if (accessType === AccessType.Get) { + rtnValue = instance[lastMemberName]; + } else if (accessType === AccessType.Set) { + instance[lastMemberName] = data; + } else if (accessType === AccessType.CallMethod) { + rtnValue = instance[lastMemberName].apply(instance, data); + } + + accessRsp.$rtnValue$ = serializeForMain(rtnValue, new Set()); + } catch (e: any) { + accessRsp.$errors$.push(String(e.stack || e)); + } } webWorkerCtx.$postMessage$([WorkerMessageType.ForwardMainDataResponse, accessRsp]); diff --git a/src/lib/web-worker/worker-proxy.ts b/src/lib/web-worker/worker-proxy.ts index 33e52d04..244673ac 100644 --- a/src/lib/web-worker/worker-proxy.ts +++ b/src/lib/web-worker/worker-proxy.ts @@ -3,8 +3,8 @@ import { ExtraInstruction, InterfaceType, MainAccessRequest, + MainAccessRequestTask, MainAccessResponse, - SerializedTransfer, } from '../types'; import { debug, @@ -33,19 +33,25 @@ const syncRequestToServiceWorker = ( ) => { const $winId$ = target[WinIdKey]; const $instanceId$ = target[InstanceIdKey]; - const accessReq: MainAccessRequest = { - $winId$, + + const accessReqTask: MainAccessRequestTask = { $instanceId$, $interfaceType$: target[InterfaceTypeKey], $nodeName$: target[NodeNameKey], - $forwardToWin$: webWorkerCtx.$winId$ !== target[WinIdKey], - $msgId$: Math.random(), + $accessType$, $memberPath$: memberPath, $data$: serializeForMain(data, new Set()), $extraInstructions$, }; + const accessReq: MainAccessRequest = { + $msgId$: Math.random(), + $winId$, + $forwardToWin$: webWorkerCtx.$winId$ !== target[WinIdKey], + $tasks$: [accessReqTask], + }; + if (debug && typeof accessReq.$winId$ !== 'number') { console.error(`Target missing winId`, accessReq); } @@ -57,16 +63,16 @@ const syncRequestToServiceWorker = ( // look ma, i'm synchronous (•‿•) const accessRsp: MainAccessResponse = JSON.parse(xhr.responseText); - const error = accessRsp.$error$; + const errors = accessRsp.$errors$.join(); const isPromise = accessRsp.$isPromise$; - if (error) { + const rtn = deserializeFromMain(target, memberPath, accessRsp.$rtnValue$!); + if (errors) { if (isPromise) { - return Promise.reject(error); + return Promise.reject(errors); } - throw new Error(error); + throw new Error(errors); } - const rtn = deserializeFromMain(target, memberPath, accessRsp.$rtnValue$!); if (isPromise) { return Promise.resolve(rtn); } diff --git a/tests/iframe/index.html b/tests/iframe/index.html index f0df226c..3fbf4564 100644 --- a/tests/iframe/index.html +++ b/tests/iframe/index.html @@ -309,7 +309,7 @@

Iframe

-
  • +