Fixed bug desyncing intervals and timeouts

This commit is contained in:
Alessandro 2019-01-27 16:46:13 +01:00 committed by GitHub
parent 3c058ebca8
commit 1fb6fe747d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 18 deletions

View File

@ -49,7 +49,7 @@ local function on(event, func)
listener.pid = spawn(function()
while listener.run do
local ev = {os.pullEvent(event)}
listener.func(unpack(ev, 2))
spawn(function() listener.func(unpack(ev, 2)) end)
end
end)
@ -89,14 +89,11 @@ local function setInterval(func, s, ...)
interval.run = true
interval.stopped = false
interval.pid = spawn(function()
while interval.run do
local timer = os.startTimer(interval.interval)
local _, p = os.pullEvent("timer")
if p == timer then
spawn(function()
interval.func(unpack(interval.args))
end)
end
while true do
sleep(interval.interval)
spawn(function()
interval.func(unpack(interval.args))
end)
end
end)
@ -135,14 +132,11 @@ local function setTimeout(func, s, ...)
interval.id = genHex()
interval.stopped = false
interval.pid = spawn(function()
local timer = os.startTimer(interval.timeout)
local _, p = os.pullEvent("timer")
if p == timer then
spawn(function()
interval.func(unpack(interval.args))
interval.stopped = true
end)
end
sleep(interval.timeout)
spawn(function()
interval.func(unpack(interval.args))
interval.stopped = true
end)
end)
interval = setmetatable(interval, {
@ -202,7 +196,6 @@ local function init()
for k,v in pairs(procs) do
c = c+1
end
return c > 0
end)() do
local event = {coroutine.yield()}