export default function minsto( model?: TModel ): Store; export type Store = StoreBase & ModelChildrenInfer & ModelActionsInfer & ModelStateInfer & ModelComputedInfer & ModelWatchersInfer; export interface StoreModel { name?: string; state?: {}; merge?(store: any, nextState: any): void; computed?: {}; actions?: {}; listeners?: { [key: string]: Listener; }; watchers?: { [key: string]: Listener>; }; isolate?: boolean; onChange?(e: StateChangeEventArgs): void; init?(store?: any, options?: InitOptions): any; children?: {}; } export interface InitOptions { parent?: Store; initial?: any; } export interface StoreBase { readonly name: string; readonly loading: boolean; readonly error: any; onChange(listener: Listener>): Unsubscribe; onDispatch(listener: Listener>): Unsubscribe; when(action: string | (Action | string)[]): Promise; when( action: string | (Action | string)[], callback: Listener> ): Unsubscribe; when(listeners: { [key: string]: Listener>; }): Unsubscribe; watch( prop: string, callback: Listener> ): Unsubscribe; watch( props: string[], callback: Listener> ): Unsubscribe; watch( selector: (state: ModelStateInfer) => TResult, callback: Listener>, options?: WatchOptions ): Unsubscribe; getState(): ModelStateInfer; mergeState( state: ModelStateInfer & ModelChildStateInfer ): void; mergeState( state: ModelStateInfer & ModelChildStateInfer, applyToAllChildren: boolean ): void; dispatch( action: Action, payload?: TPayload ): TResult; $(props: { [key: string]: any }): void; $(prop: string): any; $( prop: string, value: ((value: any, loadable?: Loadable) => any) | any ): void; lock( props: keyof TModel | string | (string | keyof TModel)[], promise: Promise ): void; mutate< TKey extends keyof ModelStateInfer, TValue extends ModelStateInfer[TKey] >( prop: TKey, value: ((prev: TValue) => TValue) | TValue ): void; loadableOf>( prop: TProp ): Loadable[TProp]>; loadableOf(prop: string): Loadable; callback( fn: TCallback, ...cacheKeys: any[] ): TCallback; } export interface Loadable { readonly value: T; readonly status: "loading" | "hasError" | "hasValue"; readonly error: any; readonly promise: Promise; readonly loading: boolean; readonly hasError: boolean; readonly hasValue: boolean; } export interface StateChangeEventArgs { store: Store; state: ModelStateInfer; } export interface ValueChangeEventArgs extends StateChangeEventArgs { previous: TValue; current: TValue; } export interface DispatchEventArgs { type: string; store: Store; payload: TPayload; } export type Unsubscribe = () => void; export type Action = ( store?: Store, payload?: TPayload, task?: Task ) => TResult; export type ActionBody = ( store?: Store, payload?: TPayload, task?: Task ) => TResult; export type ModelActionsInfer = TModel extends { actions: infer TActions; } ? { [key in keyof TActions]: ActionDispatcherInfer } : {}; export type ModelStateInfer = TModel extends { state: infer TState } ? { [key in keyof TState]: TState[key] } : {}; export type ActionDispatcherInfer = TAction extends Action< any, infer TPayload, infer TResult > ? (payload?: TPayload) => TResult : never; export type ModelComputedInfer = TModel extends { state: infer TComputed; } ? { [key in keyof TComputed]: any } : {}; export type ModelChildrenInfer = TModel extends { children: infer TChildren; } ? { [key in keyof TChildren]: Store } : {}; export type ModelChildStateInfer = TModel extends { children: infer TChildren; } ? { [key in keyof TChildren]: ModelStateInfer } : {}; export type Listener = (args?: T, task?: Task) => any; export interface Cancellable { cancelled(): boolean; cancel(): void; } export interface Task extends Cancellable { wrap(promise: Promise): Promise & Cancellable; wrap(fn: T): T; delay(ms: number, fn?: Function): Promise & Cancellable; debounce(ms: number): Promise & Cancellable; latest(): void; call any>( fn: T, ...args: Parameters ): CallResultInfer>; create(fn: (subTask?: Task) => TResult): TResult; create(): Task; onCancel(callback: Function): Unsubscribe; all( targets: Promise[], callback?: (results?: any[]) => any ): Promise & Cancellable; race< T extends { [key: string]: Promise }, TResult extends { [key in keyof T]: any } >( targets: T, callback?: (results?: TResult) => any ): Promise & Cancellable; } export type CallResultInfer = T extends Promise ? Promise & Cancellable : T; export interface WatchOptions { initial: boolean; } export type ModelWatchersInfer = {};