Skip to content

Commit

Permalink
🐛 fix: 精简插件 SDK 提供的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Sep 3, 2023
1 parent cec2cba commit ac10c71
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/render/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum PluginChannel {
fetchPluginMessage = 'lobe-chat:fetch-plugin-message',
pluginReadyForRender = 'lobe-chat:pluginReadyForRender',
pluginReadyForRender = 'lobe-chat:plugin-ready-for-render',
renderPlugin = 'lobe-chat:render-plugin',
}
15 changes: 1 addition & 14 deletions src/render/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';
import { PluginRenderProps } from '@/types';

import { PluginChannel } from './const';
import { onPluginReady, onReceiveData } from './utils';
import { onReceiveData } from './utils';

export const useWatchPluginMessage = <T = any>() => {
const [result, setData] = useState<{ data: T; loading: boolean }>({
Expand All @@ -29,16 +29,3 @@ export const useWatchPluginMessage = <T = any>() => {

return result;
};

export const useOnPluginReady = (onReady: () => void) => {
useEffect(() => {
const fn = (e: MessageEvent) => {
onPluginReady(e, onReady);
};

window.addEventListener('message', fn);
return () => {
window.removeEventListener('message', fn);
};
}, []);
};
2 changes: 1 addition & 1 deletion src/render/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './const';
export * from './hooks';
export { fetchPluginMessage, sendMessageToPlugin } from './message';
export { fetchPluginMessage } from './message';
4 changes: 0 additions & 4 deletions src/render/message.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { PluginChannel } from './const';
import { onReceiveData } from './utils';

export const sendMessageToPlugin = (window: Window, props: any) => {
window.postMessage({ props, type: PluginChannel.renderPlugin }, '*');
};

export const fetchPluginMessage = () =>
new Promise<any>((resolve) => {
const receiverData = (e: MessageEvent) => {
Expand Down
17 changes: 1 addition & 16 deletions src/render/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { PluginChannel } from '@/render/const';
import { PluginRenderProps } from '@/types';

export const onPluginReady = (e: MessageEvent, onReady: () => void) => {
if (e.data.type === PluginChannel.pluginReadyForRender) {
onReady();
}
};

export const onPluginFetchMessage = (e: MessageEvent, onRequest: (data: any) => void) => {
if (e.data.type === PluginChannel.fetchPluginMessage) {
onRequest(e.data);
}
};

export const sendMessageToPlugin = (window: Window, props: any) => {
window.postMessage({ props, type: PluginChannel.renderPlugin }, '*');
};
import { PluginChannel } from './const';

export const onReceiveData = <T>(e: MessageEvent, onData: (data: PluginRenderProps<T>) => void) => {
if (e.data.type === PluginChannel.renderPlugin) {
Expand Down

0 comments on commit ac10c71

Please sign in to comment.