mute8
Version:
JS State Container
79 lines (77 loc) • 2.75 kB
TypeScript
declare const MUT_FN_NAME = "mut";
type CallArgs = any[] | Function;
interface Plugin<T> {
/** BeforeInit() */
BI(initState: T): T;
/** BeforeUpdate() */
BU(newState: T): T;
/** AfterChange() */
AC(oldState: Readonly<T>, newState: T, actionName?: string, args?: CallArgs): void;
}
declare class Subject<T> {
private id;
private c;
private s;
private p;
private ps;
constructor(state: T, plugin?: Plugin<T>);
destroy(): void;
sanp(): Readonly<T>;
sub(fn: SubFn<T>): Sub;
mut(update: Partial<T>, actionName?: string, args?: CallArgs): void;
set<K extends keyof T>(key: K & string | typeof MUT_FN_NAME, value: T[K] | Partial<T>): void;
select<O>(fn: SelectFn<T, O>): Observer<O>;
}
interface Observer<T> {
sub(fn: SubFn<T>): Sub;
select<O>(fn: SelectFn<T, O>): Observer<O>;
sanp(): Readonly<T>;
destroy(): void;
}
declare class StoreCore<T extends object, A, AA> {
private readonly p;
readonly s: Subject<T>;
constructor(d: StoreDefiniton<T, A, AA>);
private mut;
static build<T extends object, A, AA>(state: StoreDefiniton<T, A, AA>, ext?: ProxyExtension<T, A, AA>): Store<T, A, AA>;
}
interface SmalProxy<T, A> {
snap(): T;
set mut(v: Partial<T>);
get mut(): (fn: (v: T) => void, actionName?: string) => void;
actions: Readonly<A>;
}
interface StoreProxy<T, A, AA> extends SmalProxy<T, A> {
sub(fn: SubFn<T>): Sub;
select<O>(fn: (value: Readonly<T>) => O): Observer<O>;
async: Readonly<AA>;
}
interface ProxyExtension<T extends object, A, AA> {
name: string;
init(core: StoreCore<T, A, AA>): object;
}
declare const buildProxy: typeof StoreCore.build;
type ExcludeKeys = {
async?: never;
actions?: never;
snap?: never;
sub?: never;
mut?: never;
};
type PluginBuilder = (<T extends object>(proxy: StoreProxy<T, any, any>) => Plugin<T>) | null;
type Store<T, A, AA> = StoreProxy<T, A, AA> & T;
type SubFn<T> = (value: Readonly<T>) => void;
type SelectFn<T, O> = (value: Readonly<T>) => O;
type Sub = {
destroy(): void;
};
type VoidFn = ((...args: any) => undefined);
type AsyncFn = ((...args: any) => Promise<void>);
interface StoreDefiniton<T extends object, A, AA> {
value: T & object & ExcludeKeys;
actions?: A & ThisType<T> & Record<string, VoidFn>;
async?: AA & ThisType<SmalProxy<T, A>> & Record<string, AsyncFn>;
plugin?: PluginBuilder;
}
declare const newStore: <T extends object, A, AA>(state: StoreDefiniton<T, A, AA>) => Store<T, A, AA>;
export { type AsyncFn, type CallArgs, type Plugin, type PluginBuilder, type ProxyExtension, type SelectFn, type Store, type StoreDefiniton, type StoreProxy, type Sub, type SubFn, type VoidFn, buildProxy, newStore };