UNPKG

@cervello/react

Version:

🤯 Simple, reactive, tiny and performant state-management library

59 lines (58 loc) • 3.12 kB
import type { Maybe, UseSelector, WithoutType } from '../../types/shared'; type CervelloStoreUseParam<StoreType> = { onChange: (cb: (store: StoreType) => void) => void; onPartialChange: <Attributes extends keyof WithoutType<StoreType, Function>, R extends Exclude<Attributes, '$value'>, ArrayOfAttributes extends Array<R>>(selectors: ArrayOfAttributes, cb: (store: StoreType) => void) => void; }; /** * Use function which allows you to listen for changes in the store. */ export type CervelloUseFunction<StoreType> = (param: CervelloStoreUseParam<StoreType>) => void; declare type Prettify<T> = {} & { [K in keyof T]: T[K]; }; type CervelloStore<StoreType, StoreName extends string | undefined = undefined> = StoreName extends string ? Prettify<{ [K in `${StoreName}Store`]: StoreType; } & { [K in `use${Capitalize<StoreName>}Store`]: () => StoreType; } & { [K in `use${Capitalize<StoreName>}Selector`]: UseSelector<StoreType>; } & { [K in `reset${Capitalize<StoreName>}Store`]: () => void; } & { /** Functions to be attached to changes in a general way (tracking, loggers, ... etc) */ use: (...functions: Array<(useObj: CervelloStoreUseParam<StoreType>) => void>) => CervelloStore<StoreType, StoreName>; }> : { /** Reactive store */ store: StoreType; /** Hook to react to all changes done to 'store' */ useStore: () => StoreType; /** Hook to react to changes done to 'store' in all the attributes provided */ useSelector: UseSelector<StoreType>; /** Resets the value to the initial value passed in */ reset: () => void; /** Functions to be attached to changes in a general way (tracking, loggers, ... etc) */ use: (...functions: Array<(useObj: CervelloStoreUseParam<StoreType>) => void>) => CervelloStore<StoreType, StoreName>; }; export type CervelloOptions<StoreName extends string | undefined = undefined> = { name?: StoreName; reactiveNestedObjects?: boolean; }; /** * Creates a store that is reactive and can be used inside and outside of React components. * @param initialValue - Function which returns the default values for the store * * @returns \{ store, useStore, useSelector \} or `{ exampleStore, useExampleStore, useExampleSelector }` if name is provided in options with 'example' value */ export declare function cervello<T, StoreName extends string | undefined = undefined>(initialValue: () => T, options?: CervelloOptions<StoreName>): Prettify<CervelloStore<Prettify<T & { $value: Maybe<T>; }>, StoreName>>; /** * Creates a store that is reactive and can be used inside and outside of React components. * @param initialValue - Object with the default values for the store * * @returns - \{ store, useStore, useSelector \} or `{ exampleStore, useExampleStore, useExampleSelector }` if name is provided in options with 'example' value */ export declare function cervello<T extends Record<string, unknown>, StoreName extends string | undefined = undefined>(initialValue: T, options?: CervelloOptions<StoreName>): Prettify<CervelloStore<Prettify<T & { $value: Maybe<T>; }>, StoreName>>; export {};