/** * Persistence (Extension Spec §5.2 step 5). A registered code function lives in * the **one workbook registry** (origin `code-mode`), carrying its source + * descriptor + version hash as the registry's per-function source record. There * is no sidecar: `workbook.removeFunction` drops the record too, so the * export/rehydrate resurrection bug cannot happen. Editing a function creates a * new version hash, and `upsertFunction` re-evaluates every call site. */ import { compileCodeFunction } from './compile' import type { CodeFunctionSpec, CompiledCodeFunction } from './compile' import type { Workbook } from '@tanstack/spreadsheet-core' export interface CodeFunctionRecord extends CodeFunctionSpec { version: string } /** * Register a compiled function into the workbook registry with origin * `code-mode`, attaching its serializable record so it survives export. */ export function registerCompiled( wb: Workbook, compiled: CompiledCodeFunction, ): void { const record: CodeFunctionRecord = { ...compiled.spec, version: compiled.version, } wb.upsertFunction(compiled.def, { origin: 'code-mode', source: record }) } /** * The code functions registered on `wb` — read straight from the registry, so a * removed function is gone here too. Persist these with the workbook. */ export function exportCodeFunctions(wb: Workbook): Array { return wb.functionSources('code-mode') as Array } /** Recompile + register persisted code functions into a (re)loaded workbook. */ export function rehydrateCodeFunctions( wb: Workbook, records: Array, ): void { for (const rec of records) registerCompiled(wb, compileCodeFunction(rec)) }