@@ -26,7 +26,8 @@ import { realFilePath } from "../helpers/safePath.js";
2626import {
2727 STUDIO_SIGNATURE_MANIFEST_PATHS ,
2828 affectsProjectSignature ,
29- listProjectFiles ,
29+ listProjectFilesAsync ,
30+ type ListedProjectFile ,
3031} from "../helpers/projectSignature.js" ;
3132import { isSettling , openBlobStore , readBlob , SETTLE_MS , type BlobStore } from "./blobStore.js" ;
3233import { isUncopied , uncopiedOnlyHashes } from "./uncopied.js" ;
@@ -263,9 +264,9 @@ function withoutHiddenPaths(log: HistoryLog): HistoryLog {
263264 return log ;
264265}
265266
266- const historyFiles = ( dir : string ) =>
267- listProjectFiles ( dir ) . filter ( ( file ) => isHistoryPath ( file . path ) ) ;
268- type ListedFile = ReturnType < typeof listProjectFiles > [ number ] ;
267+ const historyFiles = async ( dir : string ) =>
268+ ( await listProjectFilesAsync ( dir ) ) . filter ( ( file ) => isHistoryPath ( file . path ) ) ;
269+ type ListedFile = ListedProjectFile ;
269270const COPY_LATER_ABOVE_BYTES = 1024 ** 2 ;
270271
271272const sameWho = ( a : HistoryWho , b : HistoryWho ) => a . kind === b . kind && a . name === b . name ;
@@ -366,13 +367,29 @@ function keepTargetsOfItsFiles(entry: HistoryEntry): HistoryEntry {
366367 return entry ;
367368}
368369
370+ /** The tracked files; any change owes the project's video-state.json a rewrite. */
371+ class TrackedFiles extends Map < string , Tracked > {
372+ constructor ( private readonly changed : ( ) => void ) {
373+ super ( ) ;
374+ }
375+ override set ( path : string , file : Tracked ) : this {
376+ this . changed ( ) ;
377+ return super . set ( path , file ) ;
378+ }
379+ override delete ( path : string ) : boolean {
380+ this . changed ( ) ;
381+ return super . delete ( path ) ;
382+ }
383+ }
384+
369385class Engine {
370386 readonly dir : string ;
371387 readonly foldsCase : boolean ;
372388 readonly folder : FolderIdentity & { dev : number } ;
373389 readonly home : string ;
374390 log : HistoryLog = { baseline : new Map ( ) , entries : [ ] , pins : new Set ( ) } ;
375- tracked = new Map < string , Tracked > ( ) ;
391+ owesVideoState = true ;
392+ tracked = new TrackedFiles ( ( ) => ( this . owesVideoState = true ) ) ;
376393 windows : Group [ ] = [ ] ;
377394 outside : Group | null = null ;
378395 /** A coalescing claim, open until another key, its idle timer, an operation, a window, or another write. */
@@ -426,15 +443,19 @@ class Engine {
426443 try {
427444 return await task ( ) ;
428445 } finally {
429- if ( this . whereFolder ( ) === "here" ) {
446+ if ( this . owesVideoState && this . whereFolder ( ) === "here" ) {
447+ this . owesVideoState = false ;
430448 await writeHistoryState (
431449 this . dir ,
432450 this . projectId ,
433451 this . home ,
434452 this . log . entries ,
435453 this . manifest ( ) ,
436454 manifestAt ( this . log , this . log . entries . at ( - 1 ) ?. id ?? START ) ?? new Map ( ) ,
437- ) . catch ( ( error ) => this . options . onError ?.( error ) ) ;
455+ ) . catch ( ( error ) => {
456+ this . owesVideoState = true ;
457+ this . options . onError ?.( error ) ;
458+ } ) ;
438459 }
439460 }
440461 } ) ;
@@ -457,6 +478,7 @@ class Engine {
457478 }
458479
459480 persistLog ( record ?: LogRecord ) : void {
481+ this . owesVideoState = true ;
460482 if ( record ) saveRecord ( this . logFile , this . log , record ) ;
461483 else writeLog ( this . logFile , this . log ) ;
462484 if ( ! existsSync ( join ( this . home , "project.json" ) ) )
@@ -491,7 +513,7 @@ class Engine {
491513 const left = this . readAdopting ( ) ;
492514 const unadopted = ( file : { path : string } ) =>
493515 left . has ( file . path ) && ! this . tracked . has ( file . path ) ;
494- this . adoptMediaInBackground ( historyFiles ( this . dir ) . filter ( unadopted ) , this . now ( ) ) ;
516+ this . adoptMediaInBackground ( ( await historyFiles ( this . dir ) ) . filter ( unadopted ) , this . now ( ) ) ;
495517 // What changed while the project was closed is one outside entry, or the closed window's.
496518 this . reopenClosedWindow ( ) ;
497519 await this . settleAll ( ) ;
@@ -554,7 +576,7 @@ class Engine {
554576 async takeInUnnamedLedger ( ) : Promise < void > {
555577 const named = ( path : string ) =>
556578 this . log . entries . some ( ( entry ) => entry . files . some ( ( file ) => file . path === path ) ) ;
557- const ledger = historyFiles ( this . dir ) . find ( ( file ) => file . path === MEDIA_LEDGER ) ;
579+ const ledger = ( await historyFiles ( this . dir ) ) . find ( ( file ) => file . path === MEDIA_LEDGER ) ;
558580 if ( ledger && ! this . log . baseline . has ( MEDIA_LEDGER ) && ! named ( MEDIA_LEDGER ) ) {
559581 const copied = await this . storeIfPresent ( MEDIA_LEDGER ) ;
560582 if ( typeof copied === "object" ) this . log . baseline . set ( MEDIA_LEDGER , copied . hash ) ;
@@ -566,7 +588,7 @@ class Engine {
566588
567589 async firstOpen ( ) : Promise < void > {
568590 const sweptAt = this . now ( ) ;
569- const files = historyFiles ( this . dir ) ;
591+ const files = await historyFiles ( this . dir ) ;
570592 const copied = files . filter ( ( file ) => file . size <= COPY_LATER_ABOVE_BYTES ) ;
571593 const hashes = await Promise . allSettled (
572594 copied . map ( ( file ) => inFirstCopySlot ( ( ) => this . storeIfPresent ( file . path ) ) ) ,
@@ -690,7 +712,7 @@ class Engine {
690712 const sweptAt = this . now ( ) ;
691713 const changedAt = ( file : { mtimeMs : number ; ctimeMs : number } ) =>
692714 Math . min ( sweptAt , Math . max ( file . mtimeMs , file . ctimeMs ) ) ;
693- const seen = historyFiles ( this . dir ) . filter ( ( file ) => ! this . adopting . has ( file . path ) ) ;
715+ const seen = ( await historyFiles ( this . dir ) ) . filter ( ( file ) => ! this . adopting . has ( file . path ) ) ;
694716 const heard = new Map ( this . overwritten ) ;
695717 const present = new Set ( seen . map ( ( file ) => file . path ) ) ;
696718 const removed = [ ...this . tracked . keys ( ) ]
0 commit comments