Skip to content

Commit

Permalink
fix: create node instance service worker (#374)
Browse files Browse the repository at this point in the history
Co-authored-by: vinay gosain <[email protected]>
  • Loading branch information
vinaygosain and vinay gosain authored Apr 12, 2023
1 parent e8ef6e2 commit 099852c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/sandbox/main-access-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/sandbox/main-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const serializeForWorker = (
value: any,
added?: Set<any>,
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) {
Expand Down Expand Up @@ -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)];
Expand Down
5 changes: 3 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,7 +368,8 @@ export type SerializedInstance =
/**
* Node name for Node instances
*/
type: string
type: string,
type?: string
];

/**
Expand Down
6 changes: 4 additions & 2 deletions src/lib/web-worker/worker-constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/web-worker/worker-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
7 changes: 5 additions & 2 deletions src/lib/web-worker/worker-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
};
Expand Down Expand Up @@ -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();
Expand All @@ -193,6 +195,7 @@ export const createWindow = (
cstrInstanceId = instanceId;
cstrNodeName = nodeName;
cstrNamespace = namespace;
cstrPrevInstance = prevInstance
return new NodeCstr() as any;
};

Expand Down
14 changes: 14 additions & 0 deletions tests/platform/script/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ <h1 class="title">Script</h1>
})();
</script>
</li>
<li>
<strong>script cloneNode</strong>
<code id="testSrcCloneNode"></code>
<script src="script-1.js" type="text/partytown"></script>
<script type="text/partytown">
(function () {
const scriptElm = document.querySelector("script[src='script-1.js']");
const scriptCloneElm = scriptElm.cloneNode(true);
const elm = document.getElementById('testSrcCloneNode');
elm.textContent = scriptCloneElm.src;
elm.className = 'testSrcCloneNode';
})();
</script>
</li>
</ul>

<hr />
Expand Down
3 changes: 3 additions & 0 deletions tests/platform/script/script-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function () {
console.log('a simple script is loaded');
})();
6 changes: 6 additions & 0 deletions tests/platform/script/script.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

1 comment on commit 099852c

@vercel
Copy link

@vercel vercel bot commented on 099852c Apr 12, 2023

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.