@@ -34,8 +34,11 @@ local fs_watching_stopped = false
34
34
--- @param on_error function #Function to run on error
35
35
local function do_watch (path , on_event , on_error )
36
36
local handle = uv .new_fs_event ()
37
+ if not handle then
38
+ return
39
+ end
37
40
local unwatch_cb = function ()
38
- uv . fs_event_stop ( handle )
41
+ handle : stop ( )
39
42
end
40
43
local event_cb = function (err , filename )
41
44
if err then
@@ -44,8 +47,8 @@ local function do_watch(path, on_event, on_error)
44
47
on_event (filename , unwatch_cb )
45
48
end
46
49
end
47
- uv . fs_event_start ( handle , tostring (path ), {}, event_cb )
48
- table.insert ( handles , handle )
50
+ handle : start ( tostring (path ), {}, event_cb )
51
+ handles [ # handles + 1 ] = handle
49
52
end
50
53
51
54
--- Gets all directories in the library_dir
@@ -54,7 +57,7 @@ local function get_library_dirs()
54
57
local library_dir = Path (db .config :get_value ({ id = 1 }, " dir" ))
55
58
local library_dirs = {}
56
59
for path in library_dir :fs_iterdir () do
57
- table.insert ( library_dirs , path )
60
+ library_dirs [ # library_dirs + 1 ] = path
58
61
end
59
62
return library_dirs
60
63
end
@@ -79,14 +82,16 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
79
82
log .debug (" Filesystem event in the library root directory" )
80
83
entry_dir = Path (dir_to_watch , filename )
81
84
info_path = entry_dir / info_name
85
+ local entry_dir_str = tostring (entry_dir )
86
+ local info_path_str = tostring (info_path )
82
87
if entry_dir :exists () and entry_dir :is_dir () then
83
- log .debug (string.format (" Filesystem event: path '%s' added" , tostring ( entry_dir ) ))
84
- init_fs_watcher (tostring ( entry_dir ) )
88
+ log .debug (string.format (" Filesystem event: path '%s' added" , entry_dir_str ))
89
+ init_fs_watcher (entry_dir_str )
85
90
if info_path :exists () then
86
- mtime = fs_stat (tostring ( info_path ) ).mtime .sec
91
+ mtime = fs_stat (info_path_str ).mtime .sec
87
92
end
88
93
elseif entry_dir :is_dir () then
89
- log .debug (string.format (" Filesystem event: path '' removed" , tostring ( entry_dir ) ))
94
+ log .debug (string.format (" Filesystem event: path '' removed" , entry_dir_str ))
90
95
-- don't update here, because we'll catch it below under entry events
91
96
do_update = false
92
97
else
@@ -97,24 +102,26 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
97
102
log .debug (" Filesystem event in entry directory" )
98
103
entry_dir = Path (dir_to_watch )
99
104
info_path = entry_dir / info_name
105
+ local info_path_str = tostring (info_path )
100
106
if info_path :exists () then
101
107
-- info file exists, update with new info
102
- log .debug (string.format (" Filesystem event: '%s' changed" , tostring ( info_path ) ))
108
+ log .debug (string.format (" Filesystem event: '%s' changed" , info_path_str ))
103
109
mtime = fs_stat (tostring (info_path )).mtime .sec
104
110
elseif not entry_dir :exists () then
105
111
-- info file and entry dir don't exist. delete entry (mtime = nil) and remove watcher
106
- log .debug (string.format (" Filesystem event: '%s' removed" , tostring ( info_path ) ))
112
+ log .debug (string.format (" Filesystem event: '%s' removed" , info_path_str ))
107
113
do_unwatch = true
108
114
else
109
115
-- info file doesn't exist but entry dir does. delete entry but keep watcher
110
- log .debug (string.format (" Filesystem event: '%s' removed" , tostring ( info_path ) ))
116
+ log .debug (string.format (" Filesystem event: '%s' removed" , info_path_str ))
111
117
end
112
118
end
113
119
if do_update then
120
+ local info_path_str = tostring (info_path )
114
121
log .debug (" Update database for this fs event..." )
115
- log .debug (" Updating: " .. vim .inspect ({ path = tostring ( info_path ) , mtime = mtime }))
122
+ log .debug (" Updating: " .. vim .inspect ({ path = info_path_str , mtime = mtime }))
116
123
vim .defer_fn (function ()
117
- data .update_db ({ path = tostring ( info_path ) , mtime = mtime })
124
+ data .update_db ({ path = info_path_str , mtime = mtime })
118
125
end , 200 )
119
126
elseif do_unwatch then
120
127
log .debug (" Removing watcher" )
@@ -225,7 +232,7 @@ function M.stop()
225
232
if not vim .tbl_isempty (handles ) then
226
233
log .trace (" Stopping the fs watchers" )
227
234
for _ , handle in ipairs (handles ) do
228
- uv . fs_event_stop ( handle )
235
+ handle : stop ( )
229
236
end
230
237
handles = {}
231
238
db .state :set_fw_running ()
0 commit comments