@lomray/react-mobx-manager
Version:
This package provides Mobx stores manager for react.
144 lines (143 loc) • 4.61 kB
TypeScript
/// <reference types="node" />
import Events from "./events.js";
import Logger from "./logger.js";
import { ILoggerOpts } from "./logger.js";
import Manager from "./manager.js";
import CombinedStorage from "./storages/combined-storage.js";
import StoreStatus from "./store-status.js";
interface IWindowManager {
push: (state: Record<string, any>) => void;
}
declare global {
interface Window {
mbxM: Record<string, any>[] | IWindowManager;
}
}
interface IConstructorParams<TProps = any> {
storeManager: Manager;
getStore: <T>(store: IConstructableStore<T>, params?: Partial<IStoreParams>) => T | undefined;
componentProps: TProps;
initState?: Record<string, any>;
}
interface IStoreLifecycle {
onDestroy?: () => void;
}
interface IStore extends IStoreLifecycle {
libStoreId?: string;
libStoreContextId?: string;
libStoreParentId?: string;
libStoreSuspenseId?: string;
libStoreComponentName?: string;
libStoreStatus?: StoreStatus;
libDestroyTimer?: NodeJS.Timeout;
isGlobal?: boolean;
init?: () => void;
toJSON?: () => Record<string, any>;
}
interface IStorePersisted extends IStore {
libStorageOptions?: IPersistOptions;
addOnChangeListener?: (store: IStorePersisted, manager: Manager) => (() => void) | undefined;
wakeup?: TWakeup;
}
type TInitStore<TSto = IStore> = TSto & TAnyStore;
type IConstructableStore<TSto = IStore> = (new (props: IConstructorParams) => TInitStore<TSto>) & Partial<IStorePersisted>;
/**
* Store params
*/
type IStoreConfig = {
id?: string;
isParent?: boolean;
};
type TStoreDefinition<TSto extends TAnyStore = any> = IConstructableStore<TSto> | ({
store: IConstructableStore<TSto>;
} & IStoreConfig);
type TMapStores = Record<string, TStoreDefinition>;
interface IManagerParams {
storesParams?: Omit<IConstructorParams, 'storeManager' | 'getStore' | 'componentProps'>;
storage?: IStorage | CombinedStorage;
options?: IManagerOptions;
initState?: Record<string, any>;
logger?: Logger | Omit<ILoggerOpts, 'manager'>;
}
type TWakeup = (state: {
manager: Manager;
initState?: Record<string, any>;
persistedState?: Record<string, any>;
}) => void;
interface IStorage {
get: () => Record<string, any> | undefined | Promise<Record<string, any> | undefined>;
set: (value: Record<string, any> | undefined) => Record<string, any> | undefined | Promise<any> | void;
flush: () => void | Promise<any>;
}
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';
}
type TAnyStore = IStore | IStorePersisted;
type TStores = {
[storeKey: string]: TAnyStore;
};
/**
* Convert class type to class constructor
*/
type ClassReturnType<T> = T extends new (...args: any) => infer R ? R : T extends {
store: any;
} ? ClassReturnType<T['store']> : never;
/**
* Stores map to type
*/
type StoresType<TSt> = {
[keys in keyof TSt]: ClassReturnType<TSt[keys]>;
};
interface IStoreParams {
id?: string;
key?: string;
contextId?: string;
parentId?: string;
suspenseId?: string;
componentName?: string;
componentProps?: Record<string, any>;
}
interface IWithStoreOptions {
customContextId?: string;
}
interface IMobxManagerEvents {
[Events.CREATE_STORE]: {
store: IConstructableStore;
};
[Events.MOUNT_STORE]: {
store: TAnyStore;
};
[Events.UNMOUNT_STORE]: {
store: TAnyStore;
};
[Events.DELETE_STORE]: {
store: TAnyStore;
};
}
interface IGroupedStores {
relativeStores: TStores;
parentStores: TStores;
globalStores: TStores;
hasCreationFailure: boolean;
}
interface IPersistOptions {
behaviour?: 'exclude' | 'include';
attributes?: {
[storageId: string]: string[];
};
isNotExported?: boolean;
}
export { IWindowManager, IConstructorParams, IStoreLifecycle, IStore, IStorePersisted, TInitStore, IConstructableStore, IStoreConfig, TStoreDefinition, TMapStores, IManagerParams, TWakeup, IStorage, IManagerOptions, TAnyStore, TStores, ClassReturnType, StoresType, IStoreParams, IWithStoreOptions, IMobxManagerEvents, IGroupedStores, IPersistOptions };