fluent-state
Version:
Fluent, immutable, deeply reactive state for React — powered by Proxies and fine-grained tracking
36 lines (33 loc) • 1.21 kB
TypeScript
type Primitive = string | number | boolean | null | undefined | symbol;
type Access<T> = {
(): T;
(value: T | ((prev: T) => T)): void;
};
type FluentProxyType<T> = [T] extends [Primitive] ? Access<T> : T extends Array<infer U> ? FluentArrayProxyType<U> : FluentObjectProxyType<T>;
type FluentObjectProxyType<T> = {
[K in keyof T]: FluentProxyType<T[K]>;
} & Access<T>;
type FluentArrayProxyType<T> = Array<FluentProxyType<T> & Access<T>> & Access<T[]>;
type OnChange = {
(): void;
};
declare function getValue(path: (string | number)[], target: any): any;
declare class FluentProxy<T> {
private _state;
private _proxy;
private _onChange;
private proxyCache;
currentExecutionStack?: Set<string>;
constructor(initialState: T, onChange: OnChange);
getState(): T;
getProxy(): FluentProxyType<T>;
createProxy<T>(path?: string[]): FluentProxyType<T>;
}
type UseFluentStateReturn<T> = [
FluentProxyType<T>,
(cb: () => void) => void,
<U>(cb: () => U) => () => U
];
declare function useFluentState<T>(initialState: T): UseFluentStateReturn<T>;
export { FluentProxy, getValue, useFluentState };
export type { Access, FluentProxyType, OnChange, Primitive };