11import { AuthFilesystemWriteScope , type EnvironmentId } from "@t3tools/contracts" ;
2- import { createRef , useEffect , useMemo } from "react" ;
2+ import { createRef , useEffect , useState } from "react" ;
33
44import { projectEnvironment } from "~/state/projects" ;
55import { readEnvironmentScope , useEnvironmentScope } from "~/state/session" ;
@@ -27,32 +27,24 @@ export function useFileSaveCoordinator({
2727 onPendingChange,
2828} : FileSaveOptions ) : Pick < FileSaveCoordinator , "change" > {
2929 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 ;
5648
5749 // StrictMode replays effect setup. Retired file sessions stay inert, while the
5850 // replay gets a fresh coordinator instead of reusing a disposed one.
@@ -72,3 +64,41 @@ export function useFileSaveCoordinator({
7264 } , [ canWriteFiles , cwd , environmentId , relativePath , session ] ) ;
7365 return session ;
7466}
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+ }
0 commit comments