import type Events from './events.js'; import type { ILoggerOpts } from './logger.js'; import type Logger from './logger.js'; import type Manager from './manager.js'; import type CombinedStorage from './storages/combined-storage.js'; import type StoreStatus from './store-status.js'; export interface IWindowManager { push: (state: Record) => void; } declare global { interface Window { mbxM: Record[] | IWindowManager; } } export interface IConstructorParams { storeManager: Manager; getStore: (store: IConstructableStore, params?: Partial) => T | undefined; componentProps: TProps; initState?: Record; } export interface IStoreLifecycle { init?: () => void | (() => void); onDestroy?: () => void; onComponentPropsUpdate?: (props: any) => void; } export interface IStore extends IStoreLifecycle { libStoreId?: string; libStoreContextId?: string; libStoreParentId?: string; libStoreSuspenseId?: string; libStoreComponentName?: string; libStoreStatus?: StoreStatus; libDestroyTimer?: ReturnType; isGlobal?: boolean; toJSON?: () => Record; } export interface IRelativeStore extends IStore { } export interface IGlobalStore extends IStore { } export interface IStorePersisted extends IStore { libStorageOptions?: IPersistOptions; addOnChangeListener?: (store: IStorePersisted, manager: Manager) => (() => void) | undefined; wakeup?: TWakeup; } export type TInitStore = TSto & TAnyStore; export type IConstructableStore = (new (props: IConstructorParams) => TInitStore) & Partial; /** * Store params */ export type IStoreConfig = { id?: string; isParent?: boolean; }; export type TStoreDefinition = IConstructableStore | ({ store: IConstructableStore; } & IStoreConfig); export type TMapStores = Record; export interface IManagerParams { storesParams?: Omit; storage?: IStorage | CombinedStorage; options?: IManagerOptions; initState?: Record; logger?: Logger | Omit; } export type TWakeup = (state: { manager: Manager; initState?: Record; persistedState?: Record; }) => void; /** * Changes of one storage passed to IStorage.saveChanges */ export interface IStorageChanges { /** * Whole storage state after the changes: store id => store state (same value `set` receives) */ value: Record; /** * Changed stores: store id => store state JSON */ changes: Map; /** * Whole state JSON, equal to JSON.stringify(value) but assembled from cached store JSON */ toJSON: () => string; } export interface IStorage { get: () => Record | undefined | Promise | undefined>; set: (value: Record | undefined) => Record | undefined | Promise | void; flush: () => void | Promise; /** * Optional incremental save. When defined, CombinedStorage calls it instead of `set` */ saveChanges?: (changes: IStorageChanges) => void | Promise; } export interface IManagerOptions { shouldDisablePersist?: boolean; shouldRemoveInitState?: boolean; destroyTimers?: { init?: number; touched?: number; unused?: number; }; /** * When for some strange reason stores cannot be created or found in the parent context: * none: don't do anything * dummy: force create empty store * empty (default): don't render component if any of the stores not created */ failedCreationStrategy?: 'none' | 'dummy' | 'empty'; /** * Batch persisted stores changes and save them at most once per this delay (ms). * 0 - save on every change. Server side always saves immediately. * Default: 100 */ persistDelay?: number; } export type TAnyStore = IStore | IRelativeStore | IGlobalStore | IStorePersisted; export type TStores = { [storeKey: string]: TAnyStore; }; /** * Convert class type to class constructor */ export type ClassReturnType = T extends new (...args: any) => infer R ? R : T extends { store: any; } ? ClassReturnType : never; /** * Stores map to type */ export type StoresType = { [keys in keyof TSt]: ClassReturnType; }; export interface IStoreParams { id?: string; key?: string; contextId?: string; parentId?: string; suspenseId?: string; componentName?: string; componentProps?: Record; } export interface IWithStoreOptions { customContextId?: string; } export interface IMobxManagerEvents { [Events.CREATE_STORE]: { store: IConstructableStore; }; [Events.MOUNT_STORE]: { store: TAnyStore; }; [Events.UNMOUNT_STORE]: { store: TAnyStore; }; [Events.DELETE_STORE]: { store: TAnyStore; }; } export interface IGroupedStores { relativeStores: TStores; parentStores: TStores; globalStores: TStores; hasCreationFailure: boolean; } export interface IPersistOptions { behaviour?: 'exclude' | 'include'; attributes?: { [storageId: string]: string[]; }; isNotExported?: boolean; }