import { Subscriber, Unsubscribe } from '@cdellacqua/signals'; export type { Subscriber, Unsubscribe } from '@cdellacqua/signals'; /** A generic setter function. Used in {@link Store} */ export type Setter = (newValue: T) => void; /** A generic getter function. Used in {@link Store} */ export type Getter = () => T; /** A generic updater function. Used in {@link Store} */ export type Updater = (current: T) => T; /** A generic update function. Used in {@link Store} */ export type Update = (updater: (current: T) => T) => void; /** A comparison function used to optimize subscribers notifications. Used in {@link Store} */ export type EqualityComparator = (a: T, b: T) => boolean; /** A function that gets called once a store reaches 0 subscribers. Used in {@link Store} */ export type StopHandler = () => void; /** A function that gets called once a store gets at least one subscriber. Used in {@link Store} */ export type StartHandler = (set: Setter) => StopHandler | void; export declare const storeRuntime: { storeCount: number; }; /** * A store that can have subscribers and emit values to them. It also * provides the current value upon subscription. It's readonly in the * sense that it doesn't provide direct set/update methods, unlike {@link Store}, * therefore its value can only be changed by a {@link StartHandler} (see also {@link makeReadonlyStore}). */ export type ReadonlyStore = { /** * Subscribe a function to this store. * * Note: subscribers are deduplicated, if you need to subscribe the same * function more than once wrap it in an arrow function, e.g. * `signal$.subscribe((v) => myFunc(v));` * @param subscriber a function that will be called upon subscription and whenever the store value changes. */ subscribe(subscriber: Subscriber): Unsubscribe; /** * Return the current number of active subscriptions. */ nOfSubscriptions(): number; /** * Get the current value wrapped by the store. */ content(): T; /** * Get the current value wrapped by the store and register the current store as a dependency in the context of an effect. * * Example usage: * ```ts * import {makeReactiveRoot, makeStore} from 'universal-stores'; * * const {makeEffect} = makeReactiveRoot(); * const store$ = makeStore(1); * makeEffect(() => { * console.log(store$.watch()); // immediately prints 1 * }); * store$.set(2); // makes the effect above print 2 * dispose(); * store$.set(3); // does nothing, as the effect above has been unregistered * ``` * * {@see file://./effect.d.ts} */ watch(): T; }; /** * A store that can have subscribers and emit values to them. It also * provides the current value upon subscription. */ export type Store = ReadonlyStore & { /** * Set a value and send it to all subscribers. * @param v the new value of this store. */ set(v: T): void; /** * Set the new value of the store through an updater function that takes the current one as an argument * and send the returned value to all subscribers. * @param updater the update function that will receive the current value and return the new one. */ update(updater: Updater): void; }; /** * Configurations for Store and ReadonlyStore. */ export type StoreConfig = { /** (optional) a {@link StartHandler} that will get called once there is at least one subscriber to this store. */ start?: StartHandler; /** * (optional, defaults to `(a, b) => a === b`) a function that's used to determine if the current value of the store value is different from * the one being set and thus if the store needs to be updated and the subscribers notified. */ comparator?: EqualityComparator; }; /** * Make a store of type T. * * Example usage: * ```ts * const store$ = makeStore(0); * console.log(store$.content()); // 0 * store$.subscribe((v) => console.log(v)); * store$.set(10); // will trigger the above console log, printing 10 * ``` * @param initialValue the initial value of the store. * @param start a {@link StartHandler} that will get called once there is at least one subscriber to this store. * @returns a Store */ export declare function makeStore(initialValue: T | undefined, start?: StartHandler): Store; /** * Make a store of type T. * * Example usage: * ```ts * const store$ = makeStore(0); * console.log(store$.content()); // 0 * store$.subscribe((v) => console.log(v)); * store$.set(10); // will trigger the above console log, printing 10 * ``` * @param initialValue the initial value of the store. * @param config a {@link StoreConfig} which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a {@link StartHandler}. * @returns a Store */ export declare function makeStore(initialValue: T | undefined, config?: StoreConfig): Store; /** * Make a store of type T. * * Example usage: * ```ts * const store$ = makeStore(0); * console.log(store$.content()); // 0 * store$.subscribe((v) => console.log(v)); * store$.set(10); // will trigger the above console log, printing 10 * ``` * @param initialValue the initial value of the store. * @param startOrConfig a {@link StartHandler} or a {@link StoreConfig} which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a {@link StartHandler}. * @returns a Store */ export declare function makeStore(initialValue: T | undefined, startOrConfig?: StartHandler | StoreConfig): Store; /** * Make a store of type T. * * Example usage: * ```ts * let value = 0; * const store$ = makeReadonlyStore(value, (set) => { * value++; * set(value); * }); * console.log(store$.content()); // 1 * store$.subscribe((v) => console.log(v)); // immediately prints 2 * console.log(store$.content()); // 2 * ``` * @param initialValue the initial value of the store. * @param start a {@link StartHandler} that will get called once there is at least one subscriber to this store. * @returns a ReadonlyStore */ export declare function makeReadonlyStore(initialValue: T | undefined, start?: StartHandler): ReadonlyStore; /** * Make a store of type T. * * Example usage: * ```ts * const store$ = makeReadonlyStore({prop: 'some value'}, { * comparator: (a, b) => a.prop === b.prop, * start: (set) => { * // ... * }, * }); * ``` * @param initialValue the initial value of the store. * @param config a {@link StoreConfig} which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a {@link StartHandler}. * @returns a ReadonlyStore */ export declare function makeReadonlyStore(initialValue: T | undefined, config?: StoreConfig): ReadonlyStore; /** * Make a store of type T. * * Example usage: * ```ts * let value = 0; * const store$ = makeReadonlyStore(value, (set) => { * value++; * set(value); * }); * console.log(store$.content()); // 1 * store$.subscribe((v) => console.log(v)); // immediately prints 2 * console.log(store$.content()); // 2 * ``` * @param initialValue the initial value of the store. * @param startOrConfig a {@link StartHandler} or a {@link StoreConfig} which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a {@link StartHandler}. * @returns a ReadonlyStore */ export declare function makeReadonlyStore(initialValue: T | undefined, startOrConfig?: StartHandler | StoreConfig): ReadonlyStore;