Skip to content

Commit

Permalink
✨ feat: support fetch plugin payload and add a lobeChat client class
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 6, 2023
1 parent 21a33c7 commit 6cb8978
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/client/fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './message';
export * from './pluginPayload';
export * from './pluginSettings';
export * from './pluginState';
25 changes: 25 additions & 0 deletions src/client/fetch/pluginPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PluginRequestPayload } from '@/schema/market';

import { PluginChannel } from '../const';

export interface PluginPayload<T = any> {
args?: T;
func: string;
}
export const fetchPluginPayload = <T = any>() =>
new Promise<PluginPayload<T>>((resolve) => {
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({ args, func });

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

window.addEventListener('message', receiverData);

top?.postMessage({ type: PluginChannel.pluginReadyForRender }, '*');
});
26 changes: 5 additions & 21 deletions src/client/hooks/useOnStandalonePluginInit.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import { useEffect } from 'react';

import { PluginChannel } from '@/client';
import { PluginRequestPayload } from '@/schema/market';

interface PluginPayload<T = any> {
args?: T;
func: string;
}
import { PluginPayload, fetchPluginPayload } from '../fetch/pluginPayload';

export const useOnStandalonePluginInit = <T = any>(
callback: (payload: PluginPayload<T>) => void,
) => {
const fn = (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 || '{}');
callback({ args, func });
}
};

useEffect(() => {
window.addEventListener('message', fn);
fetchPluginPayload().then((e) => {
if (!e) return;

top?.postMessage({ type: PluginChannel.pluginReadyForRender }, '*');
return () => {
window.removeEventListener('message', fn);
};
callback(e);
});
}, []);
};
1 change: 1 addition & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './const';
export * from './fetch';
export * from './hooks';
export * from './lobeChat';
export * from './postMessage';
export * from './type';
15 changes: 15 additions & 0 deletions src/client/lobeChat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
fetchPluginMessage,
fetchPluginPayload,
fetchPluginSettings,
fetchPluginState,
} from './fetch';

class LobeChat {
getPluginPayload = fetchPluginPayload;
getPluginSettings = fetchPluginSettings;
getPluginMessage = fetchPluginMessage;
getPluginState = fetchPluginState;
}

export const lobeChat = new LobeChat();

0 comments on commit 6cb8978

Please sign in to comment.