@devries-cameron20 / t3code-1 / commits / 26a7df089d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): the file save coordinator compiles without manual memoization (#20697)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 21:48 UTC 26a7df089dcfba2c113b85b26b2c21deb10e30d6
1 file changed +57 -27
apps/web/src/components/files/useFileSaveCoordinator.ts
+57 -27
@@ -1,5 +1,5 @@
1 import { AuthFilesystemWriteScope, type EnvironmentId } from "@t3tools/contracts";
2 -import { createRef, useEffect, useMemo } from "react";
2 +import { createRef, useEffect, useState } from "react";
3
4 import { projectEnvironment } from "~/state/projects";
5 import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
@@ -27,32 +27,24 @@ export function useFileSaveCoordinator({
27 onPendingChange,
28 }: FileSaveOptions): Pick<FileSaveCoordinator, "change"> {
29 const canWriteFiles = useEnvironmentScope(environmentId, AuthFilesystemWriteScope);
30 - const writeFile = useAtomCommand(projectEnvironment.writeFile);
31 - const session = useMemo(() => {
32 - const coordinatorRef = createRef<FileSaveCoordinator>();
33 - return {
34 - change: (contents: string) => coordinatorRef.current?.change(contents),
35 - setup: () => {
36 - const coordinator = new FileSaveCoordinator({
37 - debounceMs: FILE_SAVE_DEBOUNCE_MS,
38 - canPersist: () => readEnvironmentScope(environmentId, AuthFilesystemWriteScope),
39 - onPendingChange: (pending) => onPendingChange(relativePath, pending),
40 - persist: (nextContents) =>
41 - writeFile({
42 - environmentId,
43 - input: { cwd, relativePath, contents: nextContents },
44 - }),
45 - onConfirmed: (confirmedContents) =>
46 - confirmProjectFileQueryData(environmentId, cwd, relativePath, confirmedContents),
47 - });
48 - coordinatorRef.current = coordinator;
49 - return () => {
50 - coordinatorRef.current = null;
51 - coordinator.dispose();
52 - };
53 - },
54 - };
55 - }, [cwd, environmentId, onPendingChange, relativePath, writeFile]);
30 + const writeFile = useProjectFileWriter();
31 + // A session owns the pending debounce, so it must outlive rerenders and be
32 + // replaced only when what it writes to changes.
33 + const inputs = { environmentId, cwd, relativePath, onPendingChange, writeFile };
34 + const [current, setCurrent] = useState(() => ({
35 + inputs,
36 + session: createFileSaveSession(inputs),
37 + }));
38 + if (
39 + current.inputs.environmentId !== environmentId ||
40 + current.inputs.cwd !== cwd ||
41 + current.inputs.relativePath !== relativePath ||
42 + current.inputs.onPendingChange !== onPendingChange ||
43 + current.inputs.writeFile !== writeFile
44 + ) {
45 + setCurrent({ inputs, session: createFileSaveSession(inputs) });
46 + }
47 + const session = current.session;
48
49 // StrictMode replays effect setup. Retired file sessions stay inert, while the
50 // replay gets a fresh coordinator instead of reusing a disposed one.
@@ -72,3 +64,41 @@ export function useFileSaveCoordinator({
64 }, [canWriteFiles, cwd, environmentId, relativePath, session]);
65 return session;
66 }
67 +
68 +function useProjectFileWriter() {
69 + return useAtomCommand(projectEnvironment.writeFile);
70 +}
71 +
72 +function createFileSaveSession({
73 + environmentId,
74 + cwd,
75 + relativePath,
76 + onPendingChange,
77 + writeFile,
78 +}: FileSaveOptions & {
79 + writeFile: ReturnType<typeof useProjectFileWriter>;
80 +}) {
81 + const coordinatorRef = createRef<FileSaveCoordinator>();
82 + return {
83 + change: (contents: string) => coordinatorRef.current?.change(contents),
84 + setup: () => {
85 + const coordinator = new FileSaveCoordinator({
86 + debounceMs: FILE_SAVE_DEBOUNCE_MS,
87 + canPersist: () => readEnvironmentScope(environmentId, AuthFilesystemWriteScope),
88 + onPendingChange: (pending) => onPendingChange(relativePath, pending),
89 + persist: (nextContents) =>
90 + writeFile({
91 + environmentId,
92 + input: { cwd, relativePath, contents: nextContents },
93 + }),
94 + onConfirmed: (confirmedContents) =>
95 + confirmProjectFileQueryData(environmentId, cwd, relativePath, confirmedContents),
96 + });
97 + coordinatorRef.current = coordinator;
98 + return () => {
99 + coordinatorRef.current = null;
100 + coordinator.dispose();
101 + };
102 + },
103 + };
104 +}