Skip to main content

App lifecycle

Lifecycle hooks report when the game document is hidden or shown and when the player asks to leave through Jest's platform controls.

Register hooks after SDK initialization. Every registration returns an unsubscribe function:

await JestSDK.init();

const unsubscribeHide = JestSDK.lifecycle.onHide(() => {
game.pause();
audio.pause();
});

const unsubscribeShow = JestSDK.lifecycle.onShow(() => {
game.resume();
audio.resume();
});

const unsubscribeExit = JestSDK.lifecycle.onExitRequested(async () => {
await saveProgress();
});

function destroyGame() {
unsubscribeHide();
unsubscribeShow();
unsubscribeExit();
}

Each method supports multiple listeners and returns a function that removes only that registration. Keep subscriptions active for the game lifetime, then remove them during teardown. Calling the removal function more than once is safe.

JestSDK.lifecycle.onHide(listener)

Runs when the browser changes the game document from visible to hidden, such as when the player switches tabs, backgrounds the browser, or locks the device. It does not run for the document's initial visibility state.

Use it to pause the game loop, physics, animation, and audio.

JestSDK.lifecycle.onShow(listener)

Runs when the game document changes from hidden back to visible. It does not run on initial startup; await JestSDK.init() is the startup boundary.

Use it to resume work stopped by onHide, refresh time-sensitive state, and reconcile elapsed time.

JestSDK.lifecycle.onExitRequested(listener)

Runs when the platform begins an exit flow, including its exit control and browser Back or mobile swipe-back navigation that Jest can intercept. It runs before the exit confirmation is resolved. The player can still choose to stay, and the listener cannot cancel or delay navigation.

This event only represents an exit flow the platform can intercept. Closing the tab, terminating the browser, or an operating-system shutdown may not produce a final event. Back navigation that leaves the Jest document directly, such as returning to an external referral page, may not produce one either.

Asynchronous listeners

Listeners may return a promise. The SDK starts every listener immediately and reports synchronous errors and promise rejections without interrupting the other listeners. Awaiting inside a listener still sequences that listener's work, but it does not delay the exit confirmation or navigation. If navigation unloads the document first, the promise may not settle and code after an await may not run.

Start essential work before the first await. Treat the exit event as a last chance to save rather than the only place you save, and persist progress as the player earns it.

JestSDK.lifecycle.onExitRequested(() => {
JestSDK.data.set("checkpoint", currentCheckpoint);
});

Testing locally

Running outside Jest.com puts the SDK in mock mode. Open the JestSDK debug menu and use Request Exit under App Lifecycle to send the platform exit request to your listeners. onHide and onShow come from real browser visibility, so switch tabs or lock the device to trigger them.