mirror of
https://github.com/britzl/monarch.git
synced 2025-09-28 10:32:21 +02:00
Make sure that callbacks aren't invoked more than once
This commit is contained in:
47
monarch/utils/async.lua
Normal file
47
monarch/utils/async.lua
Normal file
@@ -0,0 +1,47 @@
|
||||
local M = {}
|
||||
|
||||
local NOT_STARTED = "not_started"
|
||||
local YIELDED = "yielded"
|
||||
local RESUMED = "resumed"
|
||||
local RUNNING = "running"
|
||||
|
||||
function M.async(fn)
|
||||
local co = coroutine.running()
|
||||
|
||||
local state = NOT_STARTED
|
||||
|
||||
local function await(fn, ...)
|
||||
state = RUNNING
|
||||
fn(...)
|
||||
if state ~= RUNNING then
|
||||
return
|
||||
end
|
||||
state = YIELDED
|
||||
local r = { coroutine.yield() }
|
||||
return unpack(r)
|
||||
end
|
||||
|
||||
local function resume(...)
|
||||
if state ~= RUNNING then
|
||||
state = RUNNING
|
||||
local ok, err = coroutine.resume(co, ...)
|
||||
if not ok then
|
||||
print(err)
|
||||
print(debug.traceback())
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if co then
|
||||
return fn(await, resume)
|
||||
else
|
||||
co = coroutine.create(fn)
|
||||
return resume(await, resume)
|
||||
end
|
||||
end
|
||||
|
||||
return setmetatable(M, {
|
||||
__call = function(t, ...)
|
||||
return M.async(...)
|
||||
end
|
||||
})
|
Reference in New Issue
Block a user