import { Patch } from 'immer'; import { History } from './History'; import { Delete } from './utilityTypes'; export type SubscriberAndCallbacksFor = { subscribe: Watcher>['subscribe']; getState: () => { prev: StateFor; current: StateFor; }; actions: CallbacksFor; query: QueryCallbacksFor; history: History; }; export type StateFor = M extends MethodsOrOptions ? S : never; export type CallbacksFor = M extends MethodsOrOptions ? { [T in ActionUnion['type']]: (...payload: ActionByType, T>['payload']) => void; } & { history: { undo: () => void; redo: () => void; clear: () => void; throttle: (rate?: number) => Delete<{ [T in ActionUnion['type']]: (...payload: ActionByType, T>['payload']) => void; }, M extends Options ? M['ignoreHistoryForActions'][number] : never>; merge: () => Delete<{ [T in ActionUnion['type']]: (...payload: ActionByType, T>['payload']) => void; }, M extends Options ? M['ignoreHistoryForActions'][number] : never>; ignore: () => Delete<{ [T in ActionUnion['type']]: (...payload: ActionByType, T>['payload']) => void; }, M extends Options ? M['ignoreHistoryForActions'][number] : never>; }; } : {}; export type Methods = any, Q = any> = (state: S, query: Q) => R; export type Options = any, Q = any> = { methods: Methods; ignoreHistoryForActions: ReadonlyArray; normalizeHistory?: (state: S) => void; }; export type MethodsOrOptions = any, Q = any> = Methods | Options; export type MethodRecordBase = Record S extends object ? S | void : S>; export type Action = { type: T; payload?: P; config?: Record; }; export type ActionUnion = { [T in keyof R]: { type: T; payload: Parameters; }; }[keyof R]; export type ActionByType = A extends { type: infer T2; } ? T extends T2 ? A : never : never; export type QueryMethods = any> = (state?: S, options?: O) => R; export type QueryCallbacksFor = M extends QueryMethods ? { [T in ActionUnion['type']]: (...payload: ActionByType, T>['payload']) => ReturnType; } & { history: { canUndo: () => boolean; canRedo: () => boolean; }; } : {}; export type PatchListenerAction = { type: keyof CallbacksFor; params: any; patches: Patch[]; }; export type PatchListener = (newState: S, previousState: S, actionPerformedWithPatches: PatchListenerAction, query: QueryCallbacksFor, normalizer: (cb: (draft: S) => void) => void) => void; export declare function useMethods>(methodsOrOptions: MethodsOrOptions, // methods to manipulate the state initialState: any): SubscriberAndCallbacksFor>; export declare function useMethods, Q extends QueryMethods>(methodsOrOptions: MethodsOrOptions>, // methods to manipulate the state initialState: any, queryMethods: Q): SubscriberAndCallbacksFor, Q>; export declare function useMethods, Q extends QueryMethods>(methodsOrOptions: MethodsOrOptions>, // methods to manipulate the state initialState: any, queryMethods: Q, patchListener: PatchListener>, Q>): SubscriberAndCallbacksFor, Q>; export declare function createQuery(queryMethods: Q, getState: any, history: History): QueryCallbacksFor & { history: { canUndo: () => boolean; canRedo: () => boolean; }; }; declare class Watcher { getState: any; subscribers: Subscriber[]; constructor(getState: any); /** * Creates a Subscriber * @returns {() => void} a Function that removes the Subscriber */ subscribe(collector: (state: S) => C, onChange: (collected: C) => void, collectOnCreate?: boolean): () => void; unsubscribe(subscriber: any): Subscriber[]; notify(): void; } declare class Subscriber { collected: any; collector: () => any; onChange: (collected: any) => void; id: any; /** * Creates a Subscriber * @param collector The method that returns an object of values to be collected * @param onChange A callback method that is triggered when the collected values has changed * @param collectOnCreate If set to true, the collector/onChange will be called on instantiation */ constructor(collector: any, onChange: any, collectOnCreate?: boolean); collect(): void; } export {};