Use dmon events in reload plugin
This commit is contained in:
parent
911a3cee08
commit
7dd5699c96
|
@ -1123,6 +1123,7 @@ function core.dir_rescan_add_job(dir, filepath)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- no-op but can be overrided by plugins
|
||||||
function core.on_dirmonitor_modify()
|
function core.on_dirmonitor_modify()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,11 @@ local Doc = require "core.doc"
|
||||||
|
|
||||||
local times = setmetatable({}, { __mode = "k" })
|
local times = setmetatable({}, { __mode = "k" })
|
||||||
|
|
||||||
local autoreload_scan_rate = 5
|
|
||||||
|
|
||||||
local function update_time(doc)
|
local function update_time(doc)
|
||||||
local info = system.get_file_info(doc.filename)
|
local info = system.get_file_info(doc.filename)
|
||||||
times[doc] = info.modified
|
times[doc] = info.modified
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function reload_doc(doc)
|
local function reload_doc(doc)
|
||||||
local fp = io.open(doc.filename, "r")
|
local fp = io.open(doc.filename, "r")
|
||||||
local text = fp:read("*a")
|
local text = fp:read("*a")
|
||||||
|
@ -28,23 +25,19 @@ local function reload_doc(doc)
|
||||||
core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename)
|
core.log_quiet("Auto-reloaded doc \"%s\"", doc.filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local on_modify = core.on_dirmonitor_modify
|
||||||
|
|
||||||
core.add_thread(function()
|
core.on_dirmonitor_modify = function(dir, filepath)
|
||||||
while true do
|
local abs_filename = dir.name .. PATHSEP .. filepath
|
||||||
-- check all doc modified times
|
for _, doc in ipairs(core.docs) do
|
||||||
for _, doc in ipairs(core.docs) do
|
local info = system.get_file_info(doc.filename or "")
|
||||||
local info = system.get_file_info(doc.filename or "")
|
if doc.abs_filename == abs_filename and info and times[doc] ~= info.modified then
|
||||||
if info and times[doc] ~= info.modified then
|
reload_doc(doc)
|
||||||
reload_doc(doc)
|
break
|
||||||
end
|
|
||||||
coroutine.yield()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- wait for next scan
|
|
||||||
coroutine.yield(autoreload_scan_rate)
|
|
||||||
end
|
end
|
||||||
end)
|
on_modify(dir, filepath)
|
||||||
|
end
|
||||||
|
|
||||||
-- patch `Doc.save|load` to store modified time
|
-- patch `Doc.save|load` to store modified time
|
||||||
local load = Doc.load
|
local load = Doc.load
|
||||||
|
|
Loading…
Reference in New Issue