@@ -41,6 +41,25 @@ interface WatcherEntry {
4141 close : ( ) => void
4242}
4343
44+ const readSequences = new WeakMap < BrowserWindow , Map < string , number > > ( )
45+ const nextRead = ( win : BrowserWindow , pathname : string , type : WatchType ) : number => {
46+ const key = `${ type } :${ pathname } `
47+ let sequences = readSequences . get ( win )
48+ if ( ! sequences ) {
49+ sequences = new Map ( )
50+ readSequences . set ( win , sequences )
51+ }
52+ const sequence = ( sequences . get ( key ) ?? 0 ) + 1
53+ sequences . set ( key , sequence )
54+ return sequence
55+ }
56+ const isLatestRead = (
57+ win : BrowserWindow ,
58+ pathname : string ,
59+ type : WatchType ,
60+ sequence : number
61+ ) : boolean => readSequences . get ( win ) ?. get ( `${ type } :${ pathname } ` ) === sequence
62+
4463const add = async (
4564 win : BrowserWindow ,
4665 pathname : string ,
@@ -50,6 +69,7 @@ const add = async(
5069 trimTrailingNewline : number ,
5170 autoNormalizeLineEndings : boolean
5271) : Promise < void > => {
72+ const sequence = nextRead ( win , pathname , type )
5373 const stats = await fsPromises . stat ( pathname )
5474 const birthTime = stats . birthtime
5575 const mtimeMs = stats . mtimeMs
@@ -94,6 +114,7 @@ const add = async(
94114 return
95115 }
96116 }
117+ if ( ! isLatestRead ( win , pathname , type , sequence ) ) return
97118 win . webContents . send ( EVENT_NAME [ type ] , {
98119 type : 'add' ,
99120 change : file
@@ -102,6 +123,7 @@ const add = async(
102123}
103124
104125const unlink = ( win : BrowserWindow , pathname : string , type : WatchType ) : void => {
126+ nextRead ( win , pathname , type )
105127 const file = { pathname }
106128 win . webContents . send ( EVENT_NAME [ type ] , {
107129 type : 'unlink' ,
@@ -118,6 +140,7 @@ const change = async(
118140 trimTrailingNewline : number ,
119141 autoNormalizeLineEndings : boolean
120142) : Promise < void > => {
143+ const sequence = nextRead ( win , pathname , type )
121144 if ( type === 'dir' ) {
122145 // Only send mtimeMs so the sidebar can re-sort; skip loading file content.
123146 try {
@@ -136,10 +159,17 @@ const change = async(
136159 if ( isMarkdown ) {
137160 try {
138161 const [ data , stats ] = await Promise . all ( [
139- loadMarkdownFile ( pathname , endOfLine , autoGuessEncoding , trimTrailingNewline , autoNormalizeLineEndings ) ,
162+ loadMarkdownFile (
163+ pathname ,
164+ endOfLine ,
165+ autoGuessEncoding ,
166+ trimTrailingNewline ,
167+ autoNormalizeLineEndings
168+ ) ,
140169 fsPromises . stat ( pathname )
141170 ] )
142171 const file = { pathname, data, mtimeMs : stats . mtimeMs }
172+ if ( ! isLatestRead ( win , pathname , type , sequence ) ) return
143173 win . webContents . send ( 'mt::update-file' , {
144174 type : 'change' ,
145175 change : file
@@ -198,9 +228,8 @@ class Watcher {
198228 }
199229
200230 watch ( win : BrowserWindow , watchPath : string , type : WatchType = 'dir' ) : ( ) => void {
201- const usePolling = isOsx || isUncPath ( watchPath )
202- ? true
203- : this . _preferences . getItem < boolean > ( 'watcherUsePolling' )
231+ const usePolling =
232+ isOsx || isUncPath ( watchPath ) ? true : this . _preferences . getItem < boolean > ( 'watcherUsePolling' )
204233
205234 const id = getUniqueId ( )
206235
@@ -301,9 +330,7 @@ class Watcher {
301330 . on ( 'addDir' , ( pathname : string ) => addDir ( win , pathname , type ) )
302331 . on ( 'unlinkDir' , ( pathname : string ) => unlinkDir ( win , pathname , type ) )
303332 . on ( 'raw' , ( event : string , subpath : string , details : unknown ) => {
304- if (
305- globalThis . MARKTEXT_DEBUG_VERBOSE >= 3
306- ) {
333+ if ( globalThis . MARKTEXT_DEBUG_VERBOSE >= 3 ) {
307334 console . log ( 'watcher: ' , event , subpath , details )
308335 }
309336
@@ -443,9 +470,7 @@ class Watcher {
443470 try {
444471 const fileInfo = await fsPromises . stat ( pathname )
445472 if ( fileInfo . mtime . getTime ( ) - start . getTime ( ) < duration ) {
446- if (
447- globalThis . MARKTEXT_DEBUG_VERBOSE >= 3
448- ) {
473+ if ( globalThis . MARKTEXT_DEBUG_VERBOSE >= 3 ) {
449474 console . log (
450475 `Ignoring file event after "stat": current="${ currentTime . toISOString ( ) } ", start="${ start . toISOString ( ) } ", file="${ fileInfo . mtime . toISOString ( ) } ".`
451476 )
0 commit comments