mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-25 02:08:28 +02:00
Fix events from old connections showing in new connections
Events from previous WebSocket/gRPC connections and HTTP responses were persisting in the store and displaying in new connections. Added filter parameter to mergeModelsInStore that clears old events when switching connections, plus render-time filtering as a safety net. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -209,12 +209,24 @@ export function replaceModelsInStore<
|
|||||||
export function mergeModelsInStore<
|
export function mergeModelsInStore<
|
||||||
M extends AnyModel['model'],
|
M extends AnyModel['model'],
|
||||||
T extends Extract<AnyModel, { model: M }>,
|
T extends Extract<AnyModel, { model: M }>,
|
||||||
>(model: M, models: T[]) {
|
>(model: M, models: T[], filter?: (model: T) => boolean) {
|
||||||
mustStore().set(modelStoreDataAtom, (prev: ModelStoreData) => {
|
mustStore().set(modelStoreDataAtom, (prev: ModelStoreData) => {
|
||||||
const existingModels = { ...prev[model] } as Record<string, T>;
|
const existingModels = { ...prev[model] } as Record<string, T>;
|
||||||
|
|
||||||
|
// Merge in new models first
|
||||||
for (const m of models) {
|
for (const m of models) {
|
||||||
existingModels[m.id] = m;
|
existingModels[m.id] = m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Then filter out unwanted models
|
||||||
|
if (filter) {
|
||||||
|
for (const [id, m] of Object.entries(existingModels)) {
|
||||||
|
if (!filter(m)) {
|
||||||
|
delete existingModels[id];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...prev,
|
...prev,
|
||||||
[model]: existingModels,
|
[model]: existingModels,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
replaceModelsInStore,
|
replaceModelsInStore,
|
||||||
} from '@yaakapp-internal/models';
|
} from '@yaakapp-internal/models';
|
||||||
import { useAtomValue } from 'jotai';
|
import { useAtomValue } from 'jotai';
|
||||||
import { useEffect, useMemo } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
export function useHttpResponseEvents(response: HttpResponse | null) {
|
export function useHttpResponseEvents(response: HttpResponse | null) {
|
||||||
const allEvents = useAtomValue(httpResponseEventsAtom);
|
const allEvents = useAtomValue(httpResponseEventsAtom);
|
||||||
@@ -17,18 +17,13 @@ export function useHttpResponseEvents(response: HttpResponse | null) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use merge instead of replace to preserve events that came in via model_write
|
// Fetch events from database, filtering out events from other responses and merging atomically
|
||||||
// while we were fetching from the database
|
|
||||||
invoke<HttpResponseEvent[]>('cmd_get_http_response_events', { responseId: response.id }).then(
|
invoke<HttpResponseEvent[]>('cmd_get_http_response_events', { responseId: response.id }).then(
|
||||||
(events) => mergeModelsInStore('http_response_event', events),
|
(events) =>
|
||||||
|
mergeModelsInStore('http_response_event', events, (e) => e.responseId === response.id),
|
||||||
);
|
);
|
||||||
}, [response?.id]);
|
}, [response?.id]);
|
||||||
|
|
||||||
// Filter events for the current response
|
const events = allEvents.filter((e) => e.responseId === response?.id);
|
||||||
const events = useMemo(
|
|
||||||
() => allEvents.filter((e) => e.responseId === response?.id),
|
|
||||||
[allEvents, response?.id],
|
|
||||||
);
|
|
||||||
|
|
||||||
return { data: events, error: null, isLoading: false };
|
return { data: events, error: null, isLoading: false };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
replaceModelsInStore,
|
replaceModelsInStore,
|
||||||
} from '@yaakapp-internal/models';
|
} from '@yaakapp-internal/models';
|
||||||
import { atom, useAtomValue } from 'jotai';
|
import { atom, useAtomValue } from 'jotai';
|
||||||
import { useEffect } from 'react';
|
import { useEffect, useMemo } from 'react';
|
||||||
import { atomWithKVStorage } from '../lib/atoms/atomWithKVStorage';
|
import { atomWithKVStorage } from '../lib/atoms/atomWithKVStorage';
|
||||||
import { activeRequestIdAtom } from './useActiveRequestId';
|
import { activeRequestIdAtom } from './useActiveRequestId';
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ export const activeGrpcConnectionAtom = atom<GrpcConnection | null>((get) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export function useGrpcEvents(connectionId: string | null) {
|
export function useGrpcEvents(connectionId: string | null) {
|
||||||
const events = useAtomValue(grpcEventsAtom);
|
const allEvents = useAtomValue(grpcEventsAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (connectionId == null) {
|
if (connectionId == null) {
|
||||||
@@ -68,12 +68,14 @@ export function useGrpcEvents(connectionId: string | null) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use merge instead of replace to preserve events that came in via model_write
|
// Fetch events from database, filtering out events from other connections and merging atomically
|
||||||
// while we were fetching from the database
|
invoke<GrpcEvent[]>('models_grpc_events', { connectionId }).then((events) =>
|
||||||
invoke<GrpcEvent[]>('models_grpc_events', { connectionId }).then((events) => {
|
mergeModelsInStore('grpc_event', events, (e) => e.connectionId === connectionId),
|
||||||
mergeModelsInStore('grpc_event', events);
|
);
|
||||||
});
|
|
||||||
}, [connectionId]);
|
}, [connectionId]);
|
||||||
|
|
||||||
return events;
|
return useMemo(
|
||||||
|
() => allEvents.filter((e) => e.connectionId === connectionId),
|
||||||
|
[allEvents, connectionId],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
websocketEventsAtom,
|
websocketEventsAtom,
|
||||||
} from '@yaakapp-internal/models';
|
} from '@yaakapp-internal/models';
|
||||||
import { atom, useAtomValue } from 'jotai';
|
import { atom, useAtomValue } from 'jotai';
|
||||||
import { useEffect } from 'react';
|
import { useEffect, useMemo } from 'react';
|
||||||
import { atomWithKVStorage } from '../lib/atoms/atomWithKVStorage';
|
import { atomWithKVStorage } from '../lib/atoms/atomWithKVStorage';
|
||||||
import { jotaiStore } from '../lib/jotai';
|
import { jotaiStore } from '../lib/jotai';
|
||||||
import { activeRequestIdAtom } from './useActiveRequestId';
|
import { activeRequestIdAtom } from './useActiveRequestId';
|
||||||
@@ -47,7 +47,7 @@ export function setPinnedWebsocketConnectionId(id: string | null) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useWebsocketEvents(connectionId: string | null) {
|
export function useWebsocketEvents(connectionId: string | null) {
|
||||||
const events = useAtomValue(websocketEventsAtom);
|
const allEvents = useAtomValue(websocketEventsAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (connectionId == null) {
|
if (connectionId == null) {
|
||||||
@@ -55,12 +55,14 @@ export function useWebsocketEvents(connectionId: string | null) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use merge instead of replace to preserve events that came in via model_write
|
// Fetch events from database, filtering out events from other connections and merging atomically
|
||||||
// while we were fetching from the database
|
invoke<WebsocketEvent[]>('models_websocket_events', { connectionId }).then((events) =>
|
||||||
invoke<WebsocketEvent[]>('models_websocket_events', { connectionId }).then(
|
mergeModelsInStore('websocket_event', events, (e) => e.connectionId === connectionId),
|
||||||
(events) => mergeModelsInStore('websocket_event', events),
|
|
||||||
);
|
);
|
||||||
}, [connectionId]);
|
}, [connectionId]);
|
||||||
|
|
||||||
return events;
|
return useMemo(
|
||||||
|
() => allEvents.filter((e) => e.connectionId === connectionId),
|
||||||
|
[allEvents, connectionId],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user