@d3vtool/hooks
Version:
Collection of custom React hooks to simplify tasks in your React projects.
62 lines (61 loc) • 2.71 kB
TypeScript
import { SetStateAction } from "react";
export type OnChangeAction = () => void;
export type InitializerAction<T> = () => T;
export declare class Hub<T> {
protected currentState: T;
private onChangeListeners;
private listeners;
constructor(initialState: T);
getCurrentState(): T;
setCurrentState(newState: T): void;
attachListener(listener: React.Dispatch<SetStateAction<T>>): void;
detachListener(listener: React.Dispatch<SetStateAction<T>>): void;
notifyListener(newState: T): void;
onChange(callback: VoidFunction): void;
removeOnChange(callback: VoidFunction): void;
}
/**
* Creates a shared state hub to manage and synchronize state across multiple components or contexts.
*
* @template T - The type of the state managed by the hub.
* @param {T | InitializerAction<T>} initialState - The initial state value or a function that returns the initial state.
* @returns {Hub<T>} - Returns a hub object that manages the shared state, allowing components to subscribe to and update the state.
*
* @example
* // Example of creating a hub with an initial state:
* const myHub = createHub({ count: 0 });
*
* // Example of creating a hub with an initializer function:
* const myHub = createHub(() => ({ count: 0 }));
*
* // The created hub can be used with hooks like `useHub` or `useReadHub`.
*/
export declare function createHub<T>(initialState: T | InitializerAction<T>): Hub<T>;
/**
* A type representing a function that computes a derived state based on the current state of the hub.
*
* @template T - The type of the state managed by the hub.
* @param {T} currentState - The current state of the hub.
* @returns {T} - The computed or derived state.
*/
export type ComputeAction<T> = (currentState: T) => T;
/**
* Creates a new computed hub that derives its state based on the current state of an existing hub using a compute action.
*
* @template T - The type of the state managed by the hub.
* @param {Hub<T>} hub - The original hub from which the state will be derived.
* @param {ComputeAction<T>} computeAction - A function that computes the new state based on the current state of the original hub.
* @returns {Hub<T>} - Returns a new hub object that manages the computed state.
*
* @example
* // Example of creating a computed hub:
* const myHub = createHub({ count: 0 });
*
* // Create a computed hub that doubles the count value:
* const computedHub = createComputedHub(myHub, (state) => ({
* count: state.count * 2
* }));
*
* // The computed hub can be used with hooks like `useHub` to get the derived state.
*/
export declare function createComputedHub<T>(hub: Hub<T>, computeAction: ComputeAction<T>): Hub<T>;