mobx-keystone
Version:
A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more
132 lines (131 loc) • 5.5 kB
TypeScript
import { fromSnapshotOverrideTypeSymbol, ModelClass, propsTypeSymbol, toSnapshotOverrideTypeSymbol } from '../modelShared/BaseModelShared';
import { ModelProps, ModelPropsToTransformedCreationData, ModelPropsToUntransformedData } from '../modelShared/prop';
import { SnapshotInOfModel, SnapshotOutOfModel } from '../snapshot/SnapshotOf';
import { TypeCheckError } from '../types/TypeCheckError';
import { modelIdKey, modelTypeKey } from './metadata';
/**
* @ignore
*/
export declare const modelIdPropertyNameSymbol: unique symbol;
/**
* @ignore
*/
export type ModelIdPropertyType<TProps extends ModelProps, ModelIdPropertyName extends string> = [
ModelIdPropertyName
] extends [never] ? never : ModelPropsToUntransformedData<Pick<TProps, ModelIdPropertyName>>[ModelIdPropertyName];
/**
* Base abstract class for models. Use `Model` instead when extending.
*
* Never override the constructor, use `onInit` or `onAttachedToRootStore` instead.
*
* @template Data Data type.
* @template CreationData Creation data type.
* @template ModelIdPropertyName Model id property name.
*/
export declare abstract class BaseModel<TProps extends ModelProps, FromSnapshotOverride extends Record<string, any>, ToSnapshotOverride extends Record<string, any>, ModelIdPropertyName extends string = never> {
[propsTypeSymbol]: TProps;
[fromSnapshotOverrideTypeSymbol]: FromSnapshotOverride;
[toSnapshotOverrideTypeSymbol]: ToSnapshotOverride;
[modelIdPropertyNameSymbol]: ModelIdPropertyName;
/**
* Model type name.
*/
readonly [modelTypeKey]: string;
/**
* Model internal id. Can be modified inside a model action.
* It will return `undefined` if there's no id prop set.
*/
get [modelIdKey](): ModelIdPropertyType<TProps, ModelIdPropertyName>;
set [modelIdKey](newId: ModelIdPropertyType<TProps, ModelIdPropertyName>);
/**
* Can be overridden to offer a reference id to be used in reference resolution.
* By default it will use the `idProp` if available or return `undefined` otherwise.
*/
getRefId(): string | undefined;
/**
* Called after the model has been created.
*/
protected onInit?(): void;
/**
* Data part of the model, which is observable and will be serialized in snapshots.
* Use it if one of the data properties matches one of the model properties/functions.
*/
readonly $: ModelPropsToUntransformedData<TProps>;
/**
* Optional hook that will run once this model instance is attached to the tree of a model marked as
* root store via `registerRootStore`.
* Basically this is the place where you know the full root store is complete and where things such as
* middlewares, effects (reactions, etc), and other side effects should be registered, since it means
* that the model is now part of the active application state.
*
* It can return a disposer that will be run once this model instance is detached from such root store tree.
*
* @param rootStore
* @returns
*/
protected onAttachedToRootStore?(rootStore: object): (() => void) | void;
/**
* Performs a type check over the model instance.
* For this to work a data type has to be declared as part of the model properties.
*
* @returns A `TypeCheckError` or `null` if there is no error.
*/
typeCheck(): TypeCheckError | null;
/**
* Creates an instance of a model.
*/
constructor(data: ModelPropsToTransformedCreationData<TProps>);
toString(options?: {
withData?: boolean;
}): string;
}
/**
* @ignore
*/
export type BaseModelKeys = keyof AnyModel | "onInit" | "onAttachedToRootStore";
/**
* Any kind of model instance.
*/
export interface AnyModel extends BaseModel<any, any, any, any> {
}
/**
* @deprecated Should not be needed anymore.
*
* Tricks TypeScript into accepting abstract classes as a parameter for `ExtendedModel`.
* Does nothing in runtime.
*
* @template T Abstract model class type.
* @param type Abstract model class.
* @returns
*/
export declare function abstractModelClass<T>(type: T): T & Object;
/**
* The model id property name.
*/
export type ModelIdPropertyName<M extends AnyModel> = M[typeof modelIdPropertyNameSymbol];
/**
* Add missing model metadata to a model creation snapshot to generate a proper model snapshot.
* Usually used alongside `fromSnapshot`.
*
* @template M Model type.
* @param modelClass Model class.
* @param snapshot Model creation snapshot without metadata.
* @returns The model snapshot (including metadata).
*/
export declare function modelSnapshotInWithMetadata<M extends AnyModel>(modelClass: ModelClass<M>, snapshot: Omit<SnapshotInOfModel<M>, typeof modelTypeKey>): SnapshotInOfModel<M>;
/**
* Add missing model metadata to a model output snapshot to generate a proper model snapshot.
* Usually used alongside `applySnapshot`.
*
* @template M Model type.
* @param modelClass Model class.
* @param snapshot Model output snapshot without metadata.
* @returns The model snapshot (including metadata).
*/
export declare function modelSnapshotOutWithMetadata<M extends AnyModel>(modelClass: ModelClass<M>, snapshot: Omit<SnapshotOutOfModel<M>, typeof modelTypeKey>): SnapshotOutOfModel<M>;
/**
* A model class declaration, made of a base model and the model interface.
*/
export type ModelClassDeclaration<BaseModelClass, ModelInterface> = BaseModelClass & {
new (...args: any[]): ModelInterface;
};