diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-09-19 19:59:22 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-09-19 19:59:22 +0800 |
commit | 039b11fc0b7387ca5e19b20b33d839d42d3068aa (patch) | |
tree | 4443514545c4449365dd5d73f78490d96e8ed24a /server-beta/src/task.lua | |
parent | 2d923e462ace05fdeb3c194a67797e1adca65153 (diff) | |
download | lua-language-server-039b11fc0b7387ca5e19b20b33d839d42d3068aa.zip |
更新
Diffstat (limited to 'server-beta/src/task.lua')
-rw-r--r-- | server-beta/src/task.lua | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/server-beta/src/task.lua b/server-beta/src/task.lua index fa44b7c6..23b6ae2f 100644 --- a/server-beta/src/task.lua +++ b/server-beta/src/task.lua @@ -1,7 +1,54 @@ -local thread = require 'bee.thread' +local timer = require 'timer' ---@class task local m = {} m.type = 'task' -return m +--- 设置错误处理器 +---@param errHandle function {comment = '当有错误发生时,会以错误堆栈为参数调用该函数'} +function m.setErrorHandle(errHandle) + m.errorHandle = errHandle +end + +--- 创建一个任务 +function m.create(callback) + coroutine.create(callback) +end + +--- 休眠一段时间 +---@param time number +function m.sleep(time) + local co, main = coroutine.running() + if main then + if m.errorHandle then + m.errorHandle(debug.traceback('Cant sleep in main thread')) + end + return + end + timer.wait(time, function () + local suc, err = coroutine.resume(co) + if not suc and m.errHandle then + m.errHandle(debug.traceback(co, err)) + end + end) + coroutine.yield() +end + +--- 等待直到唤醒 +---@param waker function +function m.wait(waker) + local co, main = coroutine.running() + if main then + if m.errorHandle then + m.errorHandle(debug.traceback('Cant wait in main thread')) + end + return + end + waker(function () + local suc, err = coroutine.resume(co) + if not suc and m.errHandle then + m.errHandle(debug.traceback(co, err)) + end + end) + coroutine.yield() +end |