/** * Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal * @author Adam Midlik */ import { StateTree } from '../tree/immutable.js'; import { TransientTree } from '../tree/transient.js'; import { StateObject, StateObjectCell, StateObjectSelector, StateObjectRef } from '../object.js'; import { StateTransform } from '../transform.js'; import { StateTransformer } from '../transformer.js'; import { State } from '../state.js'; export { StateBuilder }; interface StateBuilder { readonly editInfo: StateBuilder.EditInfo; getTree(): StateTree; } declare namespace StateBuilder { export interface EditInfo { applied: boolean; sourceTree: StateTree; count: number; lastUpdate?: StateTransform.Ref; } interface BuildState { state: State | undefined; tree: TransientTree; editInfo: EditInfo; actions: Action[]; } type Action = { kind: 'add'; transform: StateTransform; } | { kind: 'update'; ref: string; params: any; } | { kind: 'delete'; ref: string; } | { kind: 'insert'; ref: string; transform: StateTransform; }; export function is(obj: any): obj is StateBuilder; export function isTo(obj: any): obj is StateBuilder.To; export class Root implements StateBuilder { private state; get editInfo(): EditInfo; get currentTree(): TransientTree; to(ref: StateTransform.Ref): To; to(ref: StateObjectRef): To; to(cell: C): To, StateObjectCell.Transformer>; to(selector: S): To, StateObjectSelector.Transformer>; toRoot(): To>, StateObject>, any>>; delete(obj: StateObjectRef): this; getTree(options?: { useHashVersion?: boolean; }): StateTree; commit(options?: Partial): Promise; constructor(tree: StateTree, state?: State); } export class To implements StateBuilder { private state; private root; get editInfo(): EditInfo; get selector(): StateObjectSelector; readonly ref: StateTransform.Ref; private getApplyRoot; /** * Apply the transformed to the parent node * If no params are specified (params <- undefined), default params are lazily resolved. */ apply>(tr: T, params?: Partial>, options?: Partial): To, T>; /** * If the ref is present, the transform is applied. * Otherwise a transform with the specifed ref is created. */ applyOrUpdate>(ref: StateTransform.Ref, tr: T, params?: Partial>, options?: Partial): To, T>; /** * Apply the transformed to the parent node * If no params are specified (params <- undefined), default params are lazily resolved. * The transformer cannot be a decorator to be able to use this. */ applyOrUpdateTagged>(tags: string | string[], tr: T, params?: Partial>, options?: Partial): To, T>; /** * A helper to greate a group-like state object and keep the current type. */ group>(tr: T, params?: StateTransformer.Params, options?: Partial): To; /** * Inserts a new transform that does not change the object type and move the original children to it. */ insert>(tr: T, params?: Partial>, options?: Partial): To, T>; private updateTagged; update>(transformer: T, params: (old: StateTransformer.Params) => Partial> | void): Root; update(params: Partial> | ((old: StateTransformer.Params) => Partial> | void)): Root; updateState(state: Partial): void; /** Add tags to the current node */ tag(tags: string | string[]): this; /** Add dependsOn to the current node */ dependsOn(dependsOn: string | string[]): void; to(ref: StateTransform.Ref): To; to(cell: C): To, StateObjectCell.Transformer>; to(selector: S): To, StateObjectSelector.Transformer>; toRoot(): To>, StateObject>, any>>; delete(ref: StateObjectRef): Root; getTree(options?: { useHashVersion?: boolean; }): StateTree; /** Returns selector to this node. */ commit(options?: Partial): Promise>; constructor(state: BuildState, ref: StateTransform.Ref, root: Root); } export {}; }