import type { Storage } from '../storage/interface.ts'; import { OBSERVABLE_SYMBOL, type AtomicStateCommitResult, type AtomicStateEvent, type AtomicStateObserver, type AtomicStateOptions, type AtomicStateScope, type AtomicStateSnapshot, type AtomicStateSubscription } from './atomic-state-events.ts'; import { WeftError } from './weft-error.ts'; export { AtomicStateChangeEvent, AtomicStateConflictEvent, AtomicStateExhaustedEvent, OBSERVABLE_SYMBOL, } from './atomic-state-events.ts'; export type { AtomicStateCommitResult, AtomicStateEvent, AtomicStateObserver, AtomicStateOptions, AtomicStateScope, AtomicStateSnapshot, AtomicStateSubscription, SleepFunction, } from './atomic-state-events.ts'; /** * Thrown by {@link AtomicState.update} when the CAS (compare-and-swap) loop * exhausts its retry budget without successfully committing. * * @example * ```ts * import { AtomicState, AtomicStateConflictError } from '@lostgradient/weft'; * import { MemoryStorage } from '@lostgradient/weft/storage/memory'; * * const storage = new MemoryStorage(); * const state = new AtomicState(storage, 'state:workflow-scope:default:counter', { * initial: 0, * maxRetries: 3, * }); * try { * await state.increment(); * } catch (error) { * if (error instanceof AtomicStateConflictError) { * console.error('conflict on', error.stateKey, 'after', error.attempts, 'attempts'); * } * } * ``` */ export declare class AtomicStateConflictError extends WeftError<'AtomicStateConflictError'> { readonly stateKey: string; readonly attempts: number; constructor(stateKey: string, attempts: number); } export declare function atomicStateDataKey(scope: AtomicStateScope, key: string): string; export declare function atomicStateVersionKey(dataKey: string): string; export declare function readAtomicStateSnapshot(storage: Storage, dataKey: string, options?: Pick, 'initial'>): Promise>; export declare function commitAtomicStateValue(storage: Storage, dataKey: string, expectedVersion: number, value: T): Promise>; export declare function commitAtomicStateDelete(storage: Storage, dataKey: string, expectedVersion: number): Promise>; /** * Storage-backed compare-and-swap state slot. `AtomicState` is scoped by the * storage key supplied to the constructor; use `engine.state.*` and * `ctx.state.*` for the built-in execution and workflow scopes. * * @example * ```ts * import { AtomicState } from '@lostgradient/weft'; * import { MemoryStorage } from '@lostgradient/weft/storage/memory'; * * const storage = new MemoryStorage(); * const counter = new AtomicState(storage, 'state:workflow-scope:default:count', { initial: 0 }); * await counter.increment(); * console.log(await counter.get()); // 1 * ``` */ export declare class AtomicState extends EventTarget { #private; constructor(storage: Storage, dataKey: string, options?: AtomicStateOptions); /** Read the current value. */ get(): Promise; /** Update the state with optimistic concurrency and automatic retry. */ update(updater: (current: T | undefined) => T): Promise; /** Replace the state value. */ set(value: T): Promise; /** * Delete the state value while still advancing the version tombstone so * concurrent writers cannot silently overwrite a delete. */ delete(): Promise; increment(this: AtomicState, amount?: number): Promise; decrement(this: AtomicState, amount?: number): Promise; merge>(this: AtomicState, patch: Partial): Promise; append(this: AtomicState, item: TItem): Promise; removeFirst(this: AtomicState): Promise; removeLast(this: AtomicState): Promise; [OBSERVABLE_SYMBOL](): { subscribe: (observer: AtomicStateObserver) => AtomicStateSubscription; }; [Symbol.asyncIterator](): AsyncIterator>; }