The Swift bridge

swiftBridge() is the JS half of the Swift WKWebView shell (shells/swift). The shell registers one script message handler, tosijsNative, with a reply, so every call is webkit.messageHandlers.tosijsNative.postMessage({ method, args }), whose promise resolves with the result or rejects with the shell's error message. Games don't call it directly: nativeBridge() (src/shells.tjs) picks it inside a Swift shell.

Pause and resume come from visibilitychange, and from the shell too, which also knows about the app switcher, the lock screen, quitting, sleep and wake. The shell sends every event as a CustomEvent on window named tosijs-native: plus the event (tosijs-native:owned, tosijs-native:resume), with the payload as its detail.

The pause hold

A pause is different, because the system is about to stop the page. WebKit keeps a page running only while it owes the app an answer, and an app-side background task does not extend that (measured: the page is frozen about 1.2 s after the app is backgrounded, whatever the app holds). So the shell asks the page to pause and waits for the answer: it calls globalThis.__tosijsNative.pause() with callAsyncJavaScript, and the promise resolves when everything listening for the pause has finished (the API runs the game's pause listeners and writes its saves), or after ten seconds. That one pending call is what lets "save when paused" finish.