From 9041761a343959bcda85c1e2963466c2f9bcf3aa Mon Sep 17 00:00:00 2001 From: wwayne Date: Tue, 28 May 2024 15:19:29 +0800 Subject: [PATCH] update --- clients/tabby-chat-panel/src/index.ts | 6 ++++-- clients/tabby-chat-panel/src/react.ts | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/clients/tabby-chat-panel/src/index.ts b/clients/tabby-chat-panel/src/index.ts index d09896e8beca..09f27d62fe2c 100644 --- a/clients/tabby-chat-panel/src/index.ts +++ b/clients/tabby-chat-panel/src/index.ts @@ -39,19 +39,21 @@ export interface ChatMessage { relevantContext?: Array } -export function createClient(target: HTMLIFrameElement, api: ClientApi): ServerApi { +export function createClient(target: HTMLIFrameElement, api: ClientApi, signal?: AbortSignal): ServerApi { return createThreadFromIframe(target, { expose: { navigate: api.navigate, }, + signal }) } -export function createServer(api: ServerApi): ClientApi { +export function createServer(api: ServerApi, signal?: AbortSignal): ClientApi { return createThreadFromInsideIframe({ expose: { init: api.init, sendMessage: api.sendMessage, }, + signal }) } diff --git a/clients/tabby-chat-panel/src/react.ts b/clients/tabby-chat-panel/src/react.ts index 0a34d52eae65..33939b651b17 100644 --- a/clients/tabby-chat-panel/src/react.ts +++ b/clients/tabby-chat-panel/src/react.ts @@ -6,12 +6,14 @@ import { createClient, createServer } from './index' function useClient(iframeRef: RefObject, api: ClientApi) { const [client, setClient] = useState(null) - let isCreated = false + let abortController: AbortController useEffect(() => { - if (iframeRef.current && !isCreated) { - isCreated = true - setClient(createClient(iframeRef.current, api)) + if (iframeRef.current) { + console.log('client abortController', abortController) + abortController?.abort() + setClient(createClient(iframeRef.current, api, abortController?.signal)) + abortController = new AbortController() } }, [iframeRef.current]) @@ -20,13 +22,15 @@ function useClient(iframeRef: RefObject, api: ClientApi) { function useServer(api: ServerApi) { const [server, setServer] = useState(null) - let isCreated = false + let abortController: AbortController useEffect(() => { const isInIframe = window.self !== window.top - if (isInIframe && !isCreated) { - isCreated = true - setServer(createServer(api)) + if (isInIframe) { + console.log('server abortController', abortController) + abortController?.abort() + setServer(createServer(api, abortController?.signal)) + abortController = new AbortController() } }, [])