mirror of
https://github.com/britzl/monarch.git
synced 2025-11-26 19:00:53 +01:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07eacc7a5f | ||
|
|
5ee6ea5982 | ||
|
|
f590c75b9e | ||
|
|
d528947998 | ||
|
|
22f0e6cddf | ||
|
|
b13adcfb62 | ||
|
|
3947e86169 |
46
README.md
46
README.md
@@ -334,5 +334,51 @@ Check if Monarch is busy showing and/or hiding a screen.
|
|||||||
* ```busy``` (boolean) - True if busy hiding and/or showing a screen.
|
* ```busy``` (boolean) - True if busy hiding and/or showing a screen.
|
||||||
|
|
||||||
|
|
||||||
|
### monarch.add_listener([url])
|
||||||
|
Add a URL that will be notified of navigation events.
|
||||||
|
|
||||||
|
**PARAMETERS**
|
||||||
|
* ```url``` (url) - URL to send navigation events to. Will use current URL if omitted.
|
||||||
|
|
||||||
|
|
||||||
|
### monarch.remove_listener([url])
|
||||||
|
Remove a previously added listener.
|
||||||
|
|
||||||
|
**PARAMETERS**
|
||||||
|
* ```url``` (url) - URL to remove. Will use current URL if omitted.
|
||||||
|
|
||||||
|
|
||||||
### monarch.debug()
|
### monarch.debug()
|
||||||
Enable verbose logging of the internals of Monarch.
|
Enable verbose logging of the internals of Monarch.
|
||||||
|
|
||||||
|
|
||||||
|
### monarch.SCREEN_TRANSITION_IN_STARTED
|
||||||
|
Message sent to listeners when a screen has started to transition in.
|
||||||
|
|
||||||
|
**PARAMETERS**
|
||||||
|
* ```screen``` (hash) - Id of the screen
|
||||||
|
* ```previous_screen``` (hash) - Id of the previous screen (if any)
|
||||||
|
|
||||||
|
|
||||||
|
### monarch.SCREEN_TRANSITION_IN_FINISHED
|
||||||
|
Message sent to listeners when a screen has finished to transition in.
|
||||||
|
|
||||||
|
**PARAMETERS**
|
||||||
|
* ```screen``` (hash) - Id of the screen
|
||||||
|
* ```previous_screen``` (hash) - Id of the previous screen (if any)
|
||||||
|
|
||||||
|
|
||||||
|
### monarch.SCREEN_TRANSITION_OUT_STARTED
|
||||||
|
Message sent to listeners when a screen has started to transition out.
|
||||||
|
|
||||||
|
**PARAMETERS**
|
||||||
|
* ```screen``` (hash) - Id of the screen
|
||||||
|
* ```next_screen``` (hash) - Id of the next screen (if any)
|
||||||
|
|
||||||
|
|
||||||
|
### monarch.SCREEN_TRANSITION_OUT_FINISHED
|
||||||
|
Message sent to listeners when a screen has finished to transition out.
|
||||||
|
|
||||||
|
**PARAMETERS**
|
||||||
|
* ```screen``` (hash) - Id of the screen
|
||||||
|
* ```next_screen``` (hash) - Id of the next screen (if any)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
[project]
|
[project]
|
||||||
title = Monarch
|
title = Monarch
|
||||||
version = 0.9
|
version = 0.9
|
||||||
dependencies = https://github.com/britzl/deftest/archive/1.2.1.zip
|
dependencies = https://github.com/britzl/deftest/archive/2.3.0.zip
|
||||||
|
|
||||||
[bootstrap]
|
[bootstrap]
|
||||||
main_collection = /example/example.collectionc
|
main_collection = /example/example.collectionc
|
||||||
|
|||||||
@@ -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")
|
||||||
@@ -10,6 +12,7 @@ local ASYNC_LOAD = hash("async_load")
|
|||||||
local UNLOAD = hash("unload")
|
local UNLOAD = hash("unload")
|
||||||
local ENABLE = hash("enable")
|
local ENABLE = hash("enable")
|
||||||
|
|
||||||
|
-- transition messages
|
||||||
M.TRANSITION = {}
|
M.TRANSITION = {}
|
||||||
M.TRANSITION.DONE = hash("transition_done")
|
M.TRANSITION.DONE = hash("transition_done")
|
||||||
M.TRANSITION.SHOW_IN = hash("transition_show_in")
|
M.TRANSITION.SHOW_IN = hash("transition_show_in")
|
||||||
@@ -17,16 +20,27 @@ M.TRANSITION.SHOW_OUT = hash("transition_show_out")
|
|||||||
M.TRANSITION.BACK_IN = hash("transition_back_in")
|
M.TRANSITION.BACK_IN = hash("transition_back_in")
|
||||||
M.TRANSITION.BACK_OUT = hash("transition_back_out")
|
M.TRANSITION.BACK_OUT = hash("transition_back_out")
|
||||||
|
|
||||||
|
-- focus messages
|
||||||
M.FOCUS = {}
|
M.FOCUS = {}
|
||||||
M.FOCUS.GAINED = hash("monarch_focus_gained")
|
M.FOCUS.GAINED = hash("monarch_focus_gained")
|
||||||
M.FOCUS.LOST = hash("monarch_focus_lost")
|
M.FOCUS.LOST = hash("monarch_focus_lost")
|
||||||
|
|
||||||
|
-- listener messages
|
||||||
|
M.SCREEN_TRANSITION_IN_STARTED = hash("monarch_screen_transition_in_started")
|
||||||
|
M.SCREEN_TRANSITION_IN_FINISHED = hash("monarch_screen_transition_in_finished")
|
||||||
|
M.SCREEN_TRANSITION_OUT_STARTED = hash("monarch_screen_transition_out_started")
|
||||||
|
M.SCREEN_TRANSITION_OUT_FINISHED = hash("monarch_screen_transition_out_finished")
|
||||||
|
|
||||||
|
|
||||||
-- all registered screens
|
-- all registered screens
|
||||||
local screens = {}
|
local screens = {}
|
||||||
|
|
||||||
-- the current stack of screens
|
-- the current stack of screens
|
||||||
local stack = {}
|
local stack = {}
|
||||||
|
|
||||||
|
-- navigation listeners
|
||||||
|
local listeners = {}
|
||||||
|
|
||||||
-- the number of active transitions
|
-- the number of active transitions
|
||||||
-- monarch is considered busy while there are active transitions
|
-- monarch is considered busy while there are active transitions
|
||||||
local active_transition_count = 0
|
local active_transition_count = 0
|
||||||
@@ -46,9 +60,15 @@ local function tohash(s)
|
|||||||
return hash_lookup[s]
|
return hash_lookup[s]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function notify_listeners(message_id, message)
|
||||||
|
log("notify_listeners()", message_id)
|
||||||
|
for _,url in pairs(listeners) do
|
||||||
|
msg.post(url, message_id, message or {})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function screen_from_proxy(proxy)
|
local function screen_from_proxy(proxy)
|
||||||
for _, screen in pairs(screens) do
|
for _,screen in pairs(screens) do
|
||||||
if screen.proxy == proxy then
|
if screen.proxy == proxy then
|
||||||
return screen
|
return screen
|
||||||
end
|
end
|
||||||
@@ -57,7 +77,7 @@ end
|
|||||||
|
|
||||||
local function screen_from_script()
|
local function screen_from_script()
|
||||||
local url = msg.url()
|
local url = msg.url()
|
||||||
for _, screen in pairs(screens) do
|
for _,screen in pairs(screens) do
|
||||||
if screen.script == url then
|
if screen.script == url then
|
||||||
return screen
|
return screen
|
||||||
end
|
end
|
||||||
@@ -188,14 +208,14 @@ end
|
|||||||
local function focus_gained(screen, previous_screen)
|
local function focus_gained(screen, previous_screen)
|
||||||
log("focus_gained()", screen.id)
|
log("focus_gained()", screen.id)
|
||||||
if screen.focus_url then
|
if screen.focus_url then
|
||||||
msg.post(screen.focus_url, M.FOCUS.GAINED, {id = previous_screen and previous_screen.id})
|
msg.post(screen.focus_url, M.FOCUS.GAINED, { id = previous_screen and previous_screen.id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function focus_lost(screen, next_screen)
|
local function focus_lost(screen, next_screen)
|
||||||
log("focus_lost()", screen.id)
|
log("focus_lost()", screen.id)
|
||||||
if screen.focus_url then
|
if screen.focus_url then
|
||||||
msg.post(screen.focus_url, M.FOCUS.LOST, {id = next_screen and next_screen.id})
|
msg.post(screen.focus_url, M.FOCUS.LOST, { id = next_screen and next_screen.id })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -232,6 +252,7 @@ local function show_out(screen, next_screen, cb)
|
|||||||
local co
|
local co
|
||||||
co = coroutine.create(function()
|
co = coroutine.create(function()
|
||||||
active_transition_count = active_transition_count + 1
|
active_transition_count = active_transition_count + 1
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_OUT_STARTED, { screen = screen.id, next_screen = next_screen.id })
|
||||||
screen.co = co
|
screen.co = co
|
||||||
change_context(screen)
|
change_context(screen)
|
||||||
release_input(screen)
|
release_input(screen)
|
||||||
@@ -247,6 +268,7 @@ local function show_out(screen, next_screen, cb)
|
|||||||
screen.co = nil
|
screen.co = nil
|
||||||
active_transition_count = active_transition_count - 1
|
active_transition_count = active_transition_count - 1
|
||||||
if cb then cb() end
|
if cb then cb() end
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen.id })
|
||||||
end)
|
end)
|
||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
end
|
end
|
||||||
@@ -256,6 +278,7 @@ local function show_in(screen, previous_screen, reload, cb)
|
|||||||
local co
|
local co
|
||||||
co = coroutine.create(function()
|
co = coroutine.create(function()
|
||||||
active_transition_count = active_transition_count + 1
|
active_transition_count = active_transition_count + 1
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_IN_STARTED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
|
||||||
screen.co = co
|
screen.co = co
|
||||||
change_context(screen)
|
change_context(screen)
|
||||||
if reload and screen.loaded then
|
if reload and screen.loaded then
|
||||||
@@ -282,6 +305,7 @@ local function show_in(screen, previous_screen, reload, cb)
|
|||||||
screen.co = nil
|
screen.co = nil
|
||||||
active_transition_count = active_transition_count - 1
|
active_transition_count = active_transition_count - 1
|
||||||
if cb then cb() end
|
if cb then cb() end
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_IN_FINISHED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
|
||||||
end)
|
end)
|
||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
end
|
end
|
||||||
@@ -291,6 +315,7 @@ local function back_in(screen, previous_screen, cb)
|
|||||||
local co
|
local co
|
||||||
co = coroutine.create(function()
|
co = coroutine.create(function()
|
||||||
active_transition_count = active_transition_count + 1
|
active_transition_count = active_transition_count + 1
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_IN_STARTED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
|
||||||
screen.co = co
|
screen.co = co
|
||||||
change_context(screen)
|
change_context(screen)
|
||||||
if screen.preloaded then
|
if screen.preloaded then
|
||||||
@@ -310,6 +335,7 @@ local function back_in(screen, previous_screen, cb)
|
|||||||
screen.co = nil
|
screen.co = nil
|
||||||
active_transition_count = active_transition_count - 1
|
active_transition_count = active_transition_count - 1
|
||||||
if cb then cb() end
|
if cb then cb() end
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_IN_FINISHED, { screen = screen.id, previous_screen = previous_screen and previous_screen.id })
|
||||||
end)
|
end)
|
||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
end
|
end
|
||||||
@@ -318,6 +344,7 @@ local function back_out(screen, next_screen, cb)
|
|||||||
log("back_out()", screen.id)
|
log("back_out()", screen.id)
|
||||||
local co
|
local co
|
||||||
co = coroutine.create(function()
|
co = coroutine.create(function()
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_OUT_STARTED, { screen = screen.id, next_screen = next_screen.id })
|
||||||
active_transition_count = active_transition_count + 1
|
active_transition_count = active_transition_count + 1
|
||||||
screen.co = co
|
screen.co = co
|
||||||
change_context(screen)
|
change_context(screen)
|
||||||
@@ -328,6 +355,7 @@ local function back_out(screen, next_screen, cb)
|
|||||||
screen.co = nil
|
screen.co = nil
|
||||||
active_transition_count = active_transition_count - 1
|
active_transition_count = active_transition_count - 1
|
||||||
if cb then cb() end
|
if cb then cb() end
|
||||||
|
notify_listeners(M.SCREEN_TRANSITION_OUT_FINISHED, { screen = screen.id, next_screen = next_screen.id })
|
||||||
end)
|
end)
|
||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
end
|
end
|
||||||
@@ -373,8 +401,11 @@ end
|
|||||||
function M.show(id, options, data, cb)
|
function M.show(id, options, data, cb)
|
||||||
assert(id, "You must provide a screen id")
|
assert(id, "You must provide a screen id")
|
||||||
if M.is_busy() then
|
if M.is_busy() then
|
||||||
|
log("show() monarch is busy, ignoring request")
|
||||||
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)))
|
||||||
@@ -396,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
|
||||||
@@ -419,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
|
||||||
@@ -431,9 +464,12 @@ end
|
|||||||
-- @return true if successfully going back, false if busy performing another operation
|
-- @return true if successfully going back, false if busy performing another operation
|
||||||
function M.back(data, cb)
|
function M.back(data, cb)
|
||||||
if M.is_busy() then
|
if M.is_busy() then
|
||||||
|
log("back() monarch is busy, ignoring request")
|
||||||
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)
|
||||||
@@ -445,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)
|
||||||
@@ -453,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
|
||||||
|
|
||||||
@@ -549,6 +586,26 @@ function M.bottom(offset)
|
|||||||
return screen and screen.id
|
return screen and screen.id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function url_to_key(url)
|
||||||
|
return (url.socket or hash("")) .. (url.path or hash("")) .. (url.fragment or hash(""))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Add a listener to be notified of when screens are shown or hidden
|
||||||
|
-- @param url The url to notify, nil for current url
|
||||||
|
function M.add_listener(url)
|
||||||
|
url = url or msg.url()
|
||||||
|
listeners[url_to_key(url)] = url
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--- Remove a previously added listener
|
||||||
|
-- @param url The url to remove, nil for current url
|
||||||
|
function M.remove_listener(url)
|
||||||
|
url = url or msg.url()
|
||||||
|
listeners[url_to_key(url)] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function M.dump_stack()
|
function M.dump_stack()
|
||||||
local s = ""
|
local s = ""
|
||||||
|
|||||||
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
|
||||||
|
})
|
||||||
@@ -315,3 +315,43 @@ embedded_instances {
|
|||||||
z: 1.0
|
z: 1.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
embedded_instances {
|
||||||
|
id: "listener1"
|
||||||
|
data: ""
|
||||||
|
position {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
}
|
||||||
|
rotation {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
scale3 {
|
||||||
|
x: 1.0
|
||||||
|
y: 1.0
|
||||||
|
z: 1.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
embedded_instances {
|
||||||
|
id: "listener2"
|
||||||
|
data: ""
|
||||||
|
position {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
}
|
||||||
|
rotation {
|
||||||
|
x: 0.0
|
||||||
|
y: 0.0
|
||||||
|
z: 0.0
|
||||||
|
w: 1.0
|
||||||
|
}
|
||||||
|
scale3 {
|
||||||
|
x: 1.0
|
||||||
|
y: 1.0
|
||||||
|
z: 1.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
52
test/msg.lua
Normal file
52
test/msg.lua
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
local mock = require "deftest.mock.mock"
|
||||||
|
|
||||||
|
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
|
||||||
|
local recipients = {}
|
||||||
|
|
||||||
|
local history = {}
|
||||||
|
|
||||||
|
local function get_recipient(url)
|
||||||
|
recipients[url] = recipients[url] or {}
|
||||||
|
return recipients[url]
|
||||||
|
end
|
||||||
|
|
||||||
|
local function post(url, message_id, message)
|
||||||
|
local data = { url = url, message_id = message_id, message = message }
|
||||||
|
history[#history + 1] = data
|
||||||
|
local recipient = get_recipient(url)
|
||||||
|
recipient[#recipient + 1] = data
|
||||||
|
msg.post.original(url, message_id, message or {})
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.mock()
|
||||||
|
recipients = {}
|
||||||
|
history = {}
|
||||||
|
mock.mock(msg)
|
||||||
|
msg.post.replace(post)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.unmock()
|
||||||
|
mock.unmock(msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function M.messages(url)
|
||||||
|
return url and get_recipient(url) or history
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.first(url)
|
||||||
|
local messages = url and get_recipient(url) or history
|
||||||
|
return messages[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.last(url)
|
||||||
|
local messages = url and get_recipient(url) or history
|
||||||
|
return messages[#messages]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
return M
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
local cowait = require "test.cowait"
|
local cowait = require "test.cowait"
|
||||||
|
local mock_msg = require "test.msg"
|
||||||
|
local unload = require "deftest.util.unload"
|
||||||
local monarch = require "monarch.monarch"
|
local monarch = require "monarch.monarch"
|
||||||
|
|
||||||
local SCREEN1_STR = hash("screen1")
|
local SCREEN1_STR = hash("screen1")
|
||||||
@@ -55,12 +57,14 @@ return function()
|
|||||||
|
|
||||||
describe("monarch", function()
|
describe("monarch", function()
|
||||||
before(function()
|
before(function()
|
||||||
|
mock_msg.mock()
|
||||||
monarch = require "monarch.monarch"
|
monarch = require "monarch.monarch"
|
||||||
screens_instances = collectionfactory.create("#screensfactory")
|
screens_instances = collectionfactory.create("#screensfactory")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
after(function()
|
after(function()
|
||||||
package.loaded["monarch.monarch"] = nil
|
mock_msg.unmock()
|
||||||
|
unload.unload("monarch%..*")
|
||||||
for id,instance_id in pairs(screens_instances) do
|
for id,instance_id in pairs(screens_instances) do
|
||||||
go.delete(instance_id)
|
go.delete(instance_id)
|
||||||
end
|
end
|
||||||
@@ -229,5 +233,43 @@ return function()
|
|||||||
assert(monarch.is_busy())
|
assert(monarch.is_busy())
|
||||||
assert(wait_until_not_busy())
|
assert(wait_until_not_busy())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("should be able to notify listeners of navigation events", function()
|
||||||
|
local URL1 = msg.url(screens_instances[hash("/listener1")])
|
||||||
|
local URL2 = msg.url(screens_instances[hash("/listener2")])
|
||||||
|
monarch.add_listener(URL1)
|
||||||
|
monarch.add_listener(URL2)
|
||||||
|
|
||||||
|
monarch.show(SCREEN1)
|
||||||
|
assert(mock_msg.messages(URL1)[1].message_id == monarch.SCREEN_TRANSITION_IN_STARTED)
|
||||||
|
assert(mock_msg.messages(URL1)[1].message.screen == SCREEN1)
|
||||||
|
assert(mock_msg.messages(URL2)[1].message_id == monarch.SCREEN_TRANSITION_IN_STARTED)
|
||||||
|
assert(mock_msg.messages(URL2)[1].message.screen == SCREEN1)
|
||||||
|
assert(wait_until_not_busy())
|
||||||
|
assert(mock_msg.messages(URL1)[2].message_id == monarch.SCREEN_TRANSITION_IN_FINISHED)
|
||||||
|
assert(mock_msg.messages(URL1)[2].message.screen == SCREEN1)
|
||||||
|
assert(mock_msg.messages(URL2)[2].message_id == monarch.SCREEN_TRANSITION_IN_FINISHED)
|
||||||
|
assert(mock_msg.messages(URL2)[2].message.screen == SCREEN1)
|
||||||
|
|
||||||
|
monarch.remove_listener(URL2)
|
||||||
|
monarch.show(SCREEN2)
|
||||||
|
assert(wait_until_not_busy())
|
||||||
|
|
||||||
|
monarch.back()
|
||||||
|
assert(wait_until_not_busy())
|
||||||
|
|
||||||
|
local messages_1 = mock_msg.messages(URL1)
|
||||||
|
local messages_2 = mock_msg.messages(URL2)
|
||||||
|
assert(#mock_msg.messages(URL1) == 10)
|
||||||
|
assert(#mock_msg.messages(URL2) == 2)
|
||||||
|
assert(mock_msg.messages(URL1)[3].message_id == monarch.SCREEN_TRANSITION_OUT_STARTED)
|
||||||
|
assert(mock_msg.messages(URL1)[3].message.screen == SCREEN1)
|
||||||
|
assert(mock_msg.messages(URL1)[4].message_id == monarch.SCREEN_TRANSITION_IN_STARTED)
|
||||||
|
assert(mock_msg.messages(URL1)[4].message.screen == SCREEN2)
|
||||||
|
assert(mock_msg.messages(URL1)[5].message_id == monarch.SCREEN_TRANSITION_OUT_FINISHED)
|
||||||
|
assert(mock_msg.messages(URL1)[5].message.screen == SCREEN1)
|
||||||
|
assert(mock_msg.messages(URL1)[6].message_id == monarch.SCREEN_TRANSITION_IN_FINISHED)
|
||||||
|
assert(mock_msg.messages(URL1)[7].message.screen == SCREEN2)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user