import { type PatchId } from "@valbuild/core"; import type { SerializedSchema } from "@valbuild/core"; import { type Operation, type Patch, type ReadonlyJSONValue } from "@valbuild/core/patch"; /** What one `.jsonValues()` entry is at a point in a module's chain. */ export type EntryState = /** Its content, known. */ { kind: "content"; content: ReadonlyJSONValue; } /** * The content of the BASE entry `from` (its own key, or the one it was * moved or copied from), with `ops` on top: an entry nobody has loaded, * whose edits are held until somebody does ({@link resolveEntry}). */ | { kind: "waiting"; from: string; ops: HeldOps | null; } /** Removed, or moved away. */ | { kind: "deleted"; } /** An op on it could not be applied. Sticky, as a module's failure is. */ | { kind: "error"; patchId: PatchId; message: string; }; /** * The ops held for an entry, newest first: a list that is extended without * copying, so a chain of N edits to an entry nobody loaded costs N, not N². */ export type HeldOps = { readonly op: Operation; readonly patchId: PatchId; readonly before: HeldOps | null; }; /** * A module's content: its source, and what its `.jsonValues()` entries are. * * `entries` holds only the entries the chain has touched. An entry it has * not is the base's: `waiting` on its own key with nothing held, as long as * the source has its marker ({@link entryState}). So a module with no * entries, or a chain that never touches one, costs nothing here. */ export type ModuleContent = { readonly source: ReadonlyJSONValue; readonly entries: ReadonlyMap; }; /** One patch of a module's chain. */ export type PatchRecord = { readonly patchId: PatchId; readonly patch: Patch; }; export type PatchRecordResult = { status: "applied"; module: ModuleContent; } | { status: "failed"; patchId: PatchId; message: string; }; /** A module's content with nothing applied yet: its base. */ export declare function baseModuleContent(source: ReadonlyJSONValue): ModuleContent; /** * The work the engine has done in this process: patches applied * (`engine:apply-patch`) and bytes copied (`engine:clone-bytes`, see * {@link SLOT_BYTES}). A test takes it before and after; never reset, so two * readers of it cannot disturb each other. */ export declare function engineWork(): { applyPatch: number; cloneBytes: number; }; /** * Applies one patch to a module's content. Nothing is mutated: the module * passed in is still what it was, and the one returned shares every subtree * the patch did not touch. * * `failed` means the patch does not apply, and then none of it does: half a * patch is half an edit. The caller stops the module's chain there. * * `schema` is the module's serialized schema: it says which ops reach into a * `.jsonValues()` entry. Without one, every op is an op on the source. */ export declare function applyPatchRecord(module: ModuleContent, record: PatchRecord, schema: SerializedSchema | undefined): PatchRecordResult; /** * A module's chain: every patch in order, stopping at the first that does * not apply. `applied` is how many did, so the chain the content is AT is * the first `applied` ids -- what its version names. */ export declare function applyChain(module: ModuleContent, records: readonly PatchRecord[], schema: SerializedSchema | undefined): { module: ModuleContent; applied: number; failed: { patchId: PatchId; message: string; } | null; }; /** * What an entry is in this module: what the chain made it, or -- for one the * chain never touched -- the base's, if the source has its marker. */ export declare function entryState(module: ModuleContent, key: string): EntryState | undefined; /** An entry's content, read. */ export type EntryContent = { kind: "content"; content: ReadonlyJSONValue; } | { kind: "missing"; } | { kind: "error"; message: string; patchId?: PatchId; }; /** * An entry's content: what {@link entryState} says, with what it was waiting * for applied. `baseContentOf` is the BASE content of an entry by key (its * `*.val.json`, or what a save left there), `undefined` for one the base does * not have. */ export declare function resolveEntry(key: string, state: EntryState | undefined, baseContentOf: (key: string) => ReadonlyJSONValue | undefined): EntryContent;