mirror of
https://github.com/britzl/monarch.git
synced 2025-06-27 02:17:53 +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:
parent
5ee6ea5982
commit
07eacc7a5f
@ -1,3 +1,5 @@
|
||||
local callback_tracker = require "monarch.utils.callback_tracker"
|
||||
|
||||
local M = {}
|
||||
|
||||
local CONTEXT = hash("monarch_context")
|
||||
@ -402,6 +404,8 @@ function M.show(id, options, data, cb)
|
||||
log("show() monarch is busy, ignoring request")
|
||||
return false
|
||||
end
|
||||
|
||||
local callbacks = callback_tracker()
|
||||
|
||||
id = tohash(id)
|
||||
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
|
||||
@ -423,13 +427,13 @@ function M.show(id, options, data, cb)
|
||||
-- close all popups
|
||||
while top.popup do
|
||||
stack[#stack] = nil
|
||||
show_out(top, screen)
|
||||
show_out(top, screen, callbacks.track())
|
||||
top = stack[#stack]
|
||||
end
|
||||
-- unload and transition out from top
|
||||
-- unless we're showing the same screen as is already visible
|
||||
if top and top.id ~= screen.id then
|
||||
show_out(top, screen)
|
||||
show_out(top, screen, callbacks.track())
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -446,7 +450,9 @@ function M.show(id, options, data, cb)
|
||||
end
|
||||
|
||||
-- show screen
|
||||
show_in(screen, top, options and options.reload, cb)
|
||||
show_in(screen, top, options and options.reload, callbacks.track())
|
||||
|
||||
if cb then callbacks.when_done(cb) end
|
||||
|
||||
return true
|
||||
end
|
||||
@ -462,6 +468,8 @@ function M.back(data, cb)
|
||||
return false
|
||||
end
|
||||
|
||||
local callbacks = callback_tracker()
|
||||
|
||||
local screen = table.remove(stack)
|
||||
if screen then
|
||||
log("back()", screen.id)
|
||||
@ -473,7 +481,7 @@ function M.back(data, cb)
|
||||
if data then
|
||||
top.data = data
|
||||
end
|
||||
back_in(top, screen, cb)
|
||||
back_in(top, screen, callbacks.track())
|
||||
end)
|
||||
else
|
||||
back_out(screen, top)
|
||||
@ -481,12 +489,13 @@ function M.back(data, cb)
|
||||
if data then
|
||||
top.data = data
|
||||
end
|
||||
back_in(top, screen, cb)
|
||||
back_in(top, screen, callbacks.track())
|
||||
end
|
||||
end
|
||||
elseif cb then
|
||||
cb()
|
||||
end
|
||||
|
||||
if cb then callbacks.when_done(cb) end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
39
monarch/utils/callback_tracker.lua
Normal file
39
monarch/utils/callback_tracker.lua
Normal 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
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user