@fimbul-works/observable
Version:
A lightweight, strongly-typed TypeScript library for reactive programming patterns, providing observable collections, values, and event handling.
93 lines (92 loc) • 4.17 kB
TypeScript
import { ObservableMap } from "./map.js";
/**
* A strict key-value registry that enforces unique registration and required existence.
* Extends ObservableMap to provide change notifications while adding stricter guarantees.
*
* @template K The type of keys in the registry
* @template V The type of values in the registry
* @extends {ObservableMap<K, V>}
*/
export declare class ObservableRegistry<K, V> extends ObservableMap<K, V> {
/**
* Registers a new key-value pair in the registry.
* Throws an error if the key is already registered.
*
* @param key - The key to register
* @param value - The value to associate with the key
* @returns {this} The registry instance for method chaining
* @throws {Error} If the key is already registered
*/
register(key: K, value: V): this;
/**
* Registers a new key-value pair and waits for all change handlers to complete.
* @param key - The key to register
* @param value - The value to associate with the key
* @returns {Promise<this>} Promise resolving to the registry instance for method chaining
* @throws {Error} If the key is already registered
*/
registerAsync(key: K, value: V): Promise<this>;
/**
* Removes a registered key-value pair from the registry.
*
* @param key - The key to unregister
* @returns {boolean} True if the key was unregistered, false if it wasn't registered
*/
unregister(key: K): boolean;
/**
* Removes a registered key-value pair and waits for all change handlers to complete.
* @param key - The key to unregister
* @returns {Promise<boolean>} Promise resolving to true if the key was unregistered, false otherwise
*/
unregisterAsync(key: K): Promise<boolean>;
/**
* Retrieves a value from the registry by its key.
* Unlike the parent ObservableMap, this method throws if the key doesn't exist.
* @param key - The key to look up
* @param throwErrorOnMissing - Throws error when a key is registered if true
* @returns {V} The value associated with the key
* @throws {Error} If the key is not registered
* @override
*/
get(key: K, throwErrorOnMissing?: boolean): V | undefined;
/**
* Updates a registered value if it exists.
* Unlike set(), this method throws if the key doesn't exist.
* @param key - The key to update
* @param value - The new value to associate with the key
* @returns {this} The registry instance for method chaining
* @throws {Error} If the key is not registered
*/
update(key: K, value: V): this;
/**
* Updates a registered value and waits for all change handlers to complete.
* @param key - The key to update
* @param value - The new value to associate with the key
* @returns {Promise<this>} Promise resolving to the registry instance for method chaining
* @throws {Error} If the key is not registered
*/
updateAsync(key: K, value: V): Promise<this>;
/**
* Updates a registered value using a transformation function.
* Throws if the key doesn't exist.
* @param key - The key to update
* @param updateFn - Function that receives the current value and returns the new value
* @returns {this} The registry instance for method chaining
* @throws {Error} If the key is not registered
*/
updateWith(key: K, updateFn: (currentValue: V) => V): this;
/**
* Updates a registered value using a transformation function and waits for all change handlers to complete.
* @param key - The key to update
* @param updateFn - Function that receives the current value and returns the new value
* @returns {Promise<this>} Promise resolving to the registry instance for method chaining
* @throws {Error} If the key is not registered
*/
updateWithAsync(key: K, updateFn: (currentValue: V) => V): Promise<this>;
/**
* Checks if all provided keys are registered.
* @param keys - The keys to check
* @returns {boolean} True if all keys are registered, false otherwise
*/
hasAll(keys: Iterable<K>): boolean;
}