mirror of https://github.com/Ale32bit-CC/Node.lua
Fixed bug desyncing intervals and timeouts
This commit is contained in:
parent
3c058ebca8
commit
1fb6fe747d
29
node.lua
29
node.lua
|
@ -49,7 +49,7 @@ local function on(event, func)
|
||||||
listener.pid = spawn(function()
|
listener.pid = spawn(function()
|
||||||
while listener.run do
|
while listener.run do
|
||||||
local ev = {os.pullEvent(event)}
|
local ev = {os.pullEvent(event)}
|
||||||
listener.func(unpack(ev, 2))
|
spawn(function() listener.func(unpack(ev, 2)) end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -89,14 +89,11 @@ local function setInterval(func, s, ...)
|
||||||
interval.run = true
|
interval.run = true
|
||||||
interval.stopped = false
|
interval.stopped = false
|
||||||
interval.pid = spawn(function()
|
interval.pid = spawn(function()
|
||||||
while interval.run do
|
while true do
|
||||||
local timer = os.startTimer(interval.interval)
|
sleep(interval.interval)
|
||||||
local _, p = os.pullEvent("timer")
|
spawn(function()
|
||||||
if p == timer then
|
interval.func(unpack(interval.args))
|
||||||
spawn(function()
|
end)
|
||||||
interval.func(unpack(interval.args))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -135,14 +132,11 @@ local function setTimeout(func, s, ...)
|
||||||
interval.id = genHex()
|
interval.id = genHex()
|
||||||
interval.stopped = false
|
interval.stopped = false
|
||||||
interval.pid = spawn(function()
|
interval.pid = spawn(function()
|
||||||
local timer = os.startTimer(interval.timeout)
|
sleep(interval.timeout)
|
||||||
local _, p = os.pullEvent("timer")
|
spawn(function()
|
||||||
if p == timer then
|
interval.func(unpack(interval.args))
|
||||||
spawn(function()
|
interval.stopped = true
|
||||||
interval.func(unpack(interval.args))
|
end)
|
||||||
interval.stopped = true
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
interval = setmetatable(interval, {
|
interval = setmetatable(interval, {
|
||||||
|
@ -202,7 +196,6 @@ local function init()
|
||||||
for k,v in pairs(procs) do
|
for k,v in pairs(procs) do
|
||||||
c = c+1
|
c = c+1
|
||||||
end
|
end
|
||||||
|
|
||||||
return c > 0
|
return c > 0
|
||||||
end)() do
|
end)() do
|
||||||
local event = {coroutine.yield()}
|
local event = {coroutine.yield()}
|
||||||
|
|
Loading…
Reference in New Issue