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

Added support for monarch.post()

This commit is contained in:
Björn Ritzl
2019-08-01 07:21:20 +02:00
parent c7981e77cf
commit 6b3cc11073
8 changed files with 132 additions and 5 deletions

View File

@@ -170,6 +170,8 @@ end
-- screen transitions
-- * focus_url - URL to a script that is to be notified of focus
-- lost/gained events
-- * receiver_url - URL to a script that is to receive messages sent
-- using monarch.send()
-- * timestep_below_popup - Timestep to set on proxy when below a popup
-- * auto_preload - true if the screen should be automatically preloaded
function M.register_proxy(id, proxy, settings)
@@ -178,6 +180,7 @@ function M.register_proxy(id, proxy, settings)
screen.proxy = proxy
screen.transition_url = settings and settings.transition_url
screen.focus_url = settings and settings.focus_url
screen.receiver_url = settings and settings.receiver_url
screen.auto_preload = settings and settings.auto_preload
if screen.auto_preload then
M.preload(id)
@@ -814,6 +817,43 @@ function M.unload(id, cb)
end
--- Post a message to a screen (using msg.post)
-- @param id (string|hash) Id of the screen to send message to
-- @param message_id (string|hash) Id of the message to send
-- @param message (table|nil) Optional message data to send
-- @return result (boolean) true if successful
-- @return error (string|nil) Error message if unable to send message
function M.post(id, message_id, message)
assert(id, "You must provide a screen id")
if not M.is_visible(id) then
return false, "Unable to post message to screen if it isn't visible"
end
assert(message_id, "You must provide a message_id")
id = tohash(id)
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
local screen = screens[id]
if screen.proxy then
if screen.receiver_url then
log("post() sending message to", screen.receiver_url)
msg.post(screen.receiver_url, message_id, message)
else
return false, "Unable to post message since screen has no receiver url specified"
end
else
run_coroutine(screen, nil, function()
change_context(screen)
log("post() sending message to", screen.receiver_url)
for id,instance in pairs(screen.factory_ids) do
msg.post(instance, message_id, message)
end
end)
end
return true
end
function M.on_message(message_id, message, sender)
if message_id == PROXY_LOADED then
local screen = screen_from_proxy(sender)

View File

@@ -7,6 +7,7 @@ go.property("popup_on_popup", false)
go.property("timestep_below_popup", 1)
go.property("transition_url", msg.url())
go.property("focus_url", msg.url())
go.property("receiver_url", msg.url())
go.property("preload", false)
@@ -22,6 +23,7 @@ function init(self)
popup_on_popup = self.popup_on_popup,
transition_url = self.transition_url ~= url and self.transition_url or nil,
focus_url = self.focus_url ~= url and self.focus_url or nil,
receiver_url = self.receiver_url ~= url and self.receiver_url or nil,
timestep_below_popup = self.timestep_below_popup,
auto_preload = self.preload,
}