UNPKG

@real-estate/core

Version:

A simple library to maintain state in JavaScript

49 lines (48 loc) 2.15 kB
declare class RState<T> { private stateContainer; private watcherIdIncrement; private privateId; /** * Creates a new State that can be watched * @param initialValue Sets the initial value of the state * @param description An optional description of the state and what is does in the app */ constructor(initialValue: T); /** * Gets you the current state in correct data type * @returns The most updated state of type specified when state is created */ get: () => T; /** * Each State instance has it's own unique id * @returns Unique Id for each state object in the system */ getStateId(): number; /** * Sets the new state. This updates the state and calls all the watchers. * @param value sets the value of the state. This is of type specified on creation */ set: (value: T) => void; /** * Sets new functions to be callbacks for when the state changes. It watches the state * @param callback Sets the callback function to be called when states changes. The callback function is also called immediately, to ensure updated UI * @param initCall Calls the function on creation in watch if true. Default false * @returns The watcher id */ watch: (callback: (state?: T | undefined) => void, initCall?: boolean) => number; /** * Delete a watcher with the id from THIS CURRENT STATE! NOT ANY OTHER STATE! * @param watcherId Watcher Id you can get when calling the watcher. Remember that watcher ids are only valid within the same state object */ deleteWatcher: (watcherId: number) => void; private getNewWatcherId; } /** * Watches multiple states for changes * @param stateArr An array of all state objects to watch * @param callback Function to call if any of the states are changed * @param initCall Calls the function only once, on creation in watch if true. Default false */ declare function MultiWatcher(stateArr: Array<RState<any>>, callback: () => void, initCall?: boolean): void; export default RState; export { MultiWatcher };