mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 18:01:08 +01:00
Fix race condition where streamed events could be lost
Events stream in via model_write listener while also being fetched from the database. If the DB fetch completed before all events were persisted, replaceModelsInStore would wipe out events that came in via model_write. Added mergeModelsInStore that adds fetched events without removing existing ones. Applied to HTTP, gRPC, and WebSocket event hooks.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import type { HttpResponse, HttpResponseEvent } from '@yaakapp-internal/models';
|
||||
import { httpResponseEventsAtom, replaceModelsInStore } from '@yaakapp-internal/models';
|
||||
import {
|
||||
httpResponseEventsAtom,
|
||||
mergeModelsInStore,
|
||||
replaceModelsInStore,
|
||||
} from '@yaakapp-internal/models';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { useEffect, useMemo } from 'react';
|
||||
|
||||
@@ -13,8 +17,10 @@ export function useHttpResponseEvents(response: HttpResponse | null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use merge instead of replace to preserve events that came in via model_write
|
||||
// while we were fetching from the database
|
||||
invoke<HttpResponseEvent[]>('cmd_get_http_response_events', { responseId: response.id }).then(
|
||||
(events) => replaceModelsInStore('http_response_event', events),
|
||||
(events) => mergeModelsInStore('http_response_event', events),
|
||||
);
|
||||
}, [response?.id]);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { GrpcConnection, GrpcEvent } from '@yaakapp-internal/models';
|
||||
import {
|
||||
grpcConnectionsAtom,
|
||||
grpcEventsAtom,
|
||||
mergeModelsInStore,
|
||||
replaceModelsInStore,
|
||||
} from '@yaakapp-internal/models';
|
||||
import { atom, useAtomValue } from 'jotai';
|
||||
@@ -67,8 +68,10 @@ export function useGrpcEvents(connectionId: string | null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use merge instead of replace to preserve events that came in via model_write
|
||||
// while we were fetching from the database
|
||||
invoke<GrpcEvent[]>('models_grpc_events', { connectionId }).then((events) => {
|
||||
replaceModelsInStore('grpc_event', events);
|
||||
mergeModelsInStore('grpc_event', events);
|
||||
});
|
||||
}, [connectionId]);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import type { WebsocketConnection, WebsocketEvent } from '@yaakapp-internal/models';
|
||||
import {
|
||||
mergeModelsInStore,
|
||||
replaceModelsInStore,
|
||||
websocketConnectionsAtom,
|
||||
websocketEventsAtom,
|
||||
@@ -54,8 +55,10 @@ export function useWebsocketEvents(connectionId: string | null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use merge instead of replace to preserve events that came in via model_write
|
||||
// while we were fetching from the database
|
||||
invoke<WebsocketEvent[]>('models_websocket_events', { connectionId }).then(
|
||||
(events) => replaceModelsInStore('websocket_event', events),
|
||||
(events) => mergeModelsInStore('websocket_event', events),
|
||||
);
|
||||
}, [connectionId]);
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ function makeEvent(
|
||||
id: 'test',
|
||||
model: 'http_response_event',
|
||||
responseId: 'resp',
|
||||
createdAt: Date.now(),
|
||||
workspaceId: 'ws',
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
event: { type, name, value } as HttpResponseEvent['event'],
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user