Skip to content

Commit e824922

Browse files
committed
Differentiate messages and wait for constants
1 parent 2eb67bf commit e824922

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

monarch/monarch.lua

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ local async = require "monarch.utils.async"
33

44
local M = {}
55

6-
local CONTEXT = hash("monarch_context")
7-
local PROXY_LOADED = hash("proxy_loaded")
8-
local PROXY_UNLOADED = hash("proxy_unloaded")
9-
local LAYOUT_CHANGED = hash("layout_changed")
10-
11-
local RELEASE_INPUT_FOCUS = hash("release_input_focus")
12-
local ACQUIRE_INPUT_FOCUS = hash("acquire_input_focus")
13-
local ASYNC_LOAD = hash("async_load")
14-
local UNLOAD = hash("unload")
15-
local ENABLE = hash("enable")
16-
local DISABLE = hash("disable")
6+
local WAITFOR_CONTEXT = hash("waitfor_monarch_context")
7+
local WAITFOR_PROXY_LOADED = hash("waitfor_proxy_loaded")
8+
local WAITFOR_PROXY_UNLOADED = hash("waitfor_proxy_unloaded")
9+
local WAITFOR_TRANSITION_DONE = hash("waitfor_transition_done")
10+
11+
local MSG_CONTEXT = hash("monarch_context")
12+
local MSG_PROXY_LOADED = hash("proxy_loaded")
13+
local MSG_PROXY_UNLOADED = hash("proxy_unloaded")
14+
local MSG_LAYOUT_CHANGED = hash("layout_changed")
15+
local MSG_RELEASE_INPUT_FOCUS = hash("release_input_focus")
16+
local MSG_ACQUIRE_INPUT_FOCUS = hash("acquire_input_focus")
17+
local MSG_ASYNC_LOAD = hash("async_load")
18+
local MSG_UNLOAD = hash("unload")
19+
local MSG_ENABLE = hash("enable")
20+
local MSG_DISABLE = hash("disable")
1721

1822
-- transition messages
1923
M.TRANSITION = {}
@@ -336,10 +340,10 @@ local function acquire_input(screen)
336340
log("acquire_input()", screen.id)
337341
if not screen.input then
338342
if screen.proxy then
339-
msg.post(screen.script, ACQUIRE_INPUT_FOCUS)
343+
msg.post(screen.script, MSG_ACQUIRE_INPUT_FOCUS)
340344
elseif screen.factory then
341345
for id,instance in pairs(screen.factory_ids) do
342-
msg.post(instance, ACQUIRE_INPUT_FOCUS)
346+
msg.post(instance, MSG_ACQUIRE_INPUT_FOCUS)
343347
end
344348
end
345349
screen.input = true
@@ -357,10 +361,10 @@ local function release_input(screen, next_screen)
357361
local release_focus = not keep_if_next_is_popup and not keep_when_below_next
358362
if release_focus then
359363
if screen.proxy then
360-
msg.post(screen.script, RELEASE_INPUT_FOCUS)
364+
msg.post(screen.script, MSG_RELEASE_INPUT_FOCUS)
361365
elseif screen.factory then
362366
for id,instance in pairs(screen.factory_ids) do
363-
msg.post(instance, RELEASE_INPUT_FOCUS)
367+
msg.post(instance, MSG_RELEASE_INPUT_FOCUS)
364368
end
365369
end
366370
screen.input = false
@@ -370,8 +374,8 @@ end
370374

371375
local function change_context(screen)
372376
log("change_context()", screen.id)
373-
screen.wait_for = CONTEXT
374-
msg.post(screen.script, CONTEXT, { id = screen.id })
377+
screen.wait_for = WAITFOR_CONTEXT
378+
msg.post(screen.script, MSG_CONTEXT, { id = screen.id })
375379
coroutine.yield()
376380
screen.wait_for = nil
377381
end
@@ -381,13 +385,13 @@ local function unload(screen, force)
381385
log("unload() proxy", screen.id)
382386
if screen.auto_preload and not force then
383387
if screen.loaded then
384-
msg.post(screen.proxy, DISABLE)
388+
msg.post(screen.proxy, MSG_DISABLE)
385389
screen.loaded = false
386390
end
387391
screen.preloaded = true
388392
else
389-
screen.wait_for = PROXY_UNLOADED
390-
msg.post(screen.proxy, UNLOAD)
393+
screen.wait_for = WAITFOR_PROXY_UNLOADED
394+
msg.post(screen.proxy, MSG_UNLOAD)
391395
coroutine.yield()
392396
screen.loaded = false
393397
screen.preloaded = false
@@ -435,8 +439,8 @@ local function preload(screen)
435439
screen.preloading = false
436440
return false, error_message
437441
end
438-
screen.wait_for = PROXY_LOADED
439-
msg.post(screen.proxy, ASYNC_LOAD)
442+
screen.wait_for = WAITFOR_PROXY_LOADED
443+
msg.post(screen.proxy, MSG_ASYNC_LOAD)
440444
coroutine.yield()
441445
elseif screen.factory then
442446
log("preload() factory")
@@ -476,7 +480,7 @@ local function load(screen)
476480
end
477481

478482
if screen.proxy then
479-
msg.post(screen.proxy, ENABLE)
483+
msg.post(screen.proxy, MSG_ENABLE)
480484
elseif screen.factory then
481485
screen.factory_ids = collectionfactory.create(screen.factory)
482486
screen.transition_url = screen.factory_ids[screen.transition_id]
@@ -490,7 +494,7 @@ end
490494
local function transition(screen, message_id, message, wait)
491495
log("transition()", screen.id)
492496
if screen.transition_url then
493-
screen.wait_for = M.TRANSITION.DONE
497+
screen.wait_for = WAITFOR_TRANSITION_DONE
494498
msg.post(screen.transition_url, message_id, message)
495499
if wait then
496500
coroutine.yield()
@@ -1158,28 +1162,28 @@ end
11581162

11591163

11601164
function M.on_message(message_id, message, sender)
1161-
if message_id == PROXY_LOADED then
1165+
if message_id == MSG_PROXY_LOADED then
11621166
local screen = find_screen(sender)
11631167
assert(screen, "Unable to find screen for loaded proxy")
1164-
if screen.wait_for == PROXY_LOADED then
1168+
if screen.wait_for == WAITFOR_PROXY_LOADED then
11651169
assert(coroutine.resume(screen.co))
11661170
end
1167-
elseif message_id == PROXY_UNLOADED then
1171+
elseif message_id == MSG_PROXY_UNLOADED then
11681172
local screen = find_screen(sender)
11691173
assert(screen, "Unable to find screen for unloaded proxy")
1170-
if screen.wait_for == PROXY_UNLOADED then
1174+
if screen.wait_for == WAITFOR_PROXY_UNLOADED then
11711175
assert(coroutine.resume(screen.co))
11721176
end
1173-
elseif message_id == CONTEXT then
1177+
elseif message_id == MSG_CONTEXT then
11741178
local screen = find_screen(sender)
11751179
assert(screen, "Unable to find screen for current script url")
1176-
if screen.wait_for == CONTEXT then
1180+
if screen.wait_for == WAITFOR_CONTEXT then
11771181
assert(coroutine.resume(screen.co))
11781182
end
11791183
elseif message_id == M.TRANSITION.DONE then
11801184
local screen = find_transition_screen(sender)
11811185
assert(screen, "Unable to find screen for transition")
1182-
if screen.wait_for == M.TRANSITION.DONE then
1186+
if screen.wait_for == WAITFOR_TRANSITION_DONE then
11831187
assert(coroutine.resume(screen.co))
11841188
end
11851189
elseif message_id == M.TRANSITION.SHOW_IN
@@ -1192,7 +1196,7 @@ function M.on_message(message_id, message, sender)
11921196
if screen.transition_fn then
11931197
screen.transition_fn(message_id, message, sender)
11941198
end
1195-
elseif message_id == LAYOUT_CHANGED then
1199+
elseif message_id == MSG_LAYOUT_CHANGED then
11961200
local screen = find_screen(sender)
11971201
if screen and screen.transition_fn then
11981202
screen.transition_fn(message_id, message, sender)

0 commit comments

Comments
 (0)