summaryrefslogtreecommitdiff
path: root/xmake/core/base/timer.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-12-11 00:46:15 +0800
committerruki <[email protected]>2019-12-10 22:37:34 +0800
commitb6dd6f29f65cab54cfbec2ec234c05abd8ab46bd (patch)
tree2a23fda12bde5a8728a9f918657410512cf39ea1 /xmake/core/base/timer.lua
parent965ce42e75869cdec94570a5411b4acd163abe20 (diff)
impl scheduler.sleep
Diffstat (limited to 'xmake/core/base/timer.lua')
-rw-r--r--xmake/core/base/timer.lua33
1 files changed, 28 insertions, 5 deletions
diff --git a/xmake/core/base/timer.lua b/xmake/core/base/timer.lua
index 28490f01c..7c81f1dd2 100644
--- a/xmake/core/base/timer.lua
+++ b/xmake/core/base/timer.lua
@@ -37,7 +37,7 @@ end
-- post timer task after delay and will be auto-remove it after be expired
function timer:post(func, delay, opt)
- return self:post_at(task, os.mclock() + delay, delay, opt)
+ return self:post_at(func, os.mclock() + delay, delay, opt)
end
-- post timer task at the absolute time and will be auto-remove it after be expired
@@ -58,7 +58,7 @@ end
-- get the delay of next task
function timer:delay()
- local delay = -1
+ local delay = nil
local tasks = self:_tasks()
if tasks:length() > 0 then
local task = tasks:peek()
@@ -70,9 +70,32 @@ function timer:delay()
return delay
end
--- run the timer loop
-function timer:runloop()
- -- TODO
+-- run the timer next loop
+function timer:next()
+ local tasks = self:_tasks()
+ while tasks:length() > 0 do
+ local triggered = false
+ local task = tasks:peek()
+ if task then
+ -- timeout?
+ local now = os.mclock()
+ if task.when <= now then
+ tasks:pop()
+ if task.continuous and not task.cancel then
+ task.when = now + task.period
+ tasks:push(task)
+ end
+ -- run timer task
+ if task.func then
+ task.func(task.cancel)
+ triggered = true
+ end
+ end
+ end
+ if not triggered then
+ break
+ end
+ end
end
-- get timer name