Skip to content

Commit

Permalink
🐛 fix: fix fetchPluginState bug
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Oct 24, 2023
1 parent 3c9ad14 commit d2ff3b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/client/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export enum PluginChannel {
initStandalonePlugin = 'lobe-chat:init-standalone-plugin',
pluginReadyForRender = 'lobe-chat:plugin-ready-for-render',
renderPlugin = 'lobe-chat:render-plugin',
renderPluginState = 'lobe-chat:render-plugin-state',
updatePluginState = 'lobe-chat:update-plugin-state',
}
11 changes: 7 additions & 4 deletions src/client/fetch/message.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { PluginRenderProps } from '@/client';

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

export const fetchPluginMessage = <T = any>() =>
new Promise<T>((resolve) => {
const receiverData = (e: MessageEvent) => {
onReceiveData(e, (data) => {
resolve(data.content as T);
if (e.data.type === PluginChannel.renderPlugin) {
const props = e.data.props as PluginRenderProps<T>;
resolve(props.content as T);

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

window.addEventListener('message', receiverData);
Expand Down
8 changes: 4 additions & 4 deletions src/client/fetch/pluginState.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PluginChannel } from '../const';
import { onReceiveData } from '../utils';

export const fetchPluginState = <T = any>() =>
new Promise<T>((resolve) => {
const receiverData = (e: MessageEvent) => {
onReceiveData(e, (data) => {
resolve(data.content as T);
if (e.data.type === PluginChannel.renderPluginState) {
resolve(e.data.props);

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

window.addEventListener('message', receiverData);
Expand Down

0 comments on commit d2ff3b6

Please sign in to comment.