Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion web/src/useKbEvents.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createInvalidationCoalescer } from "./useKbEvents";
import {
createInvalidationCoalescer,
createKbInvalidations,
OVERVIEW_SETTLE_MS,
SETTLE_MS,
} from "./useKbEvents";

describe("a burst of events buys one refetch", () => {
beforeEach(() => vi.useFakeTimers());
Expand Down Expand Up @@ -59,3 +64,56 @@ describe("a burst of events buys one refetch", () => {
expect(seen).toEqual([["pending", "kb"]]);
});
});

describe("the overview deadline and the fast keys", () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());

const overview = ["graph", "kb", "overview"];
const documents = ["documents", "kb"];

it("a fast key queued just before the deadline still waits out its own settle window", () => {
const seen: unknown[] = [];
const { push, pushGraph } = createKbInvalidations((key) => seen.push(key), "kb");
pushGraph();
vi.advanceTimersByTime(SETTLE_MS);
seen.length = 0;
vi.advanceTimersByTime(OVERVIEW_SETTLE_MS - SETTLE_MS - 10);
push(documents);
vi.advanceTimersByTime(10);
expect(seen).toEqual([overview]);
vi.advanceTimersByTime(SETTLE_MS - 10 - 1);
expect(seen).toEqual([overview]);
vi.advanceTimersByTime(1);
expect(seen).toEqual([overview, documents]);
});

it("the overview waits for its own deadline, not the fast window", () => {
const seen: unknown[] = [];
const { pushGraph } = createKbInvalidations((key) => seen.push(key), "kb");
pushGraph();
vi.advanceTimersByTime(SETTLE_MS);
expect(seen).toEqual([["graph", "kb", "neighborhood"]]);
vi.advanceTimersByTime(OVERVIEW_SETTLE_MS - SETTLE_MS);
expect(seen).toEqual([["graph", "kb", "neighborhood"], overview]);
});

it("a reconnect's broader graph key covers the queued overview", () => {
const seen: unknown[] = [];
const { push, pushGraph, flushAll } = createKbInvalidations((key) => seen.push(key), "kb");
pushGraph();
push(["graph", "kb"]);
flushAll();
expect(seen).toEqual([["graph", "kb"]]);
});

it("flushing everything delivers both layers and leaves no timer behind", () => {
const seen: unknown[] = [];
const { pushGraph, flushAll } = createKbInvalidations((key) => seen.push(key), "kb");
pushGraph();
flushAll();
expect(seen).toEqual([["graph", "kb", "neighborhood"], overview]);
vi.advanceTimersByTime(OVERVIEW_SETTLE_MS);
expect(seen).toEqual([["graph", "kb", "neighborhood"], overview]);
});
});
44 changes: 29 additions & 15 deletions web/src/useKbEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,42 @@ export function createInvalidationCoalescer(
return { push, flush };
}

/**
* 一个库的两层合并:快的键按 `SETTLE_MS`,全图 overview 按 `OVERVIEW_SETTLE_MS`。
*
* overview 到点只失效它自己(#1463):从前到点是 `push` 再 `flush`,快那层攒着的键
* 跟着提前失效,没等够自己的静默期。只有 `flushAll` 才经快的那层走:重连补上的
* `["graph", kbId]` 已覆盖 overview,不再多取一遍。
*/
export function createKbInvalidations(invalidate: (key: QueryKey) => void, kbId: string) {
const { push, flush } = createInvalidationCoalescer(invalidate, SETTLE_MS);
let flushing = false;
const overview = createInvalidationCoalescer((key) => {
if (flushing) push(key);
else invalidate(key);
}, OVERVIEW_SETTLE_MS);
const pushGraph = () => {
push(["graph", kbId, "neighborhood"]);
overview.push(["graph", kbId, "overview"]);
};
const flushAll = () => {
flushing = true;
overview.flush();
flushing = false;
flush();
};
return { push, pushGraph, flushAll };
}

export function useKbEvents(kbId: string | undefined) {
const queryClient = useQueryClient();

useEffect(() => {
if (!kbId) return;
const { push, flush } = createInvalidationCoalescer(
const { push, pushGraph, flushAll } = createKbInvalidations(
(key) => queryClient.invalidateQueries({ queryKey: key }),
SETTLE_MS,
kbId,
);
// overview 到点也经快的那层刷:重连补上的 ["graph", kbId] 已覆盖它,不再多取一遍
const overview = createInvalidationCoalescer((key) => {
push(key);
flush();
}, OVERVIEW_SETTLE_MS);
const pushGraph = () => {
push(["graph", kbId, "neighborhood"]);
overview.push(["graph", kbId, "overview"]);
};
const flushAll = () => {
overview.flush();
flush();
};

const unsubscribe = subscribeEvents(
`/api/v1/kbs/${kbId}/events`,
Expand Down
Loading