summaryrefslogtreecommitdiff
path: root/server-beta/src/pub
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-09-19 21:15:27 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-09-19 21:15:27 +0800
commitfc87521bd165f4d1d273d01749b13b841c61595d (patch)
tree2e143f1fe0509ebfa7536c28180b02478d55783c /server-beta/src/pub
parentaa4c8b521fc5a8417725a23dfc3d68bc8aae01d3 (diff)
downloadlua-language-server-fc87521bd165f4d1d273d01749b13b841c61595d.zip
更新
Diffstat (limited to 'server-beta/src/pub')
-rw-r--r--server-beta/src/pub/brave.lua33
1 files changed, 30 insertions, 3 deletions
diff --git a/server-beta/src/pub/brave.lua b/server-beta/src/pub/brave.lua
index 7662a39e..1102e8a3 100644
--- a/server-beta/src/pub/brave.lua
+++ b/server-beta/src/pub/brave.lua
@@ -1,26 +1,53 @@
local thread = require 'bee.thread'
+local work = require 'work'
---@class pub_brave
local m = {}
m.type = 'pub.brave'
+m.ability = {}
--- 注册成为勇者
function m.register(id)
m.taskpad = thread.channel('taskpad' .. id)
m.waiter = thread.channel('waiter' .. id)
+ m.id = id
+ work.init()
m.start()
end
+--- 注册能力
+function m.on(name, callback)
+ m.ability[name] = callback
+end
+
+--- 报告
+function m.push(name, params)
+ m.waiter:push(name, params)
+end
+
--- 开始找工作
function m.start()
while true do
local suc, name, id, params = m.taskpad:pop()
if not suc then
-- 找不到工作的勇者,只好睡觉
- thread.sleep(0.01)
+ thread.sleep(0.001)
+ goto CONTINUE
+ end
+ local ability = m.ability[name]
+ -- TODO
+ if not ability then
+ m.waiter:push(id)
+ log.error('Brave can not handle this work: ' .. name)
+ goto CONTINUE
+ end
+ local suc, res = xpcall(ability, log.error, params)
+ if not suc then
+ m.waiter:push(id)
+ goto CONTINUE
end
- local result = require(name)(params)
- m.waiter:push(id, result)
+ m.waiter:push(id, res)
+ ::CONTINUE::
end
end