/** * Suspend/resume hook registry and kernel reload request methods on `ctx`. */ /** * @param {{ * suspendHooks: Array<() => void | Promise>, * resumeHooks: Array<() => void | Promise>, * env: Record * }} deps */ export function createBareOsLifecycleHookMethods(deps) { const { suspendHooks, resumeHooks, env } = deps return { /** @param {() => void | Promise} fn */ bareOsRegisterSuspendHook(fn) { if (typeof fn === 'function') suspendHooks.push(fn) return () => { const i = suspendHooks.indexOf(fn) if (i >= 0) suspendHooks.splice(i, 1) } }, /** @param {() => void | Promise} fn */ bareOsRegisterResumeHook(fn) { if (typeof fn === 'function') resumeHooks.push(fn) return () => { const i = resumeHooks.indexOf(fn) if (i >= 0) resumeHooks.splice(i, 1) } }, async bareOsInvokeSuspendHooks() { for (const fn of [...suspendHooks]) { try { await fn() } catch { /* ignore */ } } }, async bareOsInvokeResumeHooks() { for (const fn of [...resumeHooks]) { try { await fn() } catch { /* ignore */ } } }, bareOsRequestKernelReload() { const err = /** @type {Error & { code?: string }} */ ( new Error('BARE_OS_KERNEL_RELOAD') ) err.code = 'BARE_OS_KERNEL_RELOAD' throw err }, bareOsRequestKernelProfileReload() { const on = env.BARE_OS_KERNEL_PROFILE_WARM === '1' || env.BARE_OS_KERNEL_PROFILE_WARM === 'true' if (!on) { throw new Error( 'bareOsRequestKernelProfileReload: enable BARE_OS_KERNEL_PROFILE_WARM=1' ) } const err = /** @type {Error & { code?: string }} */ ( new Error('BARE_OS_KERNEL_PROFILE_RELOAD') ) err.code = 'BARE_OS_KERNEL_PROFILE_RELOAD' throw err } } }