Files
faunosteo/apps/app/src/catalogue/preview.ts
T
bchanot 0176b3184d feat(app): administration screens and anatomical catalogue (V1-2.5 G, H, I)
Administration page (board 10), species, rights, official sheets list with filters, creation from the core's empty document, overview with the working-copy action; catalogue list (search by sheet name), sheet and version pages through PreviewSheet with zoom and pan. App 650, typecheck, lint, build green.
2026-11-02 17:18:27 +01:00

100 lines
3.3 KiB
TypeScript

import {
CORE_UNAVAILABLE,
closedRefusal,
parseOutcome,
type CoreOutcome,
type CoreRuntime,
type GluePreviewSheet,
} from '@faunosteo/core-wasm'
import type {
CanvasView,
PreviewInput,
PreviewState,
} from '@faunosteo/core-wasm/types'
/** One stored drawing as the page downloaded it: untrusted bytes the core hashes and reads. */
export interface PreviewDrawing {
assetId: string
sha256: string
bytes: Uint8Array
}
/**
* A stored version loaded into the core (`PreviewSheet`, RUST.md §12): the
* core checks each drawing's sha256 against its row, the fingerprint, the caps
* and every drawing through its SVG reader, then answers `preview.state` and
* `view.canvas`. Like `SheetSession`, the handle belongs to one instance of
* the module: after a trap and a recovery it is rebuilt from the kept input,
* a handle of a dropped instance is never called. `free` releases it when the
* page leaves.
*/
export class CataloguePreview {
private handle: GluePreviewSheet | null = null
private handleGeneration = -1
private freed = false
constructor(
private readonly runtime: CoreRuntime,
private readonly input: PreviewInput,
private readonly drawings: readonly PreviewDrawing[],
) {}
/** The first call builds the handle: the builder's refusals come back as closed kinds. */
state(): CoreOutcome<PreviewState> {
return this.call<PreviewState>('preview.state', {})
}
/** The composed view (drawing framed, annotation layer, ids scoped per view). */
canvas(viewId: string): CoreOutcome<string> {
const view = this.call<CanvasView>('view.canvas', { viewId })
return view.ok ? { ok: true, value: view.value.svg } : view
}
/** After this, every call answers `core_unavailable`: a page that left never rebuilds. */
free(): void {
this.freed = true
const handle = this.handle
this.handle = null
if (!handle || this.handleGeneration !== this.runtime.generation) return
try {
handle.free()
} catch {
// Freeing never decides anything; a dead instance keeps its memory.
}
}
private call<T>(op: string, input: unknown): CoreOutcome<T> {
const handle = this.current()
if (!handle.ok) return handle
return this.runtime.invoke(() =>
parseOutcome<T>(handle.value.call(op, JSON.stringify(input))),
)
}
private current(): CoreOutcome<GluePreviewSheet> {
if (this.freed) return CORE_UNAVAILABLE
if (this.handle && this.handleGeneration === this.runtime.generation) {
return { ok: true, value: this.handle }
}
this.handle = null
return this.runtime.invoke<GluePreviewSheet>((glue) => {
const builder = new glue.PreviewBuilder()
for (const drawing of this.drawings) {
// `false` past the handle's bounds: the build then refuses with `caps`.
builder.add_drawing(drawing.assetId, drawing.sha256, drawing.bytes)
}
try {
this.handle = builder.build(JSON.stringify(this.input))
} catch (error) {
// Only the module's closed refusal is a refused version; anything else poisons.
const refusal = closedRefusal(error)
if (refusal === null) throw error
const [kind, ...detail] = refusal.split(': ')
return { ok: false, kind, detail: detail.join(': ') }
}
this.handleGeneration = this.runtime.generation
return { ok: true, value: this.handle }
})
}
}