Files
faunosteo/apps/app/src/core/runtime.ts
T
bchanot 6371523f8c fix(editor): honest stopped state when the draft is unsaved; no dead retry
0279953 chose the stopped message from CoreRuntime.stopped alone: it
promised the report was kept even when the tab's storage refused the
draft (export, the banner's advice, needs the core), four surfaces still
offered a retry that could not work, and a failed recovery below the cap
was a dead end since nothing called retry().

The runtime restarts a failed recovery on the next call (compiled module
reused, cap still counts traps) and notifies status listeners. One
CoreStoppedNotice on the editor, Home, New report and the full-page
notice: reload when saved, else core.stopped_unsaved with a core-free
copy download (also on the storage-full banner) and a beforeunload
guard. Export, import and new report say the stopped message; a core
failure reading an unread draft is FAILED, not held. ESLint census of
the failure keys; core.stopped says "plusieurs erreurs".
2026-10-16 18:26:10 +02:00

63 lines
2.2 KiB
TypeScript

import { useSyncExternalStore } from 'react'
import glueUrl from '@faunosteo/core-wasm/pkg/faunosteo_wasm.js?url'
import wasmUrl from '@faunosteo/core-wasm/pkg/faunosteo_wasm_bg.wasm?url'
import {
CoreRuntime,
type CoreSource,
type GlueModule,
} from '@faunosteo/core-wasm'
/**
* The application's core runtime (Rust compiled to WebAssembly, R2). Vite emits the glue
* and the `.wasm` as hashed assets that no route chunk imports: nothing is fetched until a
* sheet path or an import asks `ready()` (review III.3 rule 6). The production CSP must
* carry `'wasm-unsafe-eval'` (DEPLOY.md); without it the load fails and the editor shows
* the degraded state.
*/
async function compileModule(): Promise<WebAssembly.Module> {
const response = await fetch(wasmUrl)
if (!response.ok) throw new Error(`core module HTTP ${response.status}`)
try {
// Needs `Content-Type: application/wasm`; any other type falls back to bytes.
return await WebAssembly.compileStreaming(response.clone())
} catch {
return WebAssembly.compile(await response.arrayBuffer())
}
}
const browserSource: CoreSource = {
compile: compileModule,
// A distinct query per instance gives a new glue module record (R0 finding).
importGlue: (instance) =>
import(/* @vite-ignore */ `${glueUrl}?instance=${instance}`) as Promise<GlueModule>,
}
let runtime: CoreRuntime | null = null
/** Whether the page's runtime spent its recoveries (never creates the runtime). */
export function coreStopped(): boolean {
return runtime?.stopped ?? false
}
/** Subscribes to the runtime's status changes (stable identity for `useSyncExternalStore`). */
export function subscribeCore(listener: () => void): () => void {
return coreRuntime().subscribe(listener)
}
/** Whether the engine stopped for good on this page; re-renders when it does. */
export function useCoreStopped(): boolean {
return useSyncExternalStore(subscribeCore, coreStopped)
}
/** The page's runtime, created on first use. */
export function coreRuntime(): CoreRuntime {
runtime ??= new CoreRuntime(browserSource)
return runtime
}
/** Test seam: run the application on another runtime (the Node source in vitest). */
export function setCoreRuntime(next: CoreRuntime): void {
runtime = next
}