Skip to content

Commit

Permalink
move filewatch into C
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Feb 6, 2023
1 parent 5eee353 commit 4e56523
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
30 changes: 4 additions & 26 deletions script/filewatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ m._watchings = {}
---@async
---@param path string
---@param recursive boolean
---@param filter? async fun(path: string):boolean
---@param filter? fun(path: string):boolean
function m.watch(path, recursive, filter)
if path == '' or not fs.is_directory(fs.path(path)) then
return function () end
Expand All @@ -47,31 +47,9 @@ function m.watch(path, recursive, filter)
watch:add(path)
log.debug('Watch add:', path)
if recursive then
local count = 1
---@async
local function scanDirctory(dir)
count = count + 1
if count % 10 == 0 then
await.delay()
if count % 100 == 0 then
log.warn('Watching so many dirs:', count, dir:string())
end
end
for fullpath, status in fs.pairs(dir) do
local st = status:type()
if st == 'directory'
or st == 'symlink'
or st == 'junction' then
if not filter or filter(fullpath:string()) then
watch:add(fullpath:string())
log.trace('Watch add:', fullpath:string())
xpcall(scanDirctory, log.error, fullpath)
end
end
end
end

xpcall(scanDirctory, log.error, fs.path(path))
watch:set_filter(filter)
watch:set_follow_symlinks(true)
watch:set_recursive(true)
end
m._watchings[path] = {
count = 1,
Expand Down
2 changes: 1 addition & 1 deletion script/meta/bee/filesystem.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local fsStatus = {}
function fsStatus:type()
end

---@class fs
---@class bee.filesystem
local fs = {}

---@class fs.copy_options
Expand Down
32 changes: 32 additions & 0 deletions script/meta/bee/filewatch.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---@meta

---@class bee.filewatch.instance
local instance = {}

---@param path string
function instance:add(path)
end

---@param enable boolean
---@return boolean
function instance:set_recursive(enable)
end

---@param enable boolean
---@return boolean
function instance:set_follow_symlinks(enable)
end

---@param callback? fun(path: string):boolean
---@return boolean
function instance:set_filter(callback)
end

---@class bee.filewatch
local fw = {}

---@return bee.filewatch.instance
function fw.create()
end

return fw

0 comments on commit 4e56523

Please sign in to comment.