/** * Sleep function signature. Accepts a duration in milliseconds and returns * a promise that resolves after the delay. Injectable for tests. * * @example * ```ts * import type { SleepFunction } from '@lostgradient/weft'; * * const sleep: SleepFunction = async (milliseconds) => { * await Bun.sleep(milliseconds); * }; * void sleep; * ``` */ export type SleepFunction = (milliseconds: number) => Promise; /** * Options for a storage-backed {@link AtomicState} handle. * * @example * ```ts * import type { AtomicStateOptions } from '@lostgradient/weft'; * * const options: AtomicStateOptions = { * initial: 0, * maxRetries: 5, * }; * ``` */ export interface AtomicStateOptions { /** * Value returned by `get()` before the slot has ever been written. Once a * write or delete creates a version tombstone, an absent value reads as * `undefined`. */ initial?: T; /** Maximum number of CAS attempts before giving up. Defaults to 10. */ maxRetries?: number; /** * Sleep function used between retry attempts. Defaults to portable `sleep`. * Injection point for tests that need to observe backoff without paying * real time costs. */ sleep?: SleepFunction; } /** * Storage scope for a durable atomic state slot. * * @example * ```ts * import type { AtomicStateScope } from '@lostgradient/weft'; * * const scope: AtomicStateScope = { * type: 'workflow', * workflowType: 'invoice', * }; * ``` */ export type AtomicStateScope = { type: 'execution'; ownerWorkflowId: string; } | { type: 'workflow'; workflowType: string; }; /** * Point-in-time read result for an {@link AtomicState} slot. * * @example * ```ts * import type { AtomicStateSnapshot } from '@lostgradient/weft'; * * const snapshot: AtomicStateSnapshot = { value: 1, version: 2 }; * console.log(snapshot.version); * ``` */ export interface AtomicStateSnapshot { value: T | undefined; version: number; } /** * Result returned by an atomic state commit attempt. * * @example * ```ts * import type { AtomicStateCommitResult } from '@lostgradient/weft'; * * const result: AtomicStateCommitResult = { * applied: true, * value: 'ready', * version: 3, * }; * console.log(result.applied); * ``` */ export interface AtomicStateCommitResult extends AtomicStateSnapshot { applied: boolean; } /** * Local event emitted by an {@link AtomicState} handle. * * @example * ```ts * import { AtomicStateChangeEvent, type AtomicStateEvent } from '@lostgradient/weft'; * * const event: AtomicStateEvent = new AtomicStateChangeEvent(1, undefined, 1); * console.log(event.type); * ``` */ export type AtomicStateEvent = AtomicStateChangeEvent | AtomicStateConflictEvent | AtomicStateExhaustedEvent; /** * Observer shape accepted by the {@link AtomicState} observable projection. * * @example * ```ts * import type { AtomicStateObserver } from '@lostgradient/weft'; * * const observer: AtomicStateObserver = { * next(event) { * console.log(event.type); * }, * }; * ``` */ export type AtomicStateObserver = ((event: AtomicStateEvent) => void) | { next?: (event: AtomicStateEvent) => void; error?: (error: unknown) => void; complete?: () => void; }; /** * Subscription returned by an {@link AtomicState} observable. * * @example * ```ts * import type { AtomicStateSubscription } from '@lostgradient/weft'; * * const subscription: AtomicStateSubscription = { * unsubscribe() {}, * }; * subscription.unsubscribe(); * ``` */ export interface AtomicStateSubscription { unsubscribe(): void; } declare global { interface SymbolConstructor { readonly observable: unique symbol; } } /** * Runtime symbol used by {@link AtomicState} to expose its observable * projection. This is `Symbol.observable` when the platform provides it and * `Symbol.for('observable')` otherwise. * * @example * ```ts * import { AtomicState, OBSERVABLE_SYMBOL } from '@lostgradient/weft'; * import { MemoryStorage } from '@lostgradient/weft/storage/memory'; * * const state = new AtomicState(new MemoryStorage(), 'state:workflow-scope:default:count'); * const observable = state[OBSERVABLE_SYMBOL](); * void observable; * ``` */ export declare const OBSERVABLE_SYMBOL: typeof Symbol.observable; /** * Event emitted after an atomic state value changes. * * @example * ```ts * import { AtomicStateChangeEvent } from '@lostgradient/weft'; * * const event = new AtomicStateChangeEvent('next', 'previous', 4); * console.log(event.value, event.previousValue, event.version); * ``` */ export declare class AtomicStateChangeEvent extends Event { readonly value: T | undefined; readonly previousValue: T | undefined; readonly version: number; constructor(value: T | undefined, previousValue: T | undefined, version: number); } /** * Event emitted when a CAS attempt observes a concurrent write. * * @example * ```ts * import { AtomicStateConflictEvent } from '@lostgradient/weft'; * * const event = new AtomicStateConflictEvent('state:workflow-scope:default:count', 2); * console.log(event.stateKey, event.attempt); * ``` */ export declare class AtomicStateConflictEvent extends Event { readonly stateKey: string; readonly attempt: number; constructor(stateKey: string, attempt: number); } /** * Event emitted when the CAS retry budget is exhausted. * * @example * ```ts * import { AtomicStateExhaustedEvent } from '@lostgradient/weft'; * * const event = new AtomicStateExhaustedEvent('state:workflow-scope:default:count', 10); * console.log(event.stateKey, event.attempts); * ``` */ export declare class AtomicStateExhaustedEvent extends Event { readonly stateKey: string; readonly attempts: number; constructor(stateKey: string, attempts: number); }