Skip to content

Commit

Permalink
fix(vscode): use a ref so window.acquireVsCodeAp is called only once
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMcIntosh committed Jan 17, 2024
1 parent 686922f commit 076828f
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/hooks/useEventBusForChat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useReducer } from "react";
import { useEffect, useReducer, useRef } from "react";
import {
ChatMessages,
ChatResponse,
Expand Down Expand Up @@ -37,16 +37,20 @@ declare global {
}
}

function postMessage(message: Record<string, unknown>) {
const vscode = window.acquireVsCodeApi ? window.acquireVsCodeApi() : null;
if (vscode) {
vscode.postMessage(message);
const usePostMessage = () => {
const ref = useRef<typeof window.postMessage | undefined>(undefined);
if (ref.current) return ref.current;
if (window.acquireVsCodeApi) {
ref.current = window.acquireVsCodeApi().postMessage;
} else if (window.postIntellijMessage) {
window.postIntellijMessage(message);
ref.current = window.postIntellijMessage.bind(this);
} else {
window.postMessage(message, "*");
ref.current = (message: Record<string, unknown>) =>
window.postMessage(message, "*");
}
}

return ref.current;
};

function formatChatResponse(
messages: ChatMessages,
Expand Down Expand Up @@ -278,25 +282,33 @@ const initialState = createInitialState();

export const useEventBusForChat = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const postMessage = usePostMessage();

useEffect(() => {
const listener = (event: MessageEvent) => {
if (event.source !== window || event.origin !== window.location.origin) {
// eslint-disable-next-line no-console
console.log("CHAT: event source or origin not the same window");
return;
}
if (isActionToChat(event.data)) {
dispatch(event.data);
}

if (
isActionToChat(event.data) &&
event.data.payload?.id &&
event.data.payload.id === state.chat.id &&
isChatDoneStreaming(event.data)
) {
postMessage({
type: EVENT_NAMES_FROM_CHAT.SAVE_CHAT,
payload: state.chat,
});
}
};

window.addEventListener("message", listener);

return () => {
window.removeEventListener("message", listener);
};
}, [state, dispatch]);
}, [state, dispatch, postMessage]);

function askQuestion(question: string) {
const messages = state.chat.messages.concat([["user", question]]);
Expand Down Expand Up @@ -347,7 +359,7 @@ export const useEventBusForChat = () => {
) {
requestCaps();
}
}, [state]);
}, [state, postMessage]);

function clearError() {
dispatch({
Expand Down

0 comments on commit 076828f

Please sign in to comment.