Skip to content

Commit

Permalink
feat: change init handlers api
Browse files Browse the repository at this point in the history
  • Loading branch information
cylon3035 committed Dec 18, 2024
1 parent af87d0d commit b7241a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ fixture.setup(mainEnv, ({ run }, { COM: { communication }, echoFeature: { echoSe
button.onclick = async () => {
// this should be removed after tech debt is solved https://github.com/wixplosives/codux/issues/24381
if (url.searchParams.get(FETCH_OPTIONS_PARAM_NAME) === 'true' && myFrame.contentWindow) {
const removeListener = installRunOptionsInitMessageHandler(myFrame.contentWindow, () => {
const removeListener = installRunOptionsInitMessageHandler(myFrame.contentWindow, (postBack) => {
removeListener();
return url.searchParams;
postBack(url.searchParams.toString());
});
}
const { id } = await iframeInitializer({
Expand Down
20 changes: 13 additions & 7 deletions packages/core/src/com/initializers/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,21 @@ export async function startIframe({ com, iframe, instanceId, src, envReadyPromis
}
}

export function installRunOptionsInitMessageHandler(target: Window, getRunOptionsParams: () => URLSearchParams) {
export function installRunOptionsInitMessageHandler(
target: Window,
callback: (postBack: (params: string) => void) => void,
) {
function listenForRunOptionsRequest(evt: MessageEvent) {
if ('kind' in evt.data && evt.data.kind === RUN_OPTIONS_REQUESTED_KIND && evt.source === target) {
evt.source.postMessage(
{
kind: RUN_OPTIONS_PROVIDED_KIND,
runOptionsParams: getRunOptionsParams().toString(),
},
evt.origin,
const { source, origin } = evt;
callback((params) =>
source.postMessage(
{
kind: RUN_OPTIONS_PROVIDED_KIND,
runOptionsParams: params,
},
origin,
),
);
}
}
Expand Down

0 comments on commit b7241a9

Please sign in to comment.