From 099852ce6f0c88b2a1a99c0301b5be40e9f22969 Mon Sep 17 00:00:00 2001 From: vinay gosain <32896629+vinaygosain@users.noreply.github.com> Date: Wed, 12 Apr 2023 19:49:46 +0530 Subject: [PATCH] fix: create node instance service worker (#374) Co-authored-by: vinay gosain --- src/lib/sandbox/main-access-handler.ts | 2 +- src/lib/sandbox/main-serialization.ts | 5 +++-- src/lib/types.ts | 5 +++-- src/lib/web-worker/worker-constructors.ts | 6 ++++-- src/lib/web-worker/worker-serialization.ts | 3 ++- src/lib/web-worker/worker-window.ts | 7 +++++-- tests/platform/script/index.html | 14 ++++++++++++++ tests/platform/script/script-1.js | 3 +++ tests/platform/script/script.spec.ts | 6 ++++++ 9 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 tests/platform/script/script-1.js diff --git a/src/lib/sandbox/main-access-handler.ts b/src/lib/sandbox/main-access-handler.ts index 3bb28f2d..f538d91a 100644 --- a/src/lib/sandbox/main-access-handler.ts +++ b/src/lib/sandbox/main-access-handler.ts @@ -94,7 +94,7 @@ export const mainAccessHandler = async ( } } if (isLast) { - accessRsp.$rtnValue$ = serializeForWorker(winId, rtnValue); + accessRsp.$rtnValue$ = serializeForWorker(winId, rtnValue, undefined, undefined, undefined, task.$instanceId$); } } else { if (debug) { diff --git a/src/lib/sandbox/main-serialization.ts b/src/lib/sandbox/main-serialization.ts index 11e90202..6e6c9e81 100644 --- a/src/lib/sandbox/main-serialization.ts +++ b/src/lib/sandbox/main-serialization.ts @@ -18,7 +18,8 @@ export const serializeForWorker = ( value: any, added?: Set, type?: string, - cstrName?: string + cstrName?: string, + prevInstanceId?: string ): SerializedTransfer | undefined => { if (value !== undefined && (type = typeof value)) { if (type === 'string' || type === 'number' || type === 'boolean' || value == null) { @@ -72,7 +73,7 @@ export const serializeForWorker = ( } else if (value.nodeType) { return [ SerializedType.Instance, - [$winId$, getAndSetInstanceId(value)!, getNodeName(value)], + [$winId$, getAndSetInstanceId(value)!, getNodeName(value), prevInstanceId], ]; } else { return [SerializedType.Object, serializeObjectForWorker($winId$, value, added, true, true)]; diff --git a/src/lib/types.ts b/src/lib/types.ts index 8fe8c567..d1b65c3b 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -187,7 +187,7 @@ export interface WebWorkerEnvironment { $body$: HTMLElement; $location$: Location; $visibilityState$?: string; - $createNode$: (nodeName: string, instanceId: InstanceId, namespace?: string) => WorkerNode; + $createNode$: (nodeName: string, instanceId: InstanceId, namespace?: string, prevInstance?: WorkerNode) => WorkerNode; $currentScriptId$?: InstanceId; $isInitialized$?: number; $isLoading$?: number; @@ -368,7 +368,8 @@ export type SerializedInstance = /** * Node name for Node instances */ - type: string + type: string, + type?: string ]; /** diff --git a/src/lib/web-worker/worker-constructors.ts b/src/lib/web-worker/worker-constructors.ts index 826b4a30..37bf5cbd 100644 --- a/src/lib/web-worker/worker-constructors.ts +++ b/src/lib/web-worker/worker-constructors.ts @@ -22,11 +22,13 @@ export const getOrCreateNodeInstance = ( instanceId: InstanceId, nodeName?: string, namespace?: string, - instance?: WorkerNode + instance?: WorkerNode, + prevInstanceId?: string ) => { instance = webWorkerInstances.get(instanceId); if (!instance && nodeName && environments[winId]) { - instance = environments[winId].$createNode$(nodeName, instanceId, namespace); + const prevInstance = webWorkerInstances.get(prevInstanceId || ''); + instance = environments[winId].$createNode$(nodeName, instanceId, namespace, prevInstance); webWorkerInstances.set(instanceId, instance!); } return instance; diff --git a/src/lib/web-worker/worker-serialization.ts b/src/lib/web-worker/worker-serialization.ts index 9853a16a..18969049 100644 --- a/src/lib/web-worker/worker-serialization.ts +++ b/src/lib/web-worker/worker-serialization.ts @@ -226,11 +226,12 @@ export const getOrCreateSerializedInstance = ([ winId, instanceId, nodeName, + prevInstanceId ]: SerializedInstance): any => { if (instanceId === winId && environments[winId]) { return environments[winId].$window$; } else { - return getOrCreateNodeInstance(winId, instanceId, nodeName!); + return getOrCreateNodeInstance(winId, instanceId, nodeName!, undefined, undefined, prevInstanceId); } }; diff --git a/src/lib/web-worker/worker-window.ts b/src/lib/web-worker/worker-window.ts index 912d3234..6126db8c 100644 --- a/src/lib/web-worker/worker-window.ts +++ b/src/lib/web-worker/worker-window.ts @@ -88,6 +88,7 @@ export const createWindow = ( let cstrInstanceId: InstanceId | undefined; let cstrNodeName: string | undefined; let cstrNamespace: string | undefined; + let cstrPrevInstance: WorkerNode | undefined; // base class all Nodes/Elements/Global Constructors will extend const WorkerBase = class implements WorkerInstance { @@ -110,7 +111,7 @@ export const createWindow = ( this[ApplyPathKey] = applyPath || []; this[InstanceDataKey] = instanceData || cstrNodeName; this[NamespaceKey] = namespace || cstrNamespace; - this[InstanceStateKey] = {}; + this[InstanceStateKey] = cstrPrevInstance && cstrPrevInstance[InstanceStateKey] || {}; cstrInstanceId = cstrNodeName = cstrNamespace = undefined; } }; @@ -179,7 +180,8 @@ export const createWindow = ( let $createNode$ = ( nodeName: string, instanceId: InstanceId, - namespace?: string + namespace?: string, + prevInstance?: WorkerNode, ): WorkerNode => { if (htmlMedia.includes(nodeName)) { initWindowMedia(); @@ -193,6 +195,7 @@ export const createWindow = ( cstrInstanceId = instanceId; cstrNodeName = nodeName; cstrNamespace = namespace; + cstrPrevInstance = prevInstance return new NodeCstr() as any; }; diff --git a/tests/platform/script/index.html b/tests/platform/script/index.html index 7c574c4e..ca87fde7 100644 --- a/tests/platform/script/index.html +++ b/tests/platform/script/index.html @@ -389,6 +389,20 @@

Script

})(); +
  • + script cloneNode + + + +

  • diff --git a/tests/platform/script/script-1.js b/tests/platform/script/script-1.js new file mode 100644 index 00000000..e84a0008 --- /dev/null +++ b/tests/platform/script/script-1.js @@ -0,0 +1,3 @@ +(function () { + console.log('a simple script is loaded'); + })(); \ No newline at end of file diff --git a/tests/platform/script/script.spec.ts b/tests/platform/script/script.spec.ts index 2ad5aef4..a18a39f2 100644 --- a/tests/platform/script/script.spec.ts +++ b/tests/platform/script/script.spec.ts @@ -83,4 +83,10 @@ test('script', async ({ page }) => { await page.waitForSelector('.testAppendMainScript'); const testAppendMainScript = page.locator('#testAppendMainScript'); await expect(testAppendMainScript).toHaveText('ptupdate'); + + await page.waitForSelector('.testSrcCloneNode'); + const testSrcCloneNode = page.locator('#testSrcCloneNode'); + const srcAttr2 = await testSrcCloneNode.textContent(); + const srcUrl2 = new URL(srcAttr2!); + expect(srcUrl2.pathname).toBe('/tests/platform/script/script-1.js'); });