Skip to content

Commit

Permalink
feat: Add support for passing functions in mainWindowAccessors (#183)
Browse files Browse the repository at this point in the history
* feat: try to create blocking calls for functions in worker-serializer

* refactor: update function serializer check

* test: ensure function passed in mainWindowAccessors is callable from worker
  • Loading branch information
slawekkolodziej committed May 24, 2022
1 parent e4b12bc commit e6260a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/lib/web-worker/worker-serialization.ts
@@ -1,5 +1,6 @@
import {
ApplyPath,
CallType,
InstanceId,
RefHandlerCallbackData,
RefId,
Expand Down Expand Up @@ -145,6 +146,10 @@ export const deserializeFromMain = (
}

if (serializedType === SerializedType.Function) {
if (winId && applyPath.length > 0) {
return (...args: any[]) => callMethod(environments[winId].$window$, applyPath, args, CallType.Blocking);
}

return noop;
}

Expand Down
11 changes: 10 additions & 1 deletion tests/integrations/main-window-accessors/index.html
Expand Up @@ -13,7 +13,7 @@
logSetters: true,
logStackTraces: false,
logScriptExecution: true,
mainWindowAccessors: ['globalField'],
mainWindowAccessors: ['globalField', 'globalFunction'],
};
</script>
<script src='/~partytown/debug/partytown.js'></script>
Expand Down Expand Up @@ -89,6 +89,15 @@ <h1>Main Window Accessors</h1>
<script type='text/partytown'>
document.getElementById('testChildWindowAccessor').innerText = window.globalField.parentObj.childValue;
</script>
<code id='testGlobalFunction'></code>
<script type='text/javascript'>
window.globalFunction = (arg) => {
document.getElementById('testGlobalFunction').textContent = JSON.stringify(arg);
}
</script>
<script type='text/partytown'>
window.globalFunction({ hello: 'world' });
</script>
</li>
<script type='text/partytown'>
document.body.classList.add('completed');
Expand Down
Expand Up @@ -9,4 +9,7 @@ test('integration window accessor', async ({ page }) => {

const child_element = page.locator('#testChildWindowAccessor');
await expect(child_element).toHaveText('stringValue');

const global_function = page.locator('#testGlobalFunction');
await expect(global_function).toHaveText('{"hello":"world"}');
});

1 comment on commit e6260a0

@vercel
Copy link

@vercel vercel bot commented on e6260a0 May 24, 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.