diff options
-rw-r--r-- | script-beta/await.lua | 23 | ||||
-rw-r--r-- | script-beta/config.lua | 1 | ||||
-rw-r--r-- | script-beta/provider/diagnostic.lua | 34 |
3 files changed, 57 insertions, 1 deletions
diff --git a/script-beta/await.lua b/script-beta/await.lua index 558c273e..50406918 100644 --- a/script-beta/await.lua +++ b/script-beta/await.lua @@ -9,6 +9,7 @@ m.coMap = setmetatable({}, { __mode = 'k' }) m.idMap = {} m.delayQueue = {} m.delayQueueIndex = 1 +m.watchList = {} m._enable = true --- 设置错误处理器 @@ -89,6 +90,11 @@ function m.close(id) log.debug('Close await:', id, count) end +function m.hasID(id, co) + co = co or coroutine.running() + return m.idMap[id] and m.idMap[id][co] ~= nil +end + --- 休眠一段时间 ---@param time number function m.sleep(time) @@ -139,6 +145,9 @@ function m.delay() end local co = coroutine.running() local current = m.coMap[co] + if m.onWatch('delay', co) == false then + return + end -- TODO if current.priority then return @@ -184,4 +193,18 @@ function m.disable() m._enable = false end +--- 注册事件 +function m.watch(callback) + m.watchList[#m.watchList+1] = callback +end + +function m.onWatch(ev, ...) + for _, callback in ipairs(m.watchList) do + local res = callback(ev, ...) + if res ~= nil then + return res + end + end +end + return m diff --git a/script-beta/config.lua b/script-beta/config.lua index aeea415b..4cd394f7 100644 --- a/script-beta/config.lua +++ b/script-beta/config.lua @@ -113,6 +113,7 @@ local ConfigTemplate = { Hash(String, String), }, workspaceDelay = {0, Integer}, + workspaceRate = {100, Integer}, }, workspace = { ignoreDir = {{}, Str2Hash ';'}, diff --git a/script-beta/provider/diagnostic.lua b/script-beta/provider/diagnostic.lua index 0e80daf5..fe014b5f 100644 --- a/script-beta/provider/diagnostic.lua +++ b/script-beta/provider/diagnostic.lua @@ -11,6 +11,7 @@ local ws = require 'workspace' local m = {} m._start = false m.cache = {} +m.sleepRest = 0.0 local function concat(t, sep) if type(t) ~= 'table' then @@ -203,10 +204,11 @@ function m.diagnosticsAll() await.close 'diagnosticsAll' await.call(function () await.sleep(delay) + m.diagnosticsAllClock = os.clock() local clock = os.clock() for uri in files.eachFile() do - await.delay() m.doDiagnostic(uri) + await.delay() end log.debug('全文诊断耗时:', os.clock() - clock) end, 'files.version', 'diagnosticsAll') @@ -217,6 +219,30 @@ function m.start() m.diagnosticsAll() end +function m.onDelay() + if not await.hasID 'diagnosticsAll' then + return + end + local currentClock = os.clock() + local passed = currentClock - m.diagnosticsAllClock + local speedRate = config.config.diagnostics.workspaceRate + if speedRate <= 0 or speedRate >= 100 then + return + end + local sleepTime = passed * (100 - speedRate) / speedRate + m.sleepRest + m.sleepRest = 0.0 + if sleepTime < 0.001 then + return + end + if sleepTime > 0.1 then + m.sleepRest = sleepTime - 0.1 + sleepTime = 0.1 + end + await.sleep(sleepTime) + m.diagnosticsAllClock = os.clock() + return false +end + files.watch(function (ev, uri) if ev == 'remove' then m.clear(uri) @@ -227,4 +253,10 @@ files.watch(function (ev, uri) end end) +await.watch(function (ev, co) + if ev == 'delay' then + return m.onDelay() + end +end) + return m |