/** * Host lifecycle: when Corestore / Hyperswarm expose `suspend`/`resume`, wire them into * `ctx.bareOsInvokeSuspendHooks` / `ctx.bareOsInvokeResumeHooks` (mobile sleep, etc.). */ /** @type {{ store: unknown, swarm: unknown } | null} */ let pendingPair = null /** * Called once from main() with live instances; hooks install on first guest ctx. * @param {unknown} store * @param {unknown} swarm */ export function bareOsRegisterCorestoreSuspendResumeHooks(store, swarm) { pendingPair = { store, swarm } } /** * @param {unknown} target * @param {string} label */ async function callSuspendResume(target, label) { if (!target || typeof target !== 'object') return const o = /** @type {{ suspend?: () => unknown, resume?: () => unknown }} */ ( target ) const fn = label === 'suspend' ? o.suspend : o.resume if (typeof fn !== 'function') return await Promise.resolve(fn.call(target)) } /** * @param {{ bareOsRegisterSuspendHook: (fn: () => void | Promise) => unknown, bareOsRegisterResumeHook: (fn: () => void | Promise) => unknown }} ctx */ export function bareOsInstallCorestoreSuspendResumeHooks(ctx) { if (!pendingPair) return const { store, swarm } = pendingPair ctx.bareOsRegisterSuspendHook(async () => { await callSuspendResume(store, 'suspend') await callSuspendResume(swarm, 'suspend') }) ctx.bareOsRegisterResumeHook(async () => { await callSuspendResume(swarm, 'resume') await callSuspendResume(store, 'resume') }) }