summaryrefslogtreecommitdiff
path: root/script/task.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-23 00:05:30 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-23 00:05:30 +0800
commit6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444 (patch)
treefdc22d78150fd1c5edc46732c8b151ccfefb519f /script/task.lua
parentd0ff66c9abe9d6abbca12fd811e0c3cb69c1033a (diff)
downloadlua-language-server-6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444.zip
正路目录
Diffstat (limited to 'script/task.lua')
-rw-r--r--script/task.lua64
1 files changed, 64 insertions, 0 deletions
diff --git a/script/task.lua b/script/task.lua
new file mode 100644
index 00000000..bb1a7922
--- /dev/null
+++ b/script/task.lua
@@ -0,0 +1,64 @@
+local mt = {}
+mt.__index = mt
+mt.type = 'task'
+
+function mt:remove()
+ if self._removed then
+ return
+ end
+ self._removed = true
+ coroutine.close(self.task)
+end
+
+function mt:isRemoved()
+ return self._removed
+end
+
+function mt:step()
+ if self._removed then
+ return
+ end
+ local suc, res = coroutine.resume(self.task)
+ if not suc then
+ self:remove()
+ log.error(debug.traceback(self.task, res))
+ return
+ end
+ if coroutine.status(self.task) == 'dead' then
+ self:remove()
+ end
+ return res
+end
+
+function mt:fastForward()
+ if self._removed then
+ return
+ end
+ while true do
+ local suc = coroutine.resume(self.task)
+ if not suc then
+ self:remove()
+ break
+ end
+ if coroutine.status(self.task) == 'dead' then
+ self:remove()
+ break
+ end
+ end
+end
+
+function mt:set(key, value)
+ self.data[key] = value
+end
+
+function mt:get(key)
+ return self.data[key]
+end
+
+return function (callback)
+ local self = setmetatable({
+ data = {},
+ task = coroutine.create(callback),
+ }, mt)
+ return self
+end