mirror of
https://github.com/britzl/monarch.git
synced 2025-06-27 10:27:49 +02:00
pcall in the callback tracker
This commit is contained in:
parent
7e2ff2990c
commit
7d986ada1b
@ -7,15 +7,20 @@ function M.create()
|
|||||||
local callback = nil
|
local callback = nil
|
||||||
local callback_count = 0
|
local callback_count = 0
|
||||||
|
|
||||||
|
local function invoke_if_done()
|
||||||
|
if callback_count == 0 and callback then
|
||||||
|
local ok, err = pcall(callback)
|
||||||
|
if not ok then print(err) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Create a callback function and track when it is done
|
--- Create a callback function and track when it is done
|
||||||
-- @return Callback function
|
-- @return Callback function
|
||||||
function instance.track()
|
function instance.track()
|
||||||
callback_count = callback_count + 1
|
callback_count = callback_count + 1
|
||||||
return function()
|
return function()
|
||||||
callback_count = callback_count - 1
|
callback_count = callback_count - 1
|
||||||
if callback_count == 0 and callback then
|
invoke_if_done()
|
||||||
callback()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -23,9 +28,7 @@ function M.create()
|
|||||||
-- @param cb Function to call when all
|
-- @param cb Function to call when all
|
||||||
function instance.when_done(cb)
|
function instance.when_done(cb)
|
||||||
callback = cb
|
callback = cb
|
||||||
if callback_count == 0 then
|
invoke_if_done()
|
||||||
callback()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
Loading…
x
Reference in New Issue
Block a user