@known-as-bmf/store
Version:
Lightweight synchronous state management library.
71 lines • 2.46 kB
TypeScript
import { SubscriptionCallback, Selector, Store, Middleware } from './types';
/**
* Create a store.
*
* @param initialState - The initial value of the state.
* @param middleware - Middleware to use for this store. You can compose multiple
* middlewares with `composeMiddlewares` and `pipeMiddlewares`.
*
* @public
*/
export declare function of<S>(initialState: S, middleware?: Middleware<S>): Store<S>;
/**
* Return the current state of a store.
*
* @param store - The store you want to get the current state from.
*
* @throws `TypeError` if the store is not a correct `Store` instance.
*
* @public
*/
export declare function deref<S>(store: Store<S>): S;
/**
* Change the state of a store using a function.
*
* @param store - The store of which you want to change the state.
* @param mutationFn - The function used to compute the value of the future state.
*
* @throws `TypeError` if the store is not a correct `Store` instance.
*
* @public
*/
export declare function swap<S>(store: Store<S>, mutationFn: (current: S) => S): void;
/**
* Change the state of a store with a new one.
*
* @param store - The store of which you want to change the state.
* @param current - The new state.
*
* @throws `TypeError` if the store is not a correct `Store` instance.
*
* @public
*/
export declare function set<S>(store: Store<S>, current: S): void;
/**
* Subscribe to state changes.
*
* @param store - The store you want to subscribe to.
* @param callback - The function to call when the state changes.
*
* @returns An unsubscribe function for this specific subscription.
*
* @throws `TypeError` if the store is not a correct `Store` instance.
*
* @public
*/
export declare function subscribe<S>(store: Store<S>, callback: SubscriptionCallback<S>): () => void;
/**
* Subscribe to state changes.
*
* @param store - The store you want to subscribe to.
* @param callback - The function to call when the state changes.
* @param selector - The selector function, narrowing down the part of the state you want to subscribe to.
*
* @returns An unsubscribe function for this specific subscription.
*
* @throws `TypeError` if the store is not a correct `Store` instance.
*
* @public
*/
export declare function subscribe<S, R>(store: Store<S>, callback: SubscriptionCallback<S>, selector: Selector<S, R>): () => void;
//# sourceMappingURL=store.d.ts.map