3
0
mirror of https://github.com/britzl/monarch.git synced 2025-09-28 02:22:20 +02:00

Track when callbacks are invoked so that we can ensure that all screens are shown/hidden before invoking callbacks

This commit is contained in:
Björn Ritzl
2018-06-21 11:33:20 +02:00
parent 5ee6ea5982
commit 07eacc7a5f
2 changed files with 55 additions and 7 deletions

View File

@@ -0,0 +1,39 @@
local M = {}
function M.create()
local instance = {}
local callback = nil
local callback_count = 0
--- Create a callback function and track when it is done
-- @return Callback function
function instance.track()
callback_count = callback_count + 1
return function()
callback_count = callback_count - 1
if callback_count == 0 and callback then
callback()
end
end
end
--- Call a function when all callbacks have been triggered
-- @param cb Function to call when all
function instance.when_done(cb)
callback = cb
if callback_count == 0 then
callback()
end
end
return instance
end
return setmetatable(M, {
__call = function(_, ...)
return M.create(...)
end
})