Skip to content

Commit 36ee98d

Browse files
fix(channels): preserve concurrent draft updates when sendOrEditStreamMessage returns false (#112370)
* fix(channels): preserve concurrent draft updates when sendOrEditStreamMessage returns false * test(channels): relocate draft race coverage for clean merge --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 4b37a05 commit 36ee98d

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎src/channels/draft-stream-loop.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,75 @@ describe("createDraftStreamLoop", () => {
260260
expect(sendOrEditStreamMessage).toHaveBeenNthCalledWith(2, "hello");
261261
});
262262

263+
it("preserves concurrent update text when sendOrEditStreamMessage returns false", async () => {
264+
let deliver!: (() => void) | undefined;
265+
const deliverPromise = new Promise<void>((resolve) => {
266+
deliver = resolve;
267+
});
268+
const capturedArgs: string[] = [];
269+
270+
const sendOrEditStreamMessage = vi
271+
.fn<(text: string) => Promise<boolean>>()
272+
.mockImplementationOnce(async (text: string) => {
273+
capturedArgs.push(text);
274+
await deliverPromise;
275+
return false;
276+
})
277+
.mockImplementationOnce(async (text: string) => {
278+
capturedArgs.push(text);
279+
return true;
280+
});
281+
282+
const loop = createDraftStreamLoop({
283+
throttleMs: 0,
284+
isStopped: () => false,
285+
sendOrEditStreamMessage,
286+
});
287+
288+
loop.update("initial");
289+
await flushMicrotasks();
290+
291+
loop.update("concurrent");
292+
deliver!();
293+
await loop.flush();
294+
295+
expect(capturedArgs[1]).toBe("concurrent");
296+
});
297+
298+
it("preserves generic pending updates when consecutive sends return false", async () => {
299+
type Update = { text: string; blocks: string[] };
300+
const initial = { text: "initial", blocks: ["initial-blocks"] };
301+
const latest = { text: "latest", blocks: ["latest-blocks"] };
302+
let releaseFirst: (() => void) | undefined;
303+
const firstSend = new Promise<void>((resolve) => {
304+
releaseFirst = resolve;
305+
});
306+
const sendOrEditStreamMessage = vi
307+
.fn<(update: Update) => Promise<boolean>>()
308+
.mockImplementationOnce(async () => {
309+
await firstSend;
310+
return false;
311+
})
312+
.mockResolvedValueOnce(false)
313+
.mockResolvedValueOnce(true);
314+
const loop = createDraftStreamLoop<Update>({
315+
throttleMs: 0,
316+
isStopped: () => false,
317+
emptyValue: { text: "", blocks: [] },
318+
isEmpty: (update) => !update.text,
319+
sendOrEditStreamMessage,
320+
});
321+
322+
loop.update(initial);
323+
await flushMicrotasks();
324+
loop.update(latest);
325+
releaseFirst?.();
326+
await loop.flush();
327+
await loop.flush();
328+
329+
expect(sendOrEditStreamMessage.mock.calls).toEqual([[initial], [latest], [latest]]);
330+
});
331+
263332
it("keeps generic payload fields atomic while newer updates queue", async () => {
264333
type Update = { text: string; blocks: string[] };
265334
let releaseFirst: (() => void) | undefined;

‎src/channels/draft-stream-loop.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export function createDraftStreamLoop<T = string>(params: {
8484
throw err;
8585
}
8686
if (sent === false) {
87-
pendingValue = value;
87+
if (!hasPendingValue(pendingValue)) {
88+
pendingValue = value;
89+
}
8890
return;
8991
}
9092
lastSentAt = Date.now();

0 commit comments

Comments
 (0)