fix(vscode): clear bridge request timeouts (#1236)
Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
committed by
GitHub
co-authored by
Isaac Sanchez
parent
7721c2fe10
commit
9816f5dd6c
@@ -39,6 +39,7 @@ interface BridgeResponse {
|
|||||||
const pendingRequests = new Map<string, {
|
const pendingRequests = new Map<string, {
|
||||||
resolve: (value: unknown) => void;
|
resolve: (value: unknown) => void;
|
||||||
reject: (reason: Error) => void;
|
reject: (reason: Error) => void;
|
||||||
|
timeout?: ReturnType<typeof setTimeout>;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
let requestIdCounter = 0;
|
let requestIdCounter = 0;
|
||||||
@@ -55,6 +56,9 @@ window.addEventListener('message', (event: MessageEvent<BridgeResponse>) => {
|
|||||||
const pending = pendingRequests.get(response.id);
|
const pending = pendingRequests.get(response.id);
|
||||||
if (pending) {
|
if (pending) {
|
||||||
pendingRequests.delete(response.id);
|
pendingRequests.delete(response.id);
|
||||||
|
if (pending.timeout) {
|
||||||
|
clearTimeout(pending.timeout);
|
||||||
|
}
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
pending.resolve(response.data);
|
pending.resolve(response.data);
|
||||||
} else {
|
} else {
|
||||||
@@ -76,14 +80,19 @@ export function sendBridgeMessageWithOptions<T = unknown>(
|
|||||||
const id = `req_${++requestIdCounter}_${Date.now()}`;
|
const id = `req_${++requestIdCounter}_${Date.now()}`;
|
||||||
const request: BridgeRequest = { id, type, payload };
|
const request: BridgeRequest = { id, type, payload };
|
||||||
|
|
||||||
pendingRequests.set(id, {
|
const pending: {
|
||||||
|
resolve: (value: unknown) => void;
|
||||||
|
reject: (reason: Error) => void;
|
||||||
|
timeout?: ReturnType<typeof setTimeout>;
|
||||||
|
} = {
|
||||||
resolve: resolve as (value: unknown) => void,
|
resolve: resolve as (value: unknown) => void,
|
||||||
reject,
|
reject,
|
||||||
});
|
};
|
||||||
|
pendingRequests.set(id, pending);
|
||||||
|
|
||||||
const timeoutMs = typeof options?.timeoutMs === 'number' ? options.timeoutMs : 30000;
|
const timeoutMs = typeof options?.timeoutMs === 'number' ? options.timeoutMs : 30000;
|
||||||
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
||||||
setTimeout(() => {
|
pending.timeout = setTimeout(() => {
|
||||||
if (pendingRequests.has(id)) {
|
if (pendingRequests.has(id)) {
|
||||||
pendingRequests.delete(id);
|
pendingRequests.delete(id);
|
||||||
reject(new Error(`Request ${type} timed out`));
|
reject(new Error(`Request ${type} timed out`));
|
||||||
|
|||||||
Reference in New Issue
Block a user