Skip to content

Commit c0d45c3

Browse files
committed
Added keep_loaded option to preload()
Fixes #88
1 parent bcc6264 commit c0d45c3

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

README_API.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,18 @@ Go back to a previous Monarch screen. This operation will be added to the queue
4848
* `callback` (function) - Optional function to call when the previous screen is visible.
4949

5050

51-
## monarch.preload(screen_id, [callback])
51+
## monarch.preload(screen_id, [options], [callback])
5252
Preload a Monarch screen. This will load but not enable the screen. This is useful for content heavy screens that you wish to be able to show without having to wait for it load. This operation will be added to the queue if Monarch is busy.
5353

5454
**PARAMETERS**
5555
* `screen_id` (string|hash) - Id of the screen to preload.
56+
* `options` (table)
5657
* `callback` (function) - Optional function to call when the screen is preloaded.
5758

59+
The options table can contain the following fields:
60+
61+
* `keep_loaded` (boolean) - If the `keep_loaded` flag is set Monarch will keep the screen preloaded even after a `hide()` or `back()` navigation event that normally would unload the screen.
62+
5863

5964
## monarch.is_preloading(screen_id)
6065
Check if a Monarch screen is preloading (via monarch.preload() or the Preload screen setting).

monarch/monarch.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,12 +996,19 @@ end
996996
--- Preload a screen. This will load but not enable and show a screen. Useful for "heavier" screens
997997
-- that you wish to show without any delay.
998998
-- @param id (string|hash) - Id of the screen to preload
999+
-- @param options (table)
9991000
-- @param cb (function) - Optional callback to invoke when screen is loaded
1000-
function M.preload(id, cb)
1001+
function M.preload(id, options, cb)
10011002
assert(id, "You must provide a screen id")
10021003
id = tohash(id)
10031004
assert(screens[id], ("There is no screen registered with id %s"):format(tostring(id)))
10041005

1006+
-- support old function signature (id, cb)
1007+
if type(options) == "function" and not cb then
1008+
cb = options
1009+
options = nil
1010+
end
1011+
10051012
log("preload() queuing action", id)
10061013
queue_action(function(action_done, action_error)
10071014
log("preload()", id)
@@ -1012,6 +1019,10 @@ function M.preload(id, cb)
10121019
return
10131020
end
10141021

1022+
-- keep_loaded is an option for monarch.preload()
1023+
-- use it to get the same behavior as the auto preload checkbox
1024+
screen.auto_preload = screen.auto_preload or options and options.keep_loaded
1025+
10151026
if screen.preloaded or screen.loaded then
10161027
pcallfn(cb)
10171028
pcallfn(action_done)

test/test_monarch.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,21 @@ return function()
377377
end)
378378
assert(not monarch.is_preloading(TRANSITION1))
379379
end)
380-
380+
381+
it("should be able to preload a screen and keep it loaded", function()
382+
assert(not monarch.is_preloading(TRANSITION1))
383+
monarch.preload(TRANSITION1, { keep_loaded = true })
384+
wait_until_done(function(done)
385+
monarch.when_preloaded(TRANSITION1, done)
386+
end)
387+
388+
monarch.show(TRANSITION1)
389+
assert(wait_until_visible(TRANSITION1), "Transition1 was never shown")
390+
monarch.back()
391+
assert(wait_until_hidden(TRANSITION1), "Transition1 was never hidden")
392+
assert(monarch.is_preloaded(TRANSITION1))
393+
end)
394+
381395
it("should ignore any preload calls while busy", function()
382396
monarch.show(TRANSITION1)
383397
-- previously a call to preload() while also showing a screen would

0 commit comments

Comments
 (0)