fix(objects): avoid repeated object list reloads
This commit is contained in:
8 files changed
+390
-48
No files matched your search
@@ -10,6 +10,14 @@ describe("ObjectBrowser context watcher", () => {
|
||||
expect(objectBrowserSource).not.toContain("() => [props.connection.id, props.database, props.schema]");
|
||||
});
|
||||
|
||||
it("invalidates object requests before waiting for the new connection", () => {
|
||||
const watcher = objectBrowserSource.match(/watch\(\s*\[\(\) => props\.connection\.id[\s\S]*?\{ immediate: true \},\s*\);/)?.[0] ?? "";
|
||||
|
||||
expect(watcher).toContain("const contextEpoch = objectBrowserRowsLoadGuard.invalidate();");
|
||||
expect(watcher.indexOf("objectBrowserRowsLoadGuard.invalidate()")).toBeLessThan(watcher.indexOf("await connectionStore.ensureConnected"));
|
||||
expect(watcher).toContain("reload({ allowCachedObjects: true, contextEpoch })");
|
||||
});
|
||||
|
||||
it("ignores connection object replacement when the context values stay unchanged", async () => {
|
||||
const connection = ref({ id: "connection-1" });
|
||||
const database = ref("database-1");
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createObjectBrowserRowsLoadGuard } from "@/lib/table/objectBrowserRowsLoadGuard";
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>((resolvePromise) => {
|
||||
resolve = resolvePromise;
|
||||
});
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
describe("ObjectBrowserRowsLoadGuard", () => {
|
||||
it.each([
|
||||
{
|
||||
name: "connection",
|
||||
nextScope: { connectionId: "new", database: "db1", schema: "public" },
|
||||
},
|
||||
{
|
||||
name: "database",
|
||||
nextScope: { connectionId: "old", database: "db2", schema: "public" },
|
||||
},
|
||||
])("drops statistics that finish after switching $name", async ({ nextScope }) => {
|
||||
const guard = createObjectBrowserRowsLoadGuard();
|
||||
const oldRequest = guard.start({ connectionId: "old", database: "db1", schema: "public" });
|
||||
const pendingStatistics = deferred<number>();
|
||||
const cacheWrites: Array<{ connectionId: string; database: string }> = [];
|
||||
const oldStatisticsTask = (async () => {
|
||||
await pendingStatistics.promise;
|
||||
if (!guard.isCurrent(oldRequest)) return;
|
||||
cacheWrites.push(oldRequest.scope);
|
||||
})();
|
||||
|
||||
guard.invalidate();
|
||||
const currentRequest = guard.start(nextScope);
|
||||
pendingStatistics.resolve(1);
|
||||
await oldStatisticsTask;
|
||||
|
||||
expect(guard.isCurrent(oldRequest)).toBe(false);
|
||||
expect(guard.isCurrent(currentRequest)).toBe(true);
|
||||
expect(oldRequest.scope).toMatchObject({ connectionId: "old", database: "db1" });
|
||||
expect(cacheWrites).toEqual([]);
|
||||
});
|
||||
|
||||
it("captures an immutable copy of the complete cache scope", () => {
|
||||
const guard = createObjectBrowserRowsLoadGuard();
|
||||
const scope = { connectionId: "c1", database: "db1", schema: "public", catalog: "catalog1" };
|
||||
const request = guard.start(scope);
|
||||
|
||||
scope.database = "db2";
|
||||
|
||||
expect(request.scope).toEqual({ connectionId: "c1", database: "db1", schema: "public", catalog: "catalog1" });
|
||||
expect(Object.isFrozen(request.scope)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user