Skip to content

Commit

Permalink
🐛 fix: fix compatible with ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 21, 2023
1 parent 89980b4 commit 2337060
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/client/fetch/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import { PluginChannel } from '../const';

export const fetchPluginMessage = <T = any>() =>
new Promise<T>((resolve) => {
if (typeof window === 'undefined') {
resolve(undefined as any);
return;
}
const receiverData = (e: MessageEvent) => {
if (e.data.type === PluginChannel.renderPlugin) {
const props = e.data.props as PluginRenderProps<T>;
resolve(props.content as T);

window?.removeEventListener('message', receiverData);
window.removeEventListener('message', receiverData);
}
};

window?.addEventListener('message', receiverData);
window.addEventListener('message', receiverData);

top?.postMessage({ type: PluginChannel.fetchPluginMessage }, '*');
});
8 changes: 6 additions & 2 deletions src/client/fetch/pluginPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ export interface PluginPayload<T = any> {

export const fetchPluginPayload = <T = any>() =>
new Promise<PluginPayload<T>>((resolve) => {
if (typeof window === 'undefined') {
resolve(undefined as any);
return;
}
const receiverData = (e: MessageEvent) => {
if (e.data.type === PluginChannel.initStandalonePlugin) {
const payload = e.data.props as PluginRequestPayload;
const func = payload.apiName;
const args = JSON.parse(payload.arguments || '{}');
resolve({ arguments: args, name: func });

window?.removeEventListener('message', receiverData);
window.removeEventListener('message', receiverData);
}
};

window?.addEventListener('message', receiverData);
window.addEventListener('message', receiverData);

top?.postMessage({ type: PluginChannel.pluginReadyForRender }, '*');
});
9 changes: 7 additions & 2 deletions src/client/fetch/pluginSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { PluginChannel } from '../const';

export const fetchPluginSettings = <T = any>() =>
new Promise<T>((resolve) => {
if (typeof window === 'undefined') {
resolve(undefined as any);
return;
}

const receiverData = (e: MessageEvent) => {
if (e.data.type === PluginChannel.renderPluginSettings) {
resolve(e.data.value);

window?.removeEventListener('message', receiverData);
window.removeEventListener('message', receiverData);
}
};

window?.addEventListener('message', receiverData);
window.addEventListener('message', receiverData);

top?.postMessage({ type: PluginChannel.fetchPluginSettings }, '*');
});
9 changes: 7 additions & 2 deletions src/client/fetch/pluginState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { PluginChannel } from '../const';

export const fetchPluginState = <T = any>(key: string) =>
new Promise<T>((resolve) => {
if (typeof window === 'undefined') {
resolve(undefined as any);
return;
}

const receiverData = (e: MessageEvent) => {
if (e.data.type === PluginChannel.renderPluginState && e.data.key === key) {
resolve(e.data.value);

window?.removeEventListener('message', receiverData);
window.removeEventListener('message', receiverData);
}
};

window?.addEventListener('message', receiverData);
window.addEventListener('message', receiverData);

top?.postMessage({ key, type: PluginChannel.fetchPluginState }, '*');
});

0 comments on commit 2337060

Please sign in to comment.