type Value = ReturnType; /** * Callback used by {@link Events} and {@link EventHandle} functions. Note the callback is limited to 8 arguments. * * @param arg1 - First argument that is passed from caller. * @param arg2 - Second argument that is passed from caller. * @param arg3 - Third argument that is passed from caller. * @param arg4 - Fourth argument that is passed from caller. * @param arg5 - Fifth argument that is passed from caller. * @param arg6 - Sixth argument that is passed from caller. * @param arg7 - Seventh argument that is passed from caller. * @param arg8 - Eighth argument that is passed from caller. */ type HandleEvent = (arg1?: Value, arg2?: Value, arg3?: Value, arg4?: Value, arg5?: Value, arg6?: Value, arg7?: Value, arg8?: Value) => void; /** * Base class for event handling, providing mechanisms to register, emit, and unbind events. This * class supports adding event listeners, emitting events with up to 8 arguments, and managing * multiple emitters. * * @example * // Create an instance of the Events class * const events = new Events(); * * // Register an event listener * events.on('testEvent', (arg1, arg2) => { * console.log('Event triggered with arguments:', arg1, arg2); * }); * * // Emit the event * events.emit('testEvent', 'value1', 'value2'); * * // Unbind the event listener * events.unbind('testEvent'); */ declare class Events { private _additionalEmitters; private _events; private _suspendEvents; /** * Creates a new Events instance. */ constructor(); /** * Sets whether events are suspended. If true, the observer will not emit events when values * are set. */ set suspendEvents(value: boolean); /** * Gets whether events are suspended. */ get suspendEvents(): boolean; /** * Registers an event listener for the specified event name. If the event is emitted, * the callback function is executed with up to 8 arguments. * * @param name - The name of the event to listen for. * @param fn - The callback function to be executed when the event is emitted. * @returns An EventHandle object that can be used to unbind the event listener. * * @example * // Register an event listener * events.on('testEvent', (arg1, arg2) => { * console.log('Event triggered with arguments:', arg1, arg2); * }); * * // Emit the event * events.emit('testEvent', 'value1', 'value2'); */ on(name: string, fn: HandleEvent): EventHandle; /** * Registers a one-time event listener for the specified event name. The callback function is * executed the next time the event is emitted, and then automatically unbound. * * @param name - The name of the event to listen for. * @param fn - The callback function to be executed once when the event is emitted. * @returns An EventHandle object that can be used to unbind the event listener * before it is triggered. * * @example * // Register a one-time event listener * events.once('testEvent', (arg1, arg2) => { * console.log('Event triggered once with arguments:', arg1, arg2); * }); * * // Emit the event * events.emit('testEvent', 'value1', 'value2'); // The callback will be called and then unbound. * * // Emit the event again * events.emit('testEvent', 'value1', 'value2'); // The callback will not be called this time. */ once(name: string, fn: HandleEvent): EventHandle; /** * Emits the specified event, executing all registered listeners for that event with the * provided arguments. If events are suspended, the emit operation will be ignored. * * @param name - The name of the event to emit. * @param arg0 - The first argument to pass to the event listeners. * @param arg1 - The second argument to pass to the event listeners. * @param arg2 - The third argument to pass to the event listeners. * @param arg3 - The fourth argument to pass to the event listeners. * @param arg4 - The fifth argument to pass to the event listeners. * @param arg5 - The sixth argument to pass to the event listeners. * @param arg6 - The seventh argument to pass to the event listeners. * @param arg7 - The eighth argument to pass to the event listeners. * @returns The current instance for chaining. * * @example * // Register an event listener * events.on('testEvent', (arg1, arg2) => { * console.log('Event triggered with arguments:', arg1, arg2); * }); * * // Emit the event * events.emit('testEvent', 'value1', 'value2'); * * // Emit the event with more arguments * events.emit('testEvent', 'value1', 'value2', 'value3', 'value4'); */ emit(name: string, arg0?: Value, arg1?: Value, arg2?: Value, arg3?: Value, arg4?: Value, arg5?: Value, arg6?: Value, arg7?: Value): this; /** * Unbinds an event listener for the specified event name. If a callback function is provided, * only that specific listener is removed. If no callback is provided, all listeners for the * event are removed. If no event name is provided, all listeners for all events are removed. * * @param name - The name of the event to unbind. If not provided, all events are * unbound. * @param fn - The specific callback function to remove. If not provided, all * listeners for the event are removed. * @returns The current instance for chaining. * * @example * // Register an event listener * const callback = (arg1, arg2) => { * console.log('Event triggered with arguments:', arg1, arg2); * }; * events.on('testEvent', callback); * * // Unbind the specific event listener * events.unbind('testEvent', callback); * * // Unbind all listeners for a specific event * events.unbind('testEvent'); * * // Unbind all listeners for all events * events.unbind(); */ unbind(name?: string, fn?: HandleEvent): this; /** * Adds another emitter. Any events fired by this instance will also be fired on the additional * emitter. * * @param emitter - The emitter */ addEmitter(emitter: Events): void; /** * Removes emitter. * * @param emitter - The emitter */ removeEmitter(emitter: Events): void; } /** * EventHandle manages the binding and unbinding of event listeners. It provides a convenient way * to add, remove, and invoke event handlers associated with specific event names. Each EventHandle * is linked to an 'owner' object, typically an instance of the Events class, allowing for elegant * event management and chaining. */ declare class EventHandle { private owner; private name; private fn; /** * Creates an instance of EventHandle. * * @param owner - Owner * @param name - Name * @param fn - Callback function */ constructor(owner: Events, name: string, fn: HandleEvent); /** * Unbinds the event handle from the owner, effectively removing the event listener. After * calling this method, the event handle will no longer trigger the callback function when the * event is emitted. */ unbind(): void; /** * Invokes the callback function associated with the event handle. This method directly * triggers the event's callback without the event being emitted by the event system. */ call(events: Events, ...args: Value[]): void; /** * Registers a new event listener on the same owner as the EventHandle. This method allows * chaining additional event listeners to the owner of this event handle. * * @param name - Name * @param fn - Callback function * @returns EventHandle */ on(name: string, fn: HandleEvent): EventHandle; } /** * Represents an action in the history. */ type HistoryAction = { /** * The action name. */ name: string; /** * The undo function. */ undo: () => void; /** * The redo function. */ redo: () => void; /** * Whether to combine with the previous action with the same name. * The effect of combining is merely changing the redo function to be the redo function of this action. * The original undo function is not modified. */ combine: boolean; }; /** * Manages history actions for undo/redo operations. This class keeps track of actions that can be * undone and redone, allowing for complex state management in applications such as editors, games, * or any interactive applications where state changes need to be reversible. * * @example * const history = new History(); * * // Define an action * const action = { * name: 'draw', * undo: () => { console.log('Undo draw'); }, * redo: () => { console.log('Redo draw'); } * }; * * // Add the action to history * history.add(action); * * // Perform undo * history.undo(); * * // Perform redo * history.redo(); */ declare class History$1 extends Events { private _executing; private _actions; private _currentActionIndex; private _canUndo; private _canRedo; /** * Adds a new history action to the stack. If the action has a combine flag and matches the * current action's name, the redo function of the current action is updated. If actions have * been undone before adding this new action, it removes all actions that come after the * current action to maintain a consistent history. * * @param action - The action to add. * @returns Returns `true` if the action is successfully added, `false` otherwise. */ add(action: HistoryAction): boolean; /** * Adds a new history action and immediately executes its redo function. * * @param action - The action. * @returns A promise that resolves once the redo function has been executed. */ addAndExecute(action: HistoryAction): Promise; /** * Undoes the last history action. This method retrieves the current action from the history * stack and executes the action's undo function. * * @returns A promise that resolves once the undo function has been executed. */ undo(): Promise; /** * Redoes the next history action. This retrieves the next action from the history stack and * executes the action's redo function. * * @returns A promise that resolves once the redo function has been executed. */ redo(): Promise; /** * Clears all history actions. */ clear(): void; /** * The current history action. */ get currentAction(): HistoryAction; /** * The last action committed to the history. */ get lastAction(): HistoryAction; /** * Sets whether we can undo at this time. */ set canUndo(value: boolean); /** * Gets whether we can undo at this time. */ get canUndo(): boolean; /** * Sets whether we can redo at this time. */ set canRedo(value: boolean); /** * Gets whether we can redo at this time. */ get canRedo(): boolean; /** * Sets the number of async actions currently executing. */ set executing(value: number); /** * Gets the number of async actions currently executing. */ get executing(): number; } type History = { add: (action: HistoryAction) => void; }; /** * The ObserverHistory module provides a mechanism for tracking changes to an Observer object and * storing them in a history stack. */ declare class ObserverHistory extends Events { item: Observer; private _history; private _enabled; private _prefix; private _combine; private _selfEvents; /** * @param args - Arguments */ constructor(args?: { item?: Observer; history?: History; enabled?: boolean; prefix?: string; combine?: boolean; }); private _initialize; private _apply; destroy(): void; set enabled(value: boolean); get enabled(): boolean; set prefix(value: string); get prefix(): string; set combine(value: boolean); get combine(): boolean; } /** * The ObserverSync class is used to construct an interface for synchronizing changes from Observer * to other services. */ type ObserverSync = Events & { write: (...args: Value[]) => void; enabled: boolean; }; type Key = keyof T & string; type LatestFn = () => Observer; type ObserverOptions = { parent?: Observer; parentPath?: string; parentField?: Value; parentKey?: Value; latestFn?: LatestFn; pathsWithDuplicates?: string[]; }; type Schema = { has: (path: string) => boolean; get: (path: string) => { type: { name: string; }; }; }; type EachFn = (path: string, type: string, value: Value, key: string) => void; /** * The Observer class is used to observe and manage changes to an object. It allows for tracking * modifications to nested properties, emitting events on changes, and maintaining state * consistency. This is particularly useful in applications where state management and change * tracking are critical, such as in data-driven interfaces or collaborative applications. * * @example * const data = { * name: 'John', * age: 30, * address: { * city: 'New York', * zip: '10001' * } * }; * * const observer = new Observer(data); * * observer.on('name:set', (newValue, oldValue) => { * console.log(`Name changed from ${oldValue} to ${newValue}`); * }); * * observer.set('name', 'Jane'); // Logs: Name changed from John to Jane */ declare class Observer extends Events { private _destroyed; private _path; private _keys; private _data; private _pathsWithDuplicates; private _parent; private _parentPath; private _parentField; private _parentKey; private _latestFn; private _silent; history: ObserverHistory; sync: ObserverSync; schema: Schema; /** * Creates a new Observer instance. * * @param data - The initial data to observe. * @param options - Additional options for the observer. */ constructor(data?: Partial, options?: ObserverOptions); private static _splitPathsCache; private static _splitPath; silence(): [boolean, boolean]; silenceRestore(state: [boolean, boolean]): void; private _prepare; /** * @param path - Path to the property in the object. * @param value - Value to set. * @param silent - If true, the change will not be recorded in history. * @param remote - State value passed to the set event used to disable remote event emission. * @param force - If true, the value will be set even if it is the same as the current value. * @returns Returns true if the value was successfully set and false otherwise. */ set

(path: P, value: P extends Key ? T[P] : Value, silent?: boolean, remote?: boolean, force?: boolean): boolean; /** * Query whether the object has the specified property. * * @param path - Path to the value. * @returns Returns true if the value is present and false otherwise. */ has(path: string): boolean; /** * @param path - Path to the value. * @param raw - Retrieve the observer object without converting it to JSON. * @returns The value at the specified path. */ get

(path: P, raw: true): Value; get

(path: P, raw?: false): P extends Key ? T[P] : Value; getRaw(path: string): any; private _equals; /** * @param path - Path to the value. * @param silent - If true, the change will not be recorded in history. * @param remote - State value passed to the set event used to disable remote event emission. * @returns Returns true if the value was successfully unset and false otherwise. */ unset(path: string, silent?: boolean, remote?: boolean): boolean; /** * @param path - Path to the value. * @param ind - Index of the value. * @param silent - If true, the remove event will not be emitted. * @param remote - State value passed to the set event used to disable remote event emission. * @returns Returns true if the value was successfully removed and false otherwise. */ remove(path: string, ind: number, silent?: boolean, remote?: boolean): boolean; /** * @param path - Path to the value. * @param value - Value to remove. * @param silent - If true, the remove event will not be emitted. * @param remote - State value passed to the set event used to disable remote event emission. * @returns Returns true if the value was successfully removed and false otherwise. */ removeValue(path: string, value: Value, silent?: boolean, remote?: boolean): boolean; /** * @param path - Path to the value. * @param value - Value to insert. * @param ind - Index to insert the value at. * @param silent - If true, the insert event will not be emitted. * @param remote - State value passed to the set event used to disable remote event emission. * @returns Returns true if the value was successfully inserted and false otherwise. */ insert(path: string, value: Value, ind?: number, silent?: boolean, remote?: boolean): boolean; private _doInsert; /** * @param path - Path to the value. * @param indOld - Index of the value to move. * @param indNew - Index to move the value to. * @param silent - If true, the move event will not be emitted. * @param remote - State value passed to the set event used to disable remote event emission. * @returns Returns true if the value was successfully moved and false otherwise. */ move(path: string, indOld: number, indNew: number, silent?: boolean, remote?: boolean): boolean; patch(data: Partial, removeMissingKeys?: boolean): void; /** * @returns The current state of the object tracked by the observer. */ json(): T; /** * @param target - The object to JSONify. * @returns A plain copy of the target, with nested observers converted to JSON. */ json(target: Value): Value; forEach(fn: EachFn, target?: Value, path?: string): void; /** * Returns the latest observer instance. This is important when * dealing with undo / redo where the observer might have been deleted * and/or possibly re-created. * * @returns The latest instance of the observer. */ latest(): Observer; /** * Destroys the observer instance. */ destroy(): void; set latestFn(value: LatestFn); get latestFn(): LatestFn; } /** * The ObserverList class is a list of Observer objects. */ declare class ObserverList extends Events { data: T[]; private _indexed; sorted: ((arg0: T, arg1: T) => number) | null; index: string | null; /** * @param options.sorted - Sorted * @param options.index - Index */ constructor(options?: { sorted?: (arg0: T, arg1: T) => number; index?: string; }); get length(): number; private _itemKey; get(index: string | number): T; set(index: string | number, value: T): void; indexOf(item: T): string | number; position(b: T, fn: (arg0: T, arg1: T) => number): number; positionNextClosest(b: T, fn: ((arg0: T, arg1: T) => number)): number; has(item: T): boolean; add(item: T): number; move(item: T, pos: number): void; remove(item: T): void; removeByKey(index: string | number): void; removeBy(fn: (arg0: T) => unknown): void; clear(): void; forEach(fn: (arg0: T, arg1: Value) => void): void; find(fn: (arg0: T) => unknown): (number | T)[][]; findOne(fn: (arg0: T) => unknown): (number | T)[]; map(fn: (value: T, index: number, array: T[]) => U): U[]; sort(fn: (a: T, b: T) => number): void; array(): T[]; json(): any[]; } export { EventHandle, Events, History$1 as History, Observer, ObserverHistory, ObserverList }; export type { HandleEvent, HistoryAction, ObserverSync };