/// import { State, StateConfigInterface, StateKey } from './state'; import type { Agile } from '../agile'; import { StateIngestConfigInterface } from './state.observer'; import { CreateStatePersistentConfigInterface, StatePersistent } from './state.persistent'; export declare class EnhancedState extends State { isPersisted: boolean; persistent: StatePersistent | undefined; computeValueMethod?: ComputeValueMethod; computeExistsMethod: ComputeExistsMethod; currentInterval?: NodeJS.Timer | number; /** * An enhanced State manages, like a normal State, a piece of Information * that we need to remember globally at a later point in time. * While providing a toolkit to use and mutate this piece of Information. * * The main difference to a normal State is however * that an enhanced State provides a wider variety of inbuilt utilities (like a persist, undo, watch functionality) * but requires a larger bundle size in return. * * You can create as many global enhanced States as you need. * * [Learn more..](https://agile-ts.org/docs/core/agile-instance/methods#createstate) * * @public * @param agileInstance - Instance of Agile the State belongs to. * @param initialValue - Initial value of the State. * @param config - Configuration object */ constructor(agileInstance: Agile, initialValue: ValueType, config?: StateConfigInterface); setKey(value: StateKey | undefined): this; /** * Undoes the latest State value change. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#undo) * * @public * @param config - Configuration object */ undo(config?: StateIngestConfigInterface): this; /** * Resets the State value to its initial value. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#reset) * * @public * @param config - Configuration object */ reset(config?: StateIngestConfigInterface): this; /** * Merges the specified `targetWithChanges` object into the current State value. * This merge can differ for different value combinations: * - If the current State value is an `object`, it does a partial update for the object. * - If the current State value is an `array` and the specified argument is an array too, * it concatenates the current State value with the value of the argument. * - If the current State value is neither an `object` nor an `array`, the patch can't be performed. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#patch) * * @public * @param targetWithChanges - Object to be merged into the current State value. * @param config - Configuration object */ patch(targetWithChanges: Object, config?: PatchConfigInterface): this; /** * Fires on each State value change. * * Returns the key/name identifier of the created watcher callback. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#watch) * * @public * @param callback - A function to be executed on each State value change. */ watch(callback: StateWatcherCallback): string; /** * Fires on each State value change. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#watch) * * @public * @param key - Key/Name identifier of the watcher callback. * @param callback - A function to be executed on each State value change. */ watch(key: string, callback: StateWatcherCallback): this; /** * Removes a watcher callback with the specified key/name identifier from the State. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#removewatcher) * * @public * @param key - Key/Name identifier of the watcher callback to be removed. */ removeWatcher(key: string): this; /** * Fires on the initial State value assignment and then destroys itself. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#oninaugurated) * * @public * @param callback - A function to be executed after the first State value assignment. */ onInaugurated(callback: StateWatcherCallback): this; /** * Repeatedly calls the specified callback function, * with a fixed time delay between each call. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#interval) * * @public * @param handler - A function to be executed every delay milliseconds. * @param delay - The time, in milliseconds (thousandths of a second), * the timer should delay in between executions of the specified function. */ interval(handler: (value: ValueType) => ValueType, delay?: number): this; /** * Cancels a active timed, repeating action * which was previously established by a call to `interval()`. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#clearinterval) * * @public */ clearInterval(): void; /** * Returns a boolean indicating whether the State exists. * * It calculates the value based on the `computeExistsMethod()` * and whether the State is a placeholder. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#exists) * * @public */ get exists(): boolean; /** * Defines the method used to compute the existence of the State. * * It is retrieved on each `exists()` method call * to determine whether the State exists or not. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#computeexists) * * @public * @param method - Method to compute the existence of the State. */ computeExists(method: ComputeExistsMethod): this; /** * Defines the method used to compute the value of the State. * * It is retrieved on each State value change, * in order to compute the new State value * based on the specified compute method. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#computevalue) * * @public * @param method - Method to compute the value of the State. */ computeValue(method: ComputeValueMethod): this; /** * Returns a boolean indicating whether the specified value is equal to the current State value. * * Equivalent to `===` with the difference that it looks at the value * and not on the reference in the case of objects. * * @public * @param value - Value to be compared with the current State value. */ is(value: ValueType): boolean; /** * Returns a boolean indicating whether the specified value is not equal to the current State value. * * Equivalent to `!==` with the difference that it looks at the value * and not on the reference in the case of objects. * * @public * @param value - Value to be compared with the current State value. */ isNot(value: ValueType): boolean; /** * Inverts the current State value. * * Some examples are: * - `'jeff'` -> `'ffej'` * - `true` -> `false` * - `[1, 2, 3]` -> `[3, 2, 1]` * - `10` -> `-10` * * @public */ invert(): this; /** * Preserves the State `value` in the corresponding external Storage. * * The State key/name is used as the unique identifier for the Persistent. * If that is not desired or the State has no unique identifier, * please specify a separate unique identifier for the Persistent. * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#persist) * * @public * @param config - Configuration object */ persist(config?: CreateStatePersistentConfigInterface): this; /** * Fires immediately after the persisted `value` * is loaded into the State from a corresponding external Storage. * * Registering such callback function makes only sense * when the State is [persisted](https://agile-ts.org/docs/core/state/methods/#persist). * * [Learn more..](https://agile-ts.org/docs/core/state/methods/#onload) * * @public * @param callback - A function to be executed after the externally persisted `value` was loaded into the State. */ onLoad(callback: (success: boolean) => void): this; /** * Returns the persistable value of the State. * * @internal */ getPersistableValue(): any; } export interface PatchConfigInterface extends StateIngestConfigInterface, PatchOptionConfigInterface { } export interface PatchOptionConfigInterface { /** * Whether to add new properties to the object during the merge. * @default true */ addNewProperties?: boolean; } export declare type StateWatcherCallback = (value: T, key: string) => void; export declare type ComputeValueMethod = (value: T) => T; export declare type ComputeExistsMethod = (value: T) => boolean;