11local log = require (" nvim-tree.log" )
22local git = require (" nvim-tree.git" )
33local utils = require (" nvim-tree.utils" )
4+ local notify = require (" nvim-tree.notify" )
45local Watcher = require (" nvim-tree.watcher" ).Watcher
56
67local M = {
@@ -69,10 +70,30 @@ function M.create_watcher(node)
6970 --- @param watcher Watcher
7071 local function callback (watcher )
7172 log .line (" watcher" , " node event scheduled refresh %s" , watcher .data .context )
73+
74+ -- event is awaiting debouncing and handling
75+ watcher .data .outstanding_events = watcher .data .outstanding_events + 1
76+
77+ -- disable watcher when outstanding exceeds max
78+ if watcher .data .outstanding_events > M .config .filesystem_watchers .max_outstanding_events then
79+ notify .error (string.format (
80+ " Observed %d consecutive file system events with interval < %dms, exceeding filesystem_watchers.max_events=%s. Disabling watcher for directory '%s'. Consider adding this directory to filesystem_watchers.ignore_dirs" ,
81+ watcher .data .outstanding_events ,
82+ M .config .filesystem_watchers .max_outstanding_events ,
83+ M .config .filesystem_watchers .debounce_delay ,
84+ node .absolute_path
85+ ))
86+ node :destroy_watcher ()
87+ end
88+
7289 utils .debounce (watcher .data .context , M .config .filesystem_watchers .debounce_delay , function ()
7390 if watcher .destroyed then
7491 return
7592 end
93+
94+ -- event has been handled
95+ watcher .data .outstanding_events = 0
96+
7697 if node .link_to then
7798 log .line (" watcher" , " node event executing refresh '%s' -> '%s'" , node .link_to , node .absolute_path )
7899 else
@@ -87,7 +108,8 @@ function M.create_watcher(node)
87108 path = path ,
88109 callback = callback ,
89110 data = {
90- context = " explorer:watch:" .. path .. " :" .. M .uid
111+ context = " explorer:watch:" .. path .. " :" .. M .uid ,
112+ outstanding_events = 0 , -- unprocessed events that have not been debounced
91113 }
92114 })
93115end
0 commit comments