@fimbul-works/observable
Version:
A lightweight, strongly-typed TypeScript library for reactive programming patterns, providing observable collections, values, and event handling.
147 lines (146 loc) • 6.01 kB
TypeScript
import type { CollectionEvent, Observable } from "./types";
/**
* An observable Set implementation that emits events when its contents change.
* Uses boolean values in events to indicate presence in the set.
*
* @template T The type of values stored in the set
* @implements {Observable<CollectionEvent<T, boolean>>}
*/
export declare class ObservableSet<T> implements Observable<CollectionEvent<T, boolean>> {
#private;
/**
* Creates a new ObservableSet instance.
* @param values - Optional initial values for the set
*/
constructor(values?: Iterable<T>);
/**
* Implements the Iterator protocol.
*/
[Symbol.iterator](): IterableIterator<T>;
/**
* @returns the number of (unique) elements in Set.
*/
get size(): number;
/**
* Adds a value to the set and emits an add event if it wasn't already present.
* @param value - The value to add to the set
* @returns {this} The set instance for method chaining
*/
add(value: T): this;
/**
* Adds a value to the set and waits for all change handlers to complete.
* @param value - The value to add
* @returns {Promise<this>} Promise resolving to the set instance for method chaining
*/
addAsync(value: T): Promise<this>;
/**
* Removes a value from the set and emits a delete event if it was present.
* @param value - The value to remove from the set
* @returns {boolean} True if the value was removed, false if it wasn't in the set
*/
delete(value: T): boolean;
/**
* Deletes a value from the set and waits for all change handlers to complete.
* @param value - The value to delete
* @returns {Promise<boolean>} Promise resolving to true if the value was deleted, false otherwise
*/
deleteAsync(value: T): Promise<boolean>;
/**
* Checks if a value exists in the set.
*
* @param value - The value to check
* @returns {boolean} True if the value exists in the set, false otherwise
*/
has(value: T): boolean;
/**
* Removes all values from the set and emits a clear event.
*/
clear(): void;
/**
* Clears the set and waits for all change handlers to complete.
* @returns {Promise<void>}
*/
clearAsync(): Promise<void>;
/**
* Returns an iterator over the set's values.
* @returns {IterableIterator<T>} An iterator over the set's values
*/
values(): IterableIterator<T>;
/**
* Registers a function to be called when the set changes.
* @param fn - Function to be called with collection events
* @returns {() => void} A cleanup function that removes the event handler
*/
onChange(fn: (event: CollectionEvent<T, boolean>) => void): () => void;
/**
* Creates a new Set containing all the elements from this set and the other set.
* @param other - The other set to union with
* @returns {ObservableSet<T>} A new ObservableSet containing the union
*/
union(other: Set<T> | ObservableSet<T>): ObservableSet<T>;
/**
* Creates a new Set containing elements present in both this set and the other set.
* @param other - The other set to intersect with
* @returns {ObservableSet<T>} A new ObservableSet containing the intersection
*/
intersection(other: Set<T> | ObservableSet<T>): ObservableSet<T>;
/**
* Creates a new Set containing elements in this set that are not in the other set.
* @param other - The other set to difference with
* @returns {ObservableSet<T>} A new ObservableSet containing the difference
*/
difference(other: Set<T> | ObservableSet<T>): ObservableSet<T>;
/**
* Creates a new Set containing elements that are in either set but not both.
* @param other - The other set to compute symmetric difference with
* @returns {ObservableSet<T>} A new ObservableSet containing the symmetric difference
*/
symmetricDifference(other: Set<T> | ObservableSet<T>): ObservableSet<T>;
/**
* Checks if this set is a subset of another set.
* @param other - The other set to check against
* @returns {boolean} True if this set is a subset of the other set
*/
isSubsetOf(other: Set<T> | ObservableSet<T>): boolean;
/**
* Checks if this set is a superset of another set.
* @param other - The other set to check against
* @returns {boolean} True if this set is a superset of the other set
*/
isSupersetOf(other: Set<T> | ObservableSet<T>): boolean;
/**
* Checks if this set has no elements in common with another set.
* @param other - The other set to check against
* @returns {boolean} True if the sets are disjoint
*/
isDisjointFrom(other: Set<T> | ObservableSet<T>): boolean;
/**
* Executes a provided function once per each value in the Set.
* @param callbackfn - Function to execute for each element
* @param thisArg - Value to use as this when executing callback
*/
forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: unknown): void;
/**
* Creates a new ObservableSet with transformed elements.
* @template U The type of elements in the new set
* @param transform - Function to transform each element
* @returns {ObservableSet<U>} A new ObservableSet with transformed elements
*/
map<U>(transform: (value: T) => U): ObservableSet<U>;
/**
* Creates a new ObservableSet with elements that pass the test.
* @param predicate - Function to test each element
* @returns {ObservableSet<T>} A new filtered ObservableSet
*/
filter(predicate: (value: T) => boolean): ObservableSet<T>;
/**
* Converts the set to an array.
* @returns {Array<T>} An array containing all elements
*/
toArray(): T[];
/**
* Converts the set to a JSON-serializable format.
* @returns {Array<T>} An array suitable for JSON serialization
*/
toJSON(): T[];
}