From 3d9bd7543422309a0ff3d48c48ba83f832c5ccd8 Mon Sep 17 00:00:00 2001 From: Le Vivilet Date: Sun, 1 Sep 2024 11:23:37 +0200 Subject: [PATCH] fix: response when calling extension host command --- src/parts/PreviewInjectedCode/PreviewInjectedCode.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/parts/PreviewInjectedCode/PreviewInjectedCode.ts b/src/parts/PreviewInjectedCode/PreviewInjectedCode.ts index a85a838c..2f8b7bc0 100644 --- a/src/parts/PreviewInjectedCode/PreviewInjectedCode.ts +++ b/src/parts/PreviewInjectedCode/PreviewInjectedCode.ts @@ -1,6 +1,11 @@ export const injectedCode = ` let commandMap = {} let port +let id = 0 + +const createId = () => { + return ++id +} const callbacks = Object.create(null) @@ -12,6 +17,7 @@ const handleMessage = async (event) => { const message = event.data if(isJsonRpcResponse(message)){ const fn = callbacks[message.id] + delete callbacks[message.id] fn(message.result) return } @@ -46,9 +52,9 @@ const withResolvers = () => { } const registerPromise = () => { - const id = 1 + const id = createId() const {resolve, promise} = withResolvers() - callbacks[id] = { resolve } + callbacks[id] = resolve return { id, promise }