mirror of
https://github.com/britzl/monarch.git
synced 2025-06-27 10:27:49 +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 M = {}
|
||||||
|
|
||||||
local CONTEXT = hash("monarch_context")
|
local CONTEXT = hash("monarch_context")
|
||||||
@ -403,6 +405,8 @@ function M.show(id, options, data, cb)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local callbacks = callback_tracker()
|
||||||
|
|
||||||
id = tohash(id)
|
id = tohash(id)
|
||||||
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(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
|
-- close all popups
|
||||||
while top.popup do
|
while top.popup do
|
||||||
stack[#stack] = nil
|
stack[#stack] = nil
|
||||||
show_out(top, screen)
|
show_out(top, screen, callbacks.track())
|
||||||
top = stack[#stack]
|
top = stack[#stack]
|
||||||
end
|
end
|
||||||
-- unload and transition out from top
|
-- unload and transition out from top
|
||||||
-- unless we're showing the same screen as is already visible
|
-- unless we're showing the same screen as is already visible
|
||||||
if top and top.id ~= screen.id then
|
if top and top.id ~= screen.id then
|
||||||
show_out(top, screen)
|
show_out(top, screen, callbacks.track())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -446,7 +450,9 @@ function M.show(id, options, data, cb)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- show screen
|
-- 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
|
return true
|
||||||
end
|
end
|
||||||
@ -462,6 +468,8 @@ function M.back(data, cb)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local callbacks = callback_tracker()
|
||||||
|
|
||||||
local screen = table.remove(stack)
|
local screen = table.remove(stack)
|
||||||
if screen then
|
if screen then
|
||||||
log("back()", screen.id)
|
log("back()", screen.id)
|
||||||
@ -473,7 +481,7 @@ function M.back(data, cb)
|
|||||||
if data then
|
if data then
|
||||||
top.data = data
|
top.data = data
|
||||||
end
|
end
|
||||||
back_in(top, screen, cb)
|
back_in(top, screen, callbacks.track())
|
||||||
end)
|
end)
|
||||||
else
|
else
|
||||||
back_out(screen, top)
|
back_out(screen, top)
|
||||||
@ -481,12 +489,13 @@ function M.back(data, cb)
|
|||||||
if data then
|
if data then
|
||||||
top.data = data
|
top.data = data
|
||||||
end
|
end
|
||||||
back_in(top, screen, cb)
|
back_in(top, screen, callbacks.track())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif cb then
|
|
||||||
cb()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if cb then callbacks.when_done(cb) end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
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