perf(clients): saving the thread list cache no longer freezes the UI (#13767)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-09-26 01:35:22 -07:00
1 parent 1d6f23b519
commit 9151ea407a
5 files changed
+122 -13

No files matched your search

@@ -1,6 +1,7 @@
import {
ConnectionPersistenceError,
EnvironmentCacheStore,
encodeShellSnapshotForCache,
} from "@t3tools/client-runtime/platform";
import {
type EnvironmentId,
@@ -56,7 +57,6 @@ const StoredVcsRefs = Schema.Struct({
const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(
Schema.fromJsonString(StoredShellSnapshot),
);
const encodeStoredShellSnapshot = Schema.encodeEffect(Schema.fromJsonString(StoredShellSnapshot));
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(
Schema.fromJsonString(StoredThreadSnapshot),
);
@@ -136,11 +136,18 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
}).pipe(Effect.tap(() => Effect.promise(() => projectFaviconDatabaseCache.hydrate()))),
),
saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(function* (environmentId, snapshot) {
const payload = yield* encodeStoredShellSnapshot({
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
environmentId,
snapshot,
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
const encodedSnapshot = yield* encodeShellSnapshotForCache(snapshot).pipe(
Effect.mapError((cause) => persistenceError("save-shell", cause)),
);
const payload = yield* Effect.try({
try: () =>
JSON.stringify({
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
environmentId,
snapshot: encodedSnapshot,
} satisfies typeof StoredShellSnapshot.Encoded),
catch: (cause) => persistenceError("save-shell", cause),
});
yield* database
.saveCache(environmentId, "shell", "snapshot", SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION, payload)
.pipe(Effect.mapError(mapDatabaseError("save-shell")));
+23
View File
@@ -87,6 +87,29 @@ describe("makeCatalogBackend", () => {
expect(setConnectionCatalog).toHaveBeenCalledWith("{}");
}),
);
it.effect("fails IndexedDB writes whose commit aborts", () =>
Effect.gen(function* () {
vi.stubGlobal("window", {});
const transaction = Object.assign(new EventTarget(), {
error: null as DOMException | null,
objectStore: () => ({
put: () => {
// A failed commit aborts the transaction without an "error" event.
queueMicrotask(() => {
transaction.error = new DOMException("Quota exceeded", "QuotaExceededError");
transaction.dispatchEvent(new Event("abort"));
});
},
}),
});
const backend = makeCatalogBackend({ transaction: () => transaction } as never);
const error = yield* backend.write("{}").pipe(Effect.flip);
expect(error.message).toContain("QuotaExceededError");
}),
);
});
describe("browser GitHub routing permissions", () => {
+15 -7
View File
@@ -6,6 +6,7 @@ import {
ConnectionTargetStore,
EMPTY_CONNECTION_CATALOG_DOCUMENT,
EnvironmentCacheStore,
encodeShellSnapshotForCache,
putRemoteDpopTokenInCatalog,
registerConnectionInCatalog,
removeCatalogValue,
@@ -91,7 +92,6 @@ const ConnectionCatalogDocumentJson = Schema.fromJsonString(ConnectionCatalogDoc
const decodeConnectionCatalogDocument = Schema.decodeUnknownEffect(ConnectionCatalogDocumentJson);
const encodeConnectionCatalogDocument = Schema.encodeEffect(ConnectionCatalogDocumentJson);
const decodeStoredShellSnapshot = Schema.decodeUnknownEffect(StoredShellSnapshotJson);
const encodeStoredShellSnapshot = Schema.encodeEffect(StoredShellSnapshotJson);
const decodeStoredThreadSnapshot = Schema.decodeUnknownEffect(StoredThreadSnapshotJson);
const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson);
const decodeStoredServerConfig = Schema.decodeUnknownEffect(StoredServerConfigJson);
@@ -188,7 +188,9 @@ function writeDatabaseValue(
) {
return Effect.callback<void, ConnectionTransientError>((resume) => {
const transaction = database.transaction(storeName, "readwrite");
transaction.addEventListener("error", () => {
// Every failed write fires "abort". A failed commit, such as
// QuotaExceededError, fires only "abort" and no "error".
transaction.addEventListener("abort", () => {
resume(
Effect.fail(catalogError("write", transaction.error ?? "Unknown IndexedDB write error")),
);
@@ -606,11 +608,17 @@ export const connectionStorageLayer = Layer.effectContext(
),
saveShell: (environmentId, snapshot) =>
Effect.gen(function* () {
const encoded = yield* encodeStoredShellSnapshot({
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
environmentId,
snapshot,
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
const encodedSnapshot = yield* encodeShellSnapshotForCache(snapshot);
const encoded = yield* Effect.try({
try: () =>
// @effect-diagnostics-next-line preferSchemaOverJson:off - the snapshot is already encoded.
JSON.stringify({
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
environmentId,
snapshot: encodedSnapshot,
} satisfies typeof StoredShellSnapshot.Encoded),
catch: (cause) => persistenceError("save-shell", cause),
});
yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
}).pipe(
Effect.mapError((cause) =>
@@ -0,0 +1,55 @@
import {
OrchestrationProjectShell,
OrchestrationShellSnapshot,
OrchestrationThreadShell,
} from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Arr from "effect/Array";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as Arbitrary from "effect/unstable/arbitrary/Arbitrary";
import { encodeShellSnapshotForCache } from "./persistence.ts";
// Generated values can hold untrimmed strings, which a decoded value never
// has. One encode and decode gives a value a client can hold; values that
// fail are dropped. Size 30 makes the generator fill optional fields.
const sampleDecoded = <S extends Schema.Constraint>(schema: S) =>
Effect.gen(function* () {
const encode = Schema.encodeEffect(schema);
const decode = Schema.decodeEffect(schema);
const generated = yield* Arbitrary.sampleEffect(Arbitrary.schema(schema), {
count: 1000,
size: 30,
});
const decoded = yield* Effect.forEach(generated, (value) =>
encode(value).pipe(Effect.flatMap(decode), Effect.option),
);
return Arr.getSomes(decoded);
});
const encodeSnapshot = Schema.encodeEffect(OrchestrationShellSnapshot);
describe("encodeShellSnapshotForCache", () => {
it.effect("matches the Schema encoding of a generated snapshot", () =>
Effect.gen(function* () {
const threads = yield* sampleDecoded(OrchestrationThreadShell);
const projects = yield* sampleDecoded(OrchestrationProjectShell);
const snapshot: OrchestrationShellSnapshot = {
snapshotSequence: 1,
// The generator rarely makes monogram icons, and they are the one
// project field whose encoding differs from the decoded value.
projects: projects.map((project, index) =>
index % 2 === 0
? { ...project, projectIcon: { kind: "monogram", text: "T3", color: "blue" } }
: project,
),
threads,
updatedAt: "2026-09-25T00:00:00.000Z",
};
expect(threads.length).toBeGreaterThan(0);
expect(projects.length).toBeGreaterThan(0);
expect(yield* encodeShellSnapshotForCache(snapshot)).toEqual(yield* encodeSnapshot(snapshot));
}),
);
});
@@ -1,5 +1,6 @@
import {
type EnvironmentId,
OrchestrationProjectShell,
type OrchestrationShellSnapshot,
type OrchestrationThreadDetailSnapshot,
type ServerConfig,
@@ -130,6 +131,21 @@ export class EnvironmentCacheStore extends Context.Service<
}
>()("@t3tools/client-runtime/platform/persistence/EnvironmentCacheStore") {}
const encodeProjectShells = Schema.encodeEffect(Schema.Array(OrchestrationProjectShell));
/**
* Encodes a shell snapshot for `EnvironmentCacheStore.saveShell`. The result
* equals `Schema.encode(OrchestrationShellSnapshot)`, so the cache format does
* not change. Walking thousands of threads through Schema blocks the UI
* thread, and a decoded thread shell is already in its encoded form, so only
* the projects go through Schema: their icon has a real encode transform.
*/
export const encodeShellSnapshotForCache = (snapshot: OrchestrationShellSnapshot) =>
Effect.map(
encodeProjectShells(snapshot.projects),
(projects) => ({ ...snapshot, projects }) satisfies typeof OrchestrationShellSnapshot.Encoded,
);
export class EnvironmentOwnedDataCleanup extends Context.Reference<{
readonly clear: (environmentId: EnvironmentId) => Effect.Effect<void>;
}>("@t3tools/client-runtime/platform/persistence/EnvironmentOwnedDataCleanup", {