Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx-keystone

Package Overview
Dependencies
Maintainers
1
Versions
195
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx-keystone - npm Package Compare versions

Comparing version 0.63.2 to 0.64.0

dist/types/arrayBased/array.d.ts

8

CHANGELOG.md
# Change Log
## 0.64.0
- [BREAKING CHANGE] `types.ref` now takes as an argument the reference constructor it needs to check for.
- When `tProp` is used instead of `prop` then `$modelType` won't be needed in input model snapshot for that particular property. As a side effect, `$modelType` is now marked as optional in types. Note that for this to work for a root model instead of a sub-property you will need to use the new `fromSnapshot` overload that takes a type as first argument. For untyped compatibility reasons, `getSnapshot` will still return `$modelType` even for typed models.
- Added `withSnapshotProcessor` to class model properties.
- Model classes can now be directly used as types instead of having to wrap them inside `types.model(...)` (except in recursive / self-recursive cases).
- Simplified some typings to make them faster.
## 0.63.2

@@ -4,0 +12,0 @@

26

dist/dataModel/BaseDataModel.d.ts

@@ -1,3 +0,4 @@

import { creationDataTypeSymbol, dataTypeSymbol, transformedCreationDataTypeSymbol, transformedDataTypeSymbol } from "../modelShared/BaseModelShared";
import type { TypeCheckError } from "../typeChecking/TypeCheckError";
import { propsTypeSymbol } from "../modelShared/BaseModelShared";
import { ModelProps, ModelPropsToCreationData, ModelPropsToData, ModelPropsToTransformedCreationData } from "../modelShared/prop";
import type { TypeCheckError } from "../types/TypeCheckError";
/**

@@ -10,15 +11,4 @@ * Base abstract class for data models. Use `DataModel` instead when extending.

*/
export declare abstract class BaseDataModel<Data extends {
[k: string]: any;
}, CreationData extends {
[k: string]: any;
}, TransformedData extends {
[k: string]: any;
}, TransformedCreationData extends {
[k: string]: any;
}> {
[dataTypeSymbol]: Data;
[creationDataTypeSymbol]: Data;
[transformedDataTypeSymbol]: TransformedData;
[transformedCreationDataTypeSymbol]: Data;
export declare abstract class BaseDataModel<TProps extends ModelProps> {
[propsTypeSymbol]: TProps;
/**

@@ -33,3 +23,3 @@ * Called after the instance is created when there's the first call to `fn(M, data)`.

*/
readonly $: Data;
readonly $: ModelPropsToData<TProps>;
/**

@@ -45,3 +35,3 @@ * Performs a type check over the model instance.

*/
constructor(data: CreationData | TransformedCreationData);
constructor(data: ModelPropsToCreationData<TProps> | ModelPropsToTransformedCreationData<TProps>);
toString(options?: {

@@ -58,3 +48,3 @@ withData?: boolean;

*/
export interface AnyDataModel extends BaseDataModel<any, any, any, any> {
export interface AnyDataModel extends BaseDataModel<any> {
}

@@ -61,0 +51,0 @@ /**

@@ -1,7 +0,7 @@

import type { AbstractModelClass } from "../modelShared/BaseModelShared";
import type { ModelProps, ModelPropsToCreationData, ModelPropsToData, ModelPropsToSetter, ModelPropsToTransformedCreationData, ModelPropsToTransformedData } from "../modelShared/prop";
import type { AbstractModelClass, ModelCreationData, ModelTransformedCreationData } from "../modelShared/BaseModelShared";
import type { ModelProps, ModelPropsToCreationData, ModelPropsToSetter, ModelPropsToTransformedCreationData, ModelPropsToTransformedData } from "../modelShared/prop";
import type { AnyDataModel, BaseDataModel, BaseDataModelKeys } from "./BaseDataModel";
export declare type _ComposedData<SuperModel, TProps extends ModelProps> = SuperModel extends BaseDataModel<any, infer CD, any, infer CTD> ? (ModelPropsToCreationData<TProps> & CD) | (ModelPropsToTransformedCreationData<TProps> & CTD) : ModelPropsToCreationData<TProps> | ModelPropsToTransformedCreationData<TProps>;
export declare type _ComposedData<SuperModel, TProps extends ModelProps> = SuperModel extends AnyDataModel ? (ModelPropsToCreationData<TProps> & ModelCreationData<SuperModel>) | (ModelPropsToTransformedCreationData<TProps> & ModelTransformedCreationData<SuperModel>) : ModelPropsToCreationData<TProps> | ModelPropsToTransformedCreationData<TProps>;
export interface _DataModel<SuperModel, TProps extends ModelProps> {
new (data: _ComposedData<SuperModel, TProps>): SuperModel & BaseDataModel<ModelPropsToData<TProps>, ModelPropsToCreationData<TProps>, ModelPropsToTransformedData<TProps>, ModelPropsToTransformedCreationData<TProps>> & Omit<ModelPropsToTransformedData<TProps>, BaseDataModelKeys> & ModelPropsToSetter<TProps>;
new (data: _ComposedData<SuperModel, TProps>): SuperModel & BaseDataModel<TProps> & Omit<ModelPropsToTransformedData<TProps>, BaseDataModelKeys> & ModelPropsToSetter<TProps>;
}

@@ -8,0 +8,0 @@ /**

import type { ModelClass } from "../modelShared/BaseModelShared";
import type { AnyType } from "../typeChecking/schemas";
import type { AnyType } from "../types/schemas";
import type { AnyDataModel } from "./BaseDataModel";

@@ -4,0 +4,0 @@ /**

@@ -19,5 +19,5 @@ export * from "./action";

export * from "./tweaker";
export * from "./typeChecking";
export * from "./types";
export * from "./utils";
export * from "./utils/tag";
export * from "./wrappers";
import type { O } from "ts-toolbelt";
import { creationDataTypeSymbol, dataTypeSymbol, fromSnapshotTypeSymbol, ModelClass, toSnapshotTypeSymbol, transformedCreationDataTypeSymbol, transformedDataTypeSymbol } from "../modelShared/BaseModelShared";
import { fromSnapshotOverrideTypeSymbol, ModelClass, propsTypeSymbol, toSnapshotOverrideTypeSymbol } from "../modelShared/BaseModelShared";
import type { ModelProps, ModelPropsToData, ModelPropsToTransformedCreationData } from "../modelShared/prop";
import type { SnapshotInOfModel, SnapshotOutOfModel } from "../snapshot/SnapshotOf";
import type { TypeCheckError } from "../typeChecking/TypeCheckError";
import type { TypeCheckError } from "../types/TypeCheckError";
import { modelIdKey, modelTypeKey } from "./metadata";

@@ -19,21 +20,10 @@ /**

*/
export declare abstract class BaseModel<Data extends {
export declare abstract class BaseModel<TProps extends ModelProps, FromSnapshotOverride extends {
[k: string]: any;
}, CreationData extends {
}, ToSnapshotOverride extends {
[k: string]: any;
}, TransformedData extends {
[k: string]: any;
}, TransformedCreationData extends {
[k: string]: any;
}, FromSnapshot extends {
[k: string]: any;
}, ToSnapshot extends {
[k: string]: any;
}, ModelIdPropertyName extends string = never> {
[dataTypeSymbol]: Data;
[creationDataTypeSymbol]: CreationData;
[transformedDataTypeSymbol]: TransformedData;
[transformedCreationDataTypeSymbol]: TransformedCreationData;
[fromSnapshotTypeSymbol]: FromSnapshot;
[toSnapshotTypeSymbol]: ToSnapshot;
[propsTypeSymbol]: TProps;
[fromSnapshotOverrideTypeSymbol]: FromSnapshotOverride;
[toSnapshotOverrideTypeSymbol]: ToSnapshotOverride;
[modelIdPropertyNameSymbol]: ModelIdPropertyName;

@@ -63,3 +53,3 @@ /**

*/
readonly $: Data;
readonly $: ModelPropsToData<TProps>;
/**

@@ -88,3 +78,3 @@ * Optional hook that will run once this model instance is attached to the tree of a model marked as

*/
constructor(data: TransformedCreationData);
constructor(data: ModelPropsToTransformedCreationData<TProps>);
toString(options?: {

@@ -101,3 +91,3 @@ withData?: boolean;

*/
export interface AnyModel extends BaseModel<any, any, any, any, any, any, any> {
export interface AnyModel extends BaseModel<any, any, any, any> {
}

@@ -104,0 +94,0 @@ /**

import type { ModelClass } from "../modelShared/BaseModelShared";
import type { AnyType } from "../typeChecking/schemas";
import type { AnyType } from "../types/schemas";
import type { AnyModel } from "./BaseModel";

@@ -4,0 +4,0 @@ /**

@@ -1,22 +0,16 @@

import type { AbstractModelClass } from "../modelShared/BaseModelShared";
import { ModelProps, ModelPropsToCreationData, ModelPropsToData, ModelPropsToSetter, ModelPropsToTransformedCreationData, ModelPropsToTransformedData } from "../modelShared/prop";
import type { SnapshotInOfObject, SnapshotOutOfObject } from "../snapshot/SnapshotOf";
import type { AbstractModelClass, ModelTransformedCreationData } from "../modelShared/BaseModelShared";
import type { ModelProps, ModelPropsToSetter, ModelPropsToSnapshotCreationData, ModelPropsToSnapshotData, ModelPropsToTransformedCreationData, ModelPropsToTransformedData } from "../modelShared/prop";
import type { AnyModel, BaseModel, BaseModelKeys, ModelIdPropertyName } from "./BaseModel";
import type { modelTypeKey } from "./metadata";
export declare type _ComposedCreationData<SuperModel, TProps extends ModelProps> = SuperModel extends BaseModel<any, any, any, infer TCD, any, any, any> ? ModelPropsToTransformedCreationData<TProps> & TCD : ModelPropsToTransformedCreationData<TProps>;
export declare type _ComposedCreationData<SuperModel, TProps extends ModelProps> = SuperModel extends AnyModel ? ModelPropsToTransformedCreationData<TProps> & ModelTransformedCreationData<SuperModel> : ModelPropsToTransformedCreationData<TProps>;
/**
* The default type used by fromSnapshot before processors are applied.
*/
export declare type FromSnapshotDefaultType<TProps extends ModelProps> = SnapshotInOfObject<ModelPropsToCreationData<TProps>>;
export declare type FromSnapshotDefaultType<TProps extends ModelProps> = ModelPropsToSnapshotCreationData<TProps>;
/**
* The default type used by getSnapshot before processors are applied.
*/
export declare type ToSnapshotDefaultType<TProps extends ModelProps> = SnapshotOutOfObject<ModelPropsToData<TProps>>;
export declare type ToSnapshotDefaultType<TProps extends ModelProps> = ModelPropsToSnapshotData<TProps>;
export declare type _ModelId<SuperModel, TProps extends ModelProps> = SuperModel extends AnyModel ? ModelIdPropertyName<SuperModel> : ExtractModelIdProp<TProps> & string;
export interface _Model<SuperModel, TProps extends ModelProps, FromSnapshot, ToSnapshot> {
new (data: _ComposedCreationData<SuperModel, TProps>): SuperModel & BaseModel<ModelPropsToData<TProps>, ModelPropsToCreationData<TProps>, ModelPropsToTransformedData<TProps>, ModelPropsToTransformedCreationData<TProps>, FromSnapshot & {
[modelTypeKey]: string;
}, ToSnapshot & {
[modelTypeKey]: string;
}, _ModelId<SuperModel, TProps>> & Omit<ModelPropsToTransformedData<TProps>, BaseModelKeys> & ModelPropsToSetter<TProps>;
export interface _Model<SuperModel, TProps extends ModelProps, FromSnapshotOverride, ToSnapshotOverride> {
new (data: _ComposedCreationData<SuperModel, TProps>): SuperModel & BaseModel<TProps, FromSnapshotOverride, ToSnapshotOverride, _ModelId<SuperModel, TProps>> & Omit<ModelPropsToTransformedData<TProps>, BaseModelKeys> & ModelPropsToSetter<TProps>;
}

@@ -27,3 +21,3 @@ /**

export declare type ExtractModelIdProp<TProps extends ModelProps> = {
[K in keyof TProps]: TProps[K]["$isId"] extends true ? K : never;
[K in keyof TProps]: TProps[K]["_internal"]["$isId"] extends true ? K : never;
}[keyof TProps];

@@ -39,3 +33,3 @@ /**

*/
export declare function ExtendedModel<TProps extends ModelProps, TModel extends AnyModel, A extends [], FS = FromSnapshotDefaultType<TProps>, TS = ToSnapshotDefaultType<TProps>>(genFn: (...args: A) => {
export declare function ExtendedModel<TProps extends ModelProps, TModel extends AnyModel, A extends [], FS = never, TS = never>(genFn: (...args: A) => {
baseModel: AbstractModelClass<TModel>;

@@ -54,3 +48,3 @@ props: TProps;

*/
export declare function ExtendedModel<TProps extends ModelProps, TModel extends AnyModel, FS = FromSnapshotDefaultType<TProps>, TS = ToSnapshotDefaultType<TProps>>(baseModel: AbstractModelClass<TModel>, modelProps: TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<TModel, TProps, FS, TS>;
export declare function ExtendedModel<TProps extends ModelProps, TModel extends AnyModel, FS = never, TS = never>(baseModel: AbstractModelClass<TModel>, modelProps: TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<TModel, TProps, FS, TS>;
/**

@@ -65,3 +59,3 @@ * Base abstract class for models.

*/
export declare function Model<TProps extends ModelProps, A extends [], FS = FromSnapshotDefaultType<TProps>, TS = ToSnapshotDefaultType<TProps>>(fnModelProps: (...args: A) => TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<unknown, TProps, FS, TS>;
export declare function Model<TProps extends ModelProps, A extends [], FS = never, TS = never>(fnModelProps: (...args: A) => TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<unknown, TProps, FS, TS>;
/**

@@ -76,3 +70,3 @@ * Base abstract class for models.

*/
export declare function Model<TProps extends ModelProps, FS = FromSnapshotDefaultType<TProps>, TS = ToSnapshotDefaultType<TProps>>(modelProps: TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<unknown, TProps, FS, TS>;
export declare function Model<TProps extends ModelProps, FS = never, TS = never>(modelProps: TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<unknown, TProps, FS, TS>;
/**

@@ -79,0 +73,0 @@ * Model options.

import type { AnyDataModel } from "../dataModel/BaseDataModel";
import type { AnyModel } from "../model/BaseModel";
import type { modelTypeKey } from "../model/metadata";
import type { Flatten, IsNeverType } from "../utils/types";
import type { ModelPropsToCreationData, ModelPropsToSnapshotCreationData, ModelPropsToSnapshotData, ModelPropsToTransformedCreationData, ModelPropsToTransformedData } from "./prop";
/**
* @ignore
*/
export declare const dataTypeSymbol: unique symbol;
export declare const propsTypeSymbol: unique symbol;
/**
* @ignore
*/
export declare const creationDataTypeSymbol: unique symbol;
export declare const fromSnapshotOverrideTypeSymbol: unique symbol;
/**
* @ignore
*/
export declare const transformedDataTypeSymbol: unique symbol;
export declare const toSnapshotOverrideTypeSymbol: unique symbol;
/**
* @ignore
*/
export declare const transformedCreationDataTypeSymbol: unique symbol;
/**
* @ignore
*/
export declare const fromSnapshotTypeSymbol: unique symbol;
/**
* @ignore
*/
export declare const toSnapshotTypeSymbol: unique symbol;
/**
* Extracts the instance type of a model class.

@@ -40,17 +31,21 @@ */

/**
* The props of a model.
*/
export declare type ModelPropsOf<M extends AnyModel | AnyDataModel> = M[typeof propsTypeSymbol];
/**
* The data type of a model.
*/
export declare type ModelData<M extends AnyModel | AnyDataModel> = M["$"];
export declare type ModelData<M extends AnyModel | AnyDataModel> = Flatten<M["$"]>;
/**
* The creation data type of a model.
*/
export declare type ModelCreationData<M extends AnyModel | AnyDataModel> = M[typeof creationDataTypeSymbol];
export declare type ModelCreationData<M extends AnyModel | AnyDataModel> = ModelPropsToCreationData<ModelPropsOf<M>>;
/**
* The transformed data type of a model.
*/
export declare type ModelTransformedData<M extends AnyModel | AnyDataModel> = M[typeof transformedDataTypeSymbol];
export declare type ModelTransformedData<M extends AnyModel | AnyDataModel> = ModelPropsToTransformedData<ModelPropsOf<M>>;
/**
* The transformed creation data type of a model.
*/
export declare type ModelTransformedCreationData<M extends AnyModel | AnyDataModel> = M[typeof transformedCreationDataTypeSymbol];
export declare type ModelTransformedCreationData<M extends AnyModel | AnyDataModel> = ModelPropsToTransformedCreationData<ModelPropsOf<M>>;
/**

@@ -60,3 +55,5 @@ * The from snapshot type of a model.

*/
export declare type ModelFromSnapshot<M extends AnyModel> = M[typeof fromSnapshotTypeSymbol];
export declare type ModelFromSnapshot<M extends AnyModel> = IsNeverType<M[typeof fromSnapshotOverrideTypeSymbol], ModelPropsToSnapshotCreationData<ModelPropsOf<M>>, M[typeof fromSnapshotOverrideTypeSymbol]> & {
[modelTypeKey]?: string;
};
/**

@@ -66,3 +63,5 @@ * The to snapshot type of a model.

*/
export declare type ModelToSnapshot<M extends AnyModel> = M[typeof toSnapshotTypeSymbol];
export declare type ModelToSnapshot<M extends AnyModel> = IsNeverType<M[typeof toSnapshotOverrideTypeSymbol], ModelPropsToSnapshotData<ModelPropsOf<M>>, M[typeof toSnapshotOverrideTypeSymbol]> & {
[modelTypeKey]?: string;
};
/**

@@ -69,0 +68,0 @@ * Tricks Typescript into accepting a particular kind of generic class as a parameter for `ExtendedModel`.

import { AnyDataModel } from "../dataModel/BaseDataModel";
import { AnyModel } from "../model/BaseModel";
import { ModelClass } from "./BaseModelShared";
import { AnyModelProp, ModelProps } from "./prop";
export declare function createModelPropDescriptor(modelPropName: string, modelProp: AnyModelProp | undefined, enumerable: boolean): PropertyDescriptor;
import { ModelProps } from "./prop";
declare type FromSnapshotProcessorFn = (sn: any) => any;
declare type ToSnapshotProcessorFn = (sn: any, instance: any) => any;
export declare function sharedInternalModel<TProps extends ModelProps, TBaseModel extends AnyModel | AnyDataModel>({ modelProps, baseModel, type, valueType, fromSnapshotProcessor, toSnapshotProcessor, }: {

@@ -11,4 +12,5 @@ modelProps: TProps;

valueType: boolean;
fromSnapshotProcessor: ((sn: any) => any) | undefined;
toSnapshotProcessor: ((sn: any, instance: any) => any) | undefined;
fromSnapshotProcessor: FromSnapshotProcessorFn | undefined;
toSnapshotProcessor: ToSnapshotProcessorFn | undefined;
}): any;
export {};

@@ -22,4 +22,4 @@ import type { AnyDataModel } from "../dataModel/BaseDataModel";

*/
export declare function decoratedModel<MC extends ModelClass<AnyModel>>(name: string | undefined, clazz: MC, decorators: {
[k in keyof InstanceType<MC>]?: ((...args: any[]) => any) | ReadonlyArray<(...args: any[]) => any>;
export declare function decoratedModel<M, MC extends abstract new (...ags: any) => M>(name: string | undefined, clazz: MC, decorators: {
[k in keyof M]?: ((...args: any[]) => any) | ReadonlyArray<(...args: any[]) => any>;
}): MC;
import type { O } from "ts-toolbelt";
import type { LateTypeChecker, TypeChecker } from "../typeChecking/TypeChecker";
import type { IsOptionalValue } from "../utils/types";
import type { SnapshotInOf, SnapshotOutOf } from "../snapshot/SnapshotOf";
import type { LateTypeChecker, TypeChecker } from "../types/TypeChecker";
import type { Flatten, IsNeverType, IsOptionalValue } from "../utils/types";
/**

@@ -11,24 +12,30 @@ * @ignore

*/
export interface ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId extends boolean = false, THasSetter = never> {
$valueType: TPropValue;
$creationValueType: TPropCreationValue;
$transformedValueType: TTransformedValue;
$transformedCreationValueType: TTransformedCreationValue;
$isOptional: TIsOptional;
$isId: TIsId;
$hasSetter: THasSetter;
defaultFn: (() => TPropValue) | typeof noDefaultValue;
defaultValue: TPropValue | typeof noDefaultValue;
typeChecker: TypeChecker | LateTypeChecker | undefined;
setter: boolean | "assign";
isId: boolean;
transform: {
transform(original: unknown, model: object, propName: PropertyKey, setOriginalValue: (newOriginalValue: unknown) => void): unknown;
untransform(transformed: unknown, model: object, propName: PropertyKey): unknown;
} | undefined;
withSetter(): ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId, string>;
export interface ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId extends boolean = false, THasSetter = never, TFromSnapshotOverride = never, TToSnapshotOverride = never> {
_internal: {
$valueType: TPropValue;
$creationValueType: TPropCreationValue;
$transformedValueType: TTransformedValue;
$transformedCreationValueType: TTransformedCreationValue;
$isOptional: TIsOptional;
$isId: TIsId;
$hasSetter: THasSetter;
$fromSnapshotOverride: TFromSnapshotOverride;
$toSnapshotOverride: TToSnapshotOverride;
defaultFn: (() => TPropValue) | typeof noDefaultValue;
defaultValue: TPropValue | typeof noDefaultValue;
typeChecker: TypeChecker | LateTypeChecker | undefined;
setter: boolean | "assign";
isId: boolean;
transform: {
transform(original: unknown, model: object, propName: PropertyKey, setOriginalValue: (newOriginalValue: unknown) => void): unknown;
untransform(transformed: unknown, model: object, propName: PropertyKey): unknown;
} | undefined;
fromSnapshotProcessor?(sn: unknown): unknown;
toSnapshotProcessor?(sn: unknown): unknown;
};
withSetter(): ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId, string, TFromSnapshotOverride, TToSnapshotOverride>;
/**
* @deprecated Setter methods are preferred.
*/
withSetter(mode: "assign"): ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId, string>;
withSetter(mode: "assign"): ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId, string, TFromSnapshotOverride, TToSnapshotOverride>;
/**

@@ -41,5 +48,17 @@ * Sets a transform for the property instance value.

*/
withTransform<TTV>(transform: ModelPropTransform<NonNullable<TPropValue>, TTV>): ModelProp<TPropValue, TPropCreationValue, TTV | Extract<TPropValue, null | undefined>, TTV | Extract<TPropCreationValue, null | undefined>, TIsOptional, TIsId, THasSetter>;
withTransform<TTV>(transform: ModelPropTransform<NonNullable<TPropValue>, TTV>): ModelProp<TPropValue, TPropCreationValue, TTV | Extract<TPropValue, null | undefined>, TTV | Extract<TPropCreationValue, null | undefined>, TIsOptional, TIsId, THasSetter, TFromSnapshotOverride, TToSnapshotOverride>;
withSnapshotProcessor<FS = TFromSnapshotOverride, TS = TToSnapshotOverride, This extends AnyModelProp = this>(processor: {
fromSnapshot?(sn: FS): ModelPropFromSnapshot<This>;
toSnapshot?(sn: ModelPropToSnapshot<This>): TS;
}): ModelProp<TPropValue, TPropCreationValue, TTransformedValue, TTransformedCreationValue, TIsOptional, TIsId, THasSetter, FS, TS>;
}
/**
* The snapshot in type of a model property.
*/
export declare type ModelPropFromSnapshot<MP extends AnyModelProp> = IsNeverType<MP["_internal"]["$fromSnapshotOverride"], SnapshotInOf<MP["_internal"]["$creationValueType"]>, MP["_internal"]["$fromSnapshotOverride"]>;
/**
* The snapshot out type of a model property.
*/
export declare type ModelPropToSnapshot<MP extends AnyModelProp> = IsNeverType<MP["_internal"]["$toSnapshotOverride"], SnapshotOutOf<MP["_internal"]["$valueType"]>, MP["_internal"]["$toSnapshotOverride"]>;
/**
* A model prop transform.

@@ -61,3 +80,3 @@ */

*/
export declare type AnyModelProp = ModelProp<any, any, any, any, any, any, any>;
export declare type AnyModelProp = ModelProp<any, any, any, any, any, any, any, any, any>;
/**

@@ -70,24 +89,34 @@ * Model properties.

export declare type OptionalModelProps<MP extends ModelProps> = {
[K in keyof MP]: MP[K]["$isOptional"] & K;
[K in keyof MP]: MP[K]["_internal"]["$isOptional"] & K;
}[keyof MP];
export declare type ModelPropsToData<MP extends ModelProps> = {
[k in keyof MP]: MP[k]["$valueType"];
};
export declare type ModelPropsToCreationData<MP extends ModelProps> = {
[k in keyof MP]?: MP[k]["$creationValueType"];
export declare type ModelPropsToData<MP extends ModelProps> = Flatten<{
[k in keyof MP]: MP[k]["_internal"]["$valueType"];
}>;
export declare type ModelPropsToSnapshotData<MP extends ModelProps> = Flatten<{
[k in keyof MP]: ModelPropToSnapshot<MP[k]> extends infer R ? R : never;
}>;
export declare type ModelPropsToCreationData<MP extends ModelProps> = Flatten<{
[k in keyof MP]?: MP[k]["_internal"]["$creationValueType"];
} & O.Omit<{
[k in keyof MP]: MP[k]["$creationValueType"];
}, OptionalModelProps<MP>>;
export declare type ModelPropsToTransformedData<MP extends ModelProps> = {
[k in keyof MP]: MP[k]["$transformedValueType"];
};
export declare type ModelPropsToTransformedCreationData<MP extends ModelProps> = {
[k in keyof MP]?: MP[k]["$transformedCreationValueType"];
[k in keyof MP]: MP[k]["_internal"]["$creationValueType"];
}, OptionalModelProps<MP>>>;
export declare type ModelPropsToSnapshotCreationData<MP extends ModelProps> = Flatten<{
[k in keyof MP]?: ModelPropFromSnapshot<MP[k]> extends infer R ? R : never;
} & O.Omit<{
[k in keyof MP]: MP[k]["$transformedCreationValueType"];
}, OptionalModelProps<MP>>;
export declare type ModelPropsToSetter<MP extends ModelProps> = {
[k in keyof MP as MP[k]["$hasSetter"] & `set${Capitalize<k & string>}`]: (value: MP[k]["$transformedValueType"]) => void;
};
export declare type ModelIdProp = ModelProp<string, string, string, string, string, true>;
[k in keyof MP]: ModelPropFromSnapshot<MP[k]> extends infer R ? R : never;
}, {
[K in keyof MP]: IsNeverType<MP[K]["_internal"]["$fromSnapshotOverride"], MP[K]["_internal"]["$isOptional"] & K, IsOptionalValue<MP[K]["_internal"]["$fromSnapshotOverride"], K, never>>;
}[keyof MP]>>;
export declare type ModelPropsToTransformedData<MP extends ModelProps> = Flatten<{
[k in keyof MP]: MP[k]["_internal"]["$transformedValueType"];
}>;
export declare type ModelPropsToTransformedCreationData<MP extends ModelProps> = Flatten<{
[k in keyof MP]?: MP[k]["_internal"]["$transformedCreationValueType"];
} & O.Omit<{
[k in keyof MP]: MP[k]["_internal"]["$transformedCreationValueType"];
}, OptionalModelProps<MP>>>;
export declare type ModelPropsToSetter<MP extends ModelProps> = Flatten<{
[k in keyof MP as MP[k]["_internal"]["$hasSetter"] & `set${Capitalize<k & string>}`]: (value: MP[k]["_internal"]["$transformedValueType"]) => void;
}>;
export declare type ModelIdProp = ModelProp<string, string | undefined, string, string | undefined, string, true>;
/**

@@ -94,0 +123,0 @@ * A property that will be used as model id, accessible through $modelId.

@@ -1,2 +0,1 @@

import { IAtom } from "mobx";
import type { ParentPath } from "./path";

@@ -10,6 +9,2 @@ /**

*/
export declare const objectParentsAtoms: WeakMap<object, IAtom>;
/**
* @ignore
*/
export declare function parentPathEquals(parentPath1: ParentPath<any> | undefined, parentPath2: ParentPath<any> | undefined, comparePath?: boolean): boolean;

@@ -16,0 +11,0 @@ /**

@@ -1,2 +0,2 @@

import type { SnapshotInOf } from "./SnapshotOf";
import type { SnapshotInOf, SnapshotOutOf } from "./SnapshotOf";
/**

@@ -10,1 +10,9 @@ * Applies a full snapshot over an object, reconciling it with the current contents of the object.

export declare function applySnapshot<T extends object>(node: T, snapshot: SnapshotInOf<T>): void;
/**
* Applies a full snapshot over an object, reconciling it with the current contents of the object.
*
* @typeparam T Object type.
* @param node Target object (model object, object or array).
* @param snapshot Snapshot to apply.
*/
export declare function applySnapshot<T extends object>(node: T, snapshot: SnapshotOutOf<T>): void;

@@ -0,1 +1,5 @@

import type { AnyDataModel } from "../dataModel/BaseDataModel";
import type { AnyModel } from "../model/BaseModel";
import type { ModelClass } from "../modelShared/BaseModelShared";
import type { AnyStandardType, TypeToData } from "../types/schemas";
import type { SnapshotInOf, SnapshotOutOf } from "./SnapshotOf";

@@ -12,12 +16,22 @@ /**

/**
* Deserializers a data structure from its snapshot form.
* Given a type deserializes a data structure from its snapshot form.
*
* @typeparam TType Object type.
* @param type Type.
* @param snapshot Snapshot, even if a primitive.
* @param options Options.
* @returns The deserialized object.
*/
export declare function fromSnapshot<TType extends AnyStandardType | ModelClass<AnyModel> | ModelClass<AnyDataModel>>(type: TType, snapshot: SnapshotInOf<TypeToData<TType>>, options?: Partial<FromSnapshotOptions>): TypeToData<TType>;
/**
* Deserializes a data structure from its snapshot form.
*
* @typeparam T Object type.
* @param snapshot Snapshot, even if a primitive.
* @param [options] Options.
* @param options Options.
* @returns The deserialized object.
*/
export declare let fromSnapshot: <T>(snapshot: SnapshotInOf<T> | SnapshotOutOf<T>, options?: Partial<FromSnapshotOptions> | undefined) => T;
export declare function fromSnapshot<T>(snapshot: SnapshotInOf<T> | SnapshotOutOf<T>, options?: Partial<FromSnapshotOptions>): T;
export declare const observableOptions: {
deep: boolean;
};

@@ -0,1 +1,2 @@

import { AnyType, TypeToData } from "../types/schemas";
import type { SnapshotOutOf } from "./SnapshotOf";

@@ -11,2 +12,12 @@ /**

*/
export declare function getSnapshot<T extends AnyType>(type: T, nodeOrPrimitive: TypeToData<T>): SnapshotOutOf<TypeToData<T>>;
/**
* Retrieves an immutable snapshot for a data structure.
* Since returned snapshots are immutable they will respect shallow equality, this is,
* if no changes are made then the snapshot will be kept the same.
*
* @typeparam T Object type.
* @param nodeOrPrimitive Data structure, including primtives.
* @returns The snapshot.
*/
export declare function getSnapshot<T>(nodeOrPrimitive: T): SnapshotOutOf<T>;

@@ -18,3 +18,3 @@ import type { Frozen, frozenKey } from "../frozen/Frozen";

};
[modelTypeKey]: string;
[modelTypeKey]?: string;
[modelIdKey]: string;

@@ -24,3 +24,3 @@ }

items: SnapshotOutOf<V>[];
[modelTypeKey]: string;
[modelTypeKey]?: string;
[modelIdKey]: string;

@@ -41,3 +41,3 @@ }

};
[modelTypeKey]: string;
[modelTypeKey]?: string;
[modelIdKey]: string;

@@ -47,5 +47,5 @@ }

items?: SnapshotOutOf<V>[];
[modelTypeKey]: string;
[modelTypeKey]?: string;
[modelIdKey]: string;
}
export declare type SnapshotInOf<T> = T extends ObjectMap<infer V> ? SnapshotInOfObjectMap<V> extends infer R ? R : never : T extends ArraySet<infer V> ? SnapshotInOfArraySet<V> extends infer R ? R : never : T extends AnyModel ? SnapshotInOfModel<T> extends infer R ? R : never : T extends Frozen<any> ? SnapshotInOfFrozen<T> extends infer R ? R : never : T extends object ? SnapshotInOfObject<T> extends infer R ? R : never : T;

@@ -1,2 +0,2 @@

import type { AnyStandardType, TypeToData } from "../typeChecking/schemas";
import type { AnyStandardType, TypeToData } from "../types/schemas";
/**

@@ -3,0 +3,0 @@ * Turns an object (array, plain object) into a tree node,

@@ -24,1 +24,11 @@ /**

export declare type IsOptionalValue<C, TV, FV> = undefined extends C ? TV : FV;
/**
* @ignore
*/
export declare type IsNeverType<T, IfNever, IfNotNever> = [T] extends [never] ? IfNever : IfNotNever;
/**
* @ignore
*/
export declare type Flatten<T> = T extends Record<any, any> ? {
[P in keyof T]: T[P];
} : T;
{
"name": "mobx-keystone",
"version": "0.63.2",
"version": "0.64.0",
"description": "A MobX powered state management solution based on data trees with first class support for Typescript, snapshots, patches and much more",

@@ -70,18 +70,18 @@ "keywords": [

"@types/jest": "^27.0.2",
"@types/node": "^16.10.3",
"jest": "^27.2.4",
"mobx": "^6.3.3",
"netlify-cli": "^6.10.3",
"@types/node": "^16.11.2",
"jest": "^27.3.1",
"mobx": "^6.3.5",
"netlify-cli": "^6.14.0",
"rollup-plugin-typescript2": "^0.30.0",
"shx": "^0.3.3",
"spec.ts": "^1.1.3",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typedoc": "^0.22.5",
"typescript": "^4.4.3",
"vite": "^2.6.4"
"ts-jest": "^27.0.7",
"ts-node": "^10.3.1",
"typedoc": "^0.22.6",
"typescript": "^4.4.4",
"vite": "^2.6.10"
},
"dependencies": {
"fast-deep-equal": "^3.1.3",
"nanoid": "^3.1.29",
"nanoid": "^3.1.30",
"ts-toolbelt": "^9.6.0",

@@ -88,0 +88,0 @@ "tslib": "^2.3.1"

import { failure } from "../utils"
import { getCurrentActionContext } from "./context"
/**
* @ignore
* @internal
*/
export function canWrite(): boolean {
function canWrite(): boolean {
return !getActionProtection() || !!getCurrentActionContext()

@@ -10,0 +6,0 @@ }

@@ -10,5 +10,5 @@ import { action, computed } from "mobx"

import { assertTweakedObject } from "../tweaker/core"
import { typesArray } from "../typeChecking/array"
import { tProp } from "../typeChecking/tProp"
import { typesUnchecked } from "../typeChecking/unchecked"
import { typesArray } from "../types/arrayBased/array"
import { tProp } from "../types/tProp"
import { typesUnchecked } from "../types/utility/unchecked"
import { failure, getMobxVersion, mobx6 } from "../utils"

@@ -15,0 +15,0 @@ import { actionTrackingMiddleware, SimpleActionContext } from "./actionTrackingMiddleware"

@@ -1,17 +0,17 @@

import {
creationDataTypeSymbol,
dataTypeSymbol,
ModelClass,
transformedCreationDataTypeSymbol,
transformedDataTypeSymbol,
} from "../modelShared/BaseModelShared"
import { ModelClass, propsTypeSymbol } from "../modelShared/BaseModelShared"
import { modelInfoByClass } from "../modelShared/modelInfo"
import { getInternalModelClassPropsInfo } from "../modelShared/modelPropsInfo"
import { noDefaultValue } from "../modelShared/prop"
import {
ModelProps,
ModelPropsToCreationData,
ModelPropsToData,
ModelPropsToTransformedCreationData,
noDefaultValue,
} from "../modelShared/prop"
import { getSnapshot } from "../snapshot/getSnapshot"
import { isTreeNode } from "../tweaker/core"
import { toTreeNode } from "../tweaker/tweak"
import { typesDataModelData } from "../typeChecking/dataModelData"
import { typeCheck } from "../typeChecking/typeCheck"
import type { TypeCheckError } from "../typeChecking/TypeCheckError"
import { typesDataModelData } from "../types/objectBased/dataModelData"
import { typeCheck } from "../types/typeCheck"
import type { TypeCheckError } from "../types/TypeCheckError"
import { failure, isObject } from "../utils"

@@ -32,13 +32,5 @@ import { getOrCreate } from "../utils/mapUtils"

*/
export abstract class BaseDataModel<
Data extends { [k: string]: any },
CreationData extends { [k: string]: any },
TransformedData extends { [k: string]: any },
TransformedCreationData extends { [k: string]: any }
> {
export abstract class BaseDataModel<TProps extends ModelProps> {
// just to make typing work properly
[dataTypeSymbol]: Data;
[creationDataTypeSymbol]: Data;
[transformedDataTypeSymbol]: TransformedData;
[transformedCreationDataTypeSymbol]: Data
[propsTypeSymbol]: TProps

@@ -55,3 +47,3 @@ /**

*/
readonly $!: Data
readonly $!: ModelPropsToData<TProps>

@@ -72,3 +64,5 @@ /**

*/
constructor(data: CreationData | TransformedCreationData) {
constructor(
data: ModelPropsToCreationData<TProps> | ModelPropsToTransformedCreationData<TProps>
) {
if (!isObject(data)) {

@@ -81,2 +75,3 @@ throw failure("data models can only work over data objects")

type Data = ModelPropsToData<TProps>
let tweakedData: Data

@@ -107,5 +102,5 @@ if (isTreeNode(data)) {

// apply untransform (if any)
if (propData.transform) {
if (propData._internal.transform) {
changed = true
newValue = propData.transform.untransform(newValue, this, k)
newValue = propData._internal.transform.untransform(newValue, this, k)
}

@@ -115,8 +110,8 @@

if (newValue == null) {
if (propData.defaultFn !== noDefaultValue) {
if (propData._internal.defaultFn !== noDefaultValue) {
changed = true
newValue = propData.defaultFn()
} else if (propData.defaultValue !== noDefaultValue) {
newValue = propData._internal.defaultFn()
} else if (propData._internal.defaultValue !== noDefaultValue) {
changed = true
newValue = propData.defaultValue
newValue = propData._internal.defaultValue
}

@@ -151,6 +146,3 @@ }

// delete unnecessary props
delete self[dataTypeSymbol]
delete self[creationDataTypeSymbol]
delete self[transformedDataTypeSymbol]
delete self[transformedCreationDataTypeSymbol]
delete self[propsTypeSymbol]

@@ -194,3 +186,3 @@ internalNewDataModel(this, tweakedData as any, {

*/
export interface AnyDataModel extends BaseDataModel<any, any, any, any> {}
export interface AnyDataModel extends BaseDataModel<any> {}

@@ -197,0 +189,0 @@ /**

@@ -1,2 +0,7 @@

import type { AbstractModelClass, ModelClass } from "../modelShared/BaseModelShared"
import type {
AbstractModelClass,
ModelClass,
ModelCreationData,
ModelTransformedCreationData,
} from "../modelShared/BaseModelShared"
import { sharedInternalModel } from "../modelShared/Model"

@@ -6,3 +11,2 @@ import type {

ModelPropsToCreationData,
ModelPropsToData,
ModelPropsToSetter,

@@ -15,9 +19,6 @@ ModelPropsToTransformedCreationData,

export type _ComposedData<SuperModel, TProps extends ModelProps> = SuperModel extends BaseDataModel<
any,
infer CD,
any,
infer CTD
>
? (ModelPropsToCreationData<TProps> & CD) | (ModelPropsToTransformedCreationData<TProps> & CTD)
export type _ComposedData<SuperModel, TProps extends ModelProps> = SuperModel extends AnyDataModel
?
| (ModelPropsToCreationData<TProps> & ModelCreationData<SuperModel>)
| (ModelPropsToTransformedCreationData<TProps> & ModelTransformedCreationData<SuperModel>)
: ModelPropsToCreationData<TProps> | ModelPropsToTransformedCreationData<TProps>

@@ -27,8 +28,3 @@

new (data: _ComposedData<SuperModel, TProps>): SuperModel &
BaseDataModel<
ModelPropsToData<TProps>,
ModelPropsToCreationData<TProps>,
ModelPropsToTransformedData<TProps>,
ModelPropsToTransformedCreationData<TProps>
> &
BaseDataModel<TProps> &
Omit<ModelPropsToTransformedData<TProps>, BaseDataModelKeys> &

@@ -35,0 +31,0 @@ ModelPropsToSetter<TProps>

import type { ModelClass } from "../modelShared/BaseModelShared"
import { modelMetadataSymbol } from "../modelShared/modelSymbols"
import type { AnyType } from "../typeChecking/schemas"
import type { AnyType } from "../types/schemas"
import { failure } from "../utils"

@@ -5,0 +5,0 @@ import type { AnyDataModel } from "./BaseDataModel"

@@ -19,5 +19,5 @@ export * from "./action"

export * from "./tweaker"
export * from "./typeChecking"
export * from "./types"
export * from "./utils"
export * from "./utils/tag"
export * from "./wrappers"
import { observable } from "mobx"
import type { O } from "ts-toolbelt"
import {
creationDataTypeSymbol,
dataTypeSymbol,
fromSnapshotTypeSymbol,
fromSnapshotOverrideTypeSymbol,
ModelClass,
toSnapshotTypeSymbol,
transformedCreationDataTypeSymbol,
transformedDataTypeSymbol,
propsTypeSymbol,
toSnapshotOverrideTypeSymbol,
} from "../modelShared/BaseModelShared"
import { modelInfoByClass } from "../modelShared/modelInfo"
import type {
ModelProps,
ModelPropsToData,
ModelPropsToTransformedCreationData,
} from "../modelShared/prop"
import { getSnapshot } from "../snapshot/getSnapshot"
import type { SnapshotInOfModel, SnapshotOutOfModel } from "../snapshot/SnapshotOf"
import { typesModel } from "../typeChecking/model"
import { typeCheck } from "../typeChecking/typeCheck"
import type { TypeCheckError } from "../typeChecking/TypeCheckError"
import { typesModel } from "../types/objectBased/model"
import { typeCheck } from "../types/typeCheck"
import type { TypeCheckError } from "../types/TypeCheckError"
import { assertIsObject, failure } from "../utils"

@@ -41,17 +43,11 @@ import { getModelIdPropertyName } from "./getModelMetadata"

export abstract class BaseModel<
Data extends { [k: string]: any },
CreationData extends { [k: string]: any },
TransformedData extends { [k: string]: any },
TransformedCreationData extends { [k: string]: any },
FromSnapshot extends { [k: string]: any },
ToSnapshot extends { [k: string]: any },
TProps extends ModelProps,
FromSnapshotOverride extends { [k: string]: any },
ToSnapshotOverride extends { [k: string]: any },
ModelIdPropertyName extends string = never
> {
// just to make typing work properly
[dataTypeSymbol]: Data;
[creationDataTypeSymbol]: CreationData;
[transformedDataTypeSymbol]: TransformedData;
[transformedCreationDataTypeSymbol]: TransformedCreationData;
[fromSnapshotTypeSymbol]: FromSnapshot;
[toSnapshotTypeSymbol]: ToSnapshot;
[propsTypeSymbol]: TProps;
[fromSnapshotOverrideTypeSymbol]: FromSnapshotOverride;
[toSnapshotOverrideTypeSymbol]: ToSnapshotOverride;
[modelIdPropertyNameSymbol]: ModelIdPropertyName;

@@ -70,3 +66,3 @@

const idProp = getModelIdPropertyName(this.constructor as any)
return idProp ? this.$[idProp] : undefined
return idProp ? this.$[idProp] : (undefined as any)
}

@@ -99,3 +95,3 @@

*/
readonly $!: Data
readonly $!: ModelPropsToData<TProps>

@@ -130,3 +126,3 @@ /**

*/
constructor(data: TransformedCreationData) {
constructor(data: ModelPropsToTransformedCreationData<TProps>) {
let initialData = data as any

@@ -141,8 +137,5 @@ const { snapshotInitialData, modelClass, generateNewIds }: ModelConstructorOptions =

// delete unnecessary props
delete self[dataTypeSymbol]
delete self[creationDataTypeSymbol]
delete self[transformedDataTypeSymbol]
delete self[transformedCreationDataTypeSymbol]
delete self[fromSnapshotTypeSymbol]
delete self[toSnapshotTypeSymbol]
delete self[propsTypeSymbol]
delete self[fromSnapshotOverrideTypeSymbol]
delete self[toSnapshotOverrideTypeSymbol]
delete self[modelIdPropertyNameSymbol]

@@ -202,3 +195,3 @@

*/
export interface AnyModel extends BaseModel<any, any, any, any, any, any, any> {}
export interface AnyModel extends BaseModel<any, any, any, any> {}

@@ -205,0 +198,0 @@ /**

import type { ModelClass } from "../modelShared/BaseModelShared"
import { modelMetadataSymbol } from "../modelShared/modelSymbols"
import type { AnyType } from "../typeChecking/schemas"
import type { AnyType } from "../types/schemas"
import { failure } from "../utils"

@@ -5,0 +5,0 @@ import { getOrCreate } from "../utils/mapUtils"

@@ -1,14 +0,16 @@

import type { AbstractModelClass, ModelClass } from "../modelShared/BaseModelShared"
import type {
AbstractModelClass,
ModelClass,
ModelTransformedCreationData,
} from "../modelShared/BaseModelShared"
import { sharedInternalModel } from "../modelShared/Model"
import {
import type {
ModelProps,
ModelPropsToCreationData,
ModelPropsToData,
ModelPropsToSetter,
ModelPropsToSnapshotCreationData,
ModelPropsToSnapshotData,
ModelPropsToTransformedCreationData,
ModelPropsToTransformedData,
} from "../modelShared/prop"
import type { SnapshotInOfObject, SnapshotOutOfObject } from "../snapshot/SnapshotOf"
import type { AnyModel, BaseModel, BaseModelKeys, ModelIdPropertyName } from "./BaseModel"
import type { modelTypeKey } from "./metadata"
import { assertIsModelClass, isModelClass } from "./utils"

@@ -19,4 +21,4 @@

TProps extends ModelProps
> = SuperModel extends BaseModel<any, any, any, infer TCD, any, any, any>
? ModelPropsToTransformedCreationData<TProps> & TCD
> = SuperModel extends AnyModel
? ModelPropsToTransformedCreationData<TProps> & ModelTransformedCreationData<SuperModel>
: ModelPropsToTransformedCreationData<TProps>

@@ -27,5 +29,4 @@

*/
export type FromSnapshotDefaultType<TProps extends ModelProps> = SnapshotInOfObject<
ModelPropsToCreationData<TProps>
>
export type FromSnapshotDefaultType<TProps extends ModelProps> =
ModelPropsToSnapshotCreationData<TProps>

@@ -35,5 +36,3 @@ /**

*/
export type ToSnapshotDefaultType<TProps extends ModelProps> = SnapshotOutOfObject<
ModelPropsToData<TProps>
>
export type ToSnapshotDefaultType<TProps extends ModelProps> = ModelPropsToSnapshotData<TProps>

@@ -44,17 +43,10 @@ export type _ModelId<SuperModel, TProps extends ModelProps> = SuperModel extends AnyModel

export interface _Model<SuperModel, TProps extends ModelProps, FromSnapshot, ToSnapshot> {
export interface _Model<
SuperModel,
TProps extends ModelProps,
FromSnapshotOverride,
ToSnapshotOverride
> {
new (data: _ComposedCreationData<SuperModel, TProps>): SuperModel &
BaseModel<
ModelPropsToData<TProps>,
ModelPropsToCreationData<TProps>,
ModelPropsToTransformedData<TProps>,
ModelPropsToTransformedCreationData<TProps>,
FromSnapshot & {
[modelTypeKey]: string
},
ToSnapshot & {
[modelTypeKey]: string
},
_ModelId<SuperModel, TProps>
> &
BaseModel<TProps, FromSnapshotOverride, ToSnapshotOverride, _ModelId<SuperModel, TProps>> &
Omit<ModelPropsToTransformedData<TProps>, BaseModelKeys> &

@@ -68,3 +60,3 @@ ModelPropsToSetter<TProps>

export type ExtractModelIdProp<TProps extends ModelProps> = {
[K in keyof TProps]: TProps[K]["$isId"] extends true ? K : never
[K in keyof TProps]: TProps[K]["_internal"]["$isId"] extends true ? K : never
}[keyof TProps]

@@ -85,4 +77,4 @@

A extends [],
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
FS = never,
TS = never
>(

@@ -109,4 +101,4 @@ genFn: (...args: A) => {

TModel extends AnyModel,
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
FS = never,
TS = never
>(

@@ -122,4 +114,4 @@ baseModel: AbstractModelClass<TModel>,

TModel extends AnyModel,
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
FS = never,
TS = never
>(...args: any[]): _Model<TModel, TProps, FS, TS> {

@@ -155,8 +147,3 @@ let baseModel

*/
export function Model<
TProps extends ModelProps,
A extends [],
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
>(
export function Model<TProps extends ModelProps, A extends [], FS = never, TS = never>(
fnModelProps: (...args: A) => TProps,

@@ -175,14 +162,9 @@ modelOptions?: ModelOptions<TProps, FS, TS>

*/
export function Model<
TProps extends ModelProps,
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
>(modelProps: TProps, modelOptions?: ModelOptions<TProps, FS, TS>): _Model<unknown, TProps, FS, TS>
export function Model<TProps extends ModelProps, FS = never, TS = never>(
modelProps: TProps,
modelOptions?: ModelOptions<TProps, FS, TS>
): _Model<unknown, TProps, FS, TS>
// base
export function Model<
TProps extends ModelProps,
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
>(
export function Model<TProps extends ModelProps, FS = never, TS = never>(
fnModelPropsOrModelProps: (() => TProps) | TProps,

@@ -201,4 +183,4 @@ modelOptions?: ModelOptions<TProps, FS, TS>

TBaseModel extends AnyModel,
FS = FromSnapshotDefaultType<TProps>,
TS = ToSnapshotDefaultType<TProps>
FS = never,
TS = never
>(

@@ -205,0 +187,0 @@ modelProps: TProps,

@@ -56,3 +56,3 @@ import { action, set } from "mobx"

if (generateNewIds) {
id = (modelIdPropData.defaultFn as () => string)()
id = (modelIdPropData._internal.defaultFn as () => string)()
} else {

@@ -74,3 +74,3 @@ id = sn[modelIdPropertyName]

} else {
id = (modelIdPropData.defaultFn as () => string)()
id = (modelIdPropData._internal.defaultFn as () => string)()
}

@@ -98,5 +98,5 @@ }

// apply untransform (if any)
if (mode === "new" && propData.transform) {
if (mode === "new" && propData._internal.transform) {
changed = true
newValue = propData.transform.untransform(newValue, modelObj, k)
newValue = propData._internal.transform.untransform(newValue, modelObj, k)
}

@@ -106,8 +106,8 @@

if (newValue == null) {
if (propData.defaultFn !== noDefaultValue) {
if (propData._internal.defaultFn !== noDefaultValue) {
changed = true
newValue = propData.defaultFn()
} else if (propData.defaultValue !== noDefaultValue) {
newValue = propData._internal.defaultFn()
} else if (propData._internal.defaultValue !== noDefaultValue) {
changed = true
newValue = propData.defaultValue
newValue = propData._internal.defaultValue
}

@@ -114,0 +114,0 @@ }

import type { AnyDataModel } from "../dataModel/BaseDataModel"
import type { AnyModel } from "../model/BaseModel"
import type { modelTypeKey } from "../model/metadata"
import type { Flatten, IsNeverType } from "../utils/types"
import type {
ModelPropsToCreationData,
ModelPropsToSnapshotCreationData,
ModelPropsToSnapshotData,
ModelPropsToTransformedCreationData,
ModelPropsToTransformedData,
} from "./prop"

@@ -7,3 +16,3 @@ /**

*/
export const dataTypeSymbol = Symbol()
export const propsTypeSymbol = Symbol()

@@ -13,3 +22,3 @@ /**

*/
export const creationDataTypeSymbol = Symbol()
export const fromSnapshotOverrideTypeSymbol = Symbol()

@@ -19,21 +28,6 @@ /**

*/
export const transformedDataTypeSymbol = Symbol()
export const toSnapshotOverrideTypeSymbol = Symbol()
/**
* @ignore
*/
export const transformedCreationDataTypeSymbol = Symbol()
/**
* @ignore
*/
export const fromSnapshotTypeSymbol = Symbol()
/**
* @ignore
*/
export const toSnapshotTypeSymbol = Symbol()
/**
* @ignore
* @internal

@@ -61,5 +55,10 @@ */

/**
* The props of a model.
*/
export type ModelPropsOf<M extends AnyModel | AnyDataModel> = M[typeof propsTypeSymbol]
/**
* The data type of a model.
*/
export type ModelData<M extends AnyModel | AnyDataModel> = M["$"]
export type ModelData<M extends AnyModel | AnyDataModel> = Flatten<M["$"]>

@@ -69,3 +68,5 @@ /**

*/
export type ModelCreationData<M extends AnyModel | AnyDataModel> = M[typeof creationDataTypeSymbol]
export type ModelCreationData<M extends AnyModel | AnyDataModel> = ModelPropsToCreationData<
ModelPropsOf<M>
>

@@ -75,4 +76,5 @@ /**

*/
export type ModelTransformedData<M extends AnyModel | AnyDataModel> =
M[typeof transformedDataTypeSymbol]
export type ModelTransformedData<M extends AnyModel | AnyDataModel> = ModelPropsToTransformedData<
ModelPropsOf<M>
>

@@ -83,3 +85,3 @@ /**

export type ModelTransformedCreationData<M extends AnyModel | AnyDataModel> =
M[typeof transformedCreationDataTypeSymbol]
ModelPropsToTransformedCreationData<ModelPropsOf<M>>

@@ -90,3 +92,7 @@ /**

*/
export type ModelFromSnapshot<M extends AnyModel> = M[typeof fromSnapshotTypeSymbol]
export type ModelFromSnapshot<M extends AnyModel> = IsNeverType<
M[typeof fromSnapshotOverrideTypeSymbol],
ModelPropsToSnapshotCreationData<ModelPropsOf<M>>,
M[typeof fromSnapshotOverrideTypeSymbol]
> & { [modelTypeKey]?: string }

@@ -97,3 +103,7 @@ /**

*/
export type ModelToSnapshot<M extends AnyModel> = M[typeof toSnapshotTypeSymbol]
export type ModelToSnapshot<M extends AnyModel> = IsNeverType<
M[typeof toSnapshotOverrideTypeSymbol],
ModelPropsToSnapshotData<ModelPropsOf<M>>,
M[typeof toSnapshotOverrideTypeSymbol]
> & { [modelTypeKey]?: string }

@@ -100,0 +110,0 @@ /**

@@ -12,10 +12,11 @@ import { applySet } from "../action/applySet"

import type { ModelConstructorOptions } from "../model/ModelConstructorOptions"
import { typesObject } from "../typeChecking/object"
import { typesString } from "../typeChecking/primitives"
import type { AnyType } from "../typeChecking/schemas"
import { tProp } from "../typeChecking/tProp"
import type { LateTypeChecker } from "../typeChecking/TypeChecker"
import { typesUnchecked } from "../typeChecking/unchecked"
import { typesObject } from "../types/objectBased/object"
import { typesString } from "../types/primitiveBased/primitives"
import type { AnyType } from "../types/schemas"
import { tProp } from "../types/tProp"
import type { LateTypeChecker } from "../types/TypeChecker"
import { typesUnchecked } from "../types/utility/unchecked"
import { assertIsObject, failure, propNameToSetterName } from "../utils"
import { ModelClass, modelInitializedSymbol, ModelTransformedData } from "./BaseModelShared"
import { chainFns } from "../utils/chainFns"
import { ModelClass, modelInitializedSymbol } from "./BaseModelShared"
import { modelInitializersSymbol } from "./modelClassInitializer"

@@ -36,3 +37,3 @@ import { getInternalModelClassPropsInfo, setInternalModelClassPropsInfo } from "./modelPropsInfo"

export function createModelPropDescriptor(
function createModelPropDescriptor(
modelPropName: string,

@@ -63,13 +64,13 @@ modelProp: AnyModelProp | undefined,

modelProp: AnyModelProp | undefined,
modelPropName: keyof ModelTransformedData<M>
): ModelTransformedData<M>[typeof modelPropName] {
modelPropName: string
): any {
// no need to use get since these vars always get on the initial $
const value = model.$[modelPropName]
if (modelProp?.transform) {
return modelProp.transform.transform(value, model, modelPropName, (newValue) => {
if (modelProp?._internal.transform) {
return modelProp._internal.transform.transform(value, model, modelPropName, (newValue) => {
// use apply set instead to wrap it in an action
// set the $ object to set the original value directly
applySet(model.$, modelPropName, newValue)
})
}) as any
}

@@ -83,13 +84,13 @@

modelProp: AnyModelProp | undefined,
modelPropName: keyof ModelTransformedData<M>,
value: ModelTransformedData<M>[typeof modelPropName]
modelPropName: string,
value: any
): void {
if (modelProp?.setter === "assign" && !getCurrentActionContext()) {
if (modelProp?._internal.setter === "assign" && !getCurrentActionContext()) {
// use apply set instead to wrap it in an action
applySet(model, modelPropName, value)
applySet(model, modelPropName as any, value)
return
}
const transformedValue = modelProp?.transform
? modelProp.transform.untransform(value, model, modelPropName)
const transformedValue = modelProp?._internal.transform
? modelProp._internal.transform.untransform(value, model, modelPropName)
: value

@@ -102,6 +103,9 @@ // no need to use set since these vars always get on the initial $

const tPropForId = tProp(typesString, idGenerator)
tPropForId.isId = true
tPropForId._internal.isId = true
const propForId = prop(idGenerator)
propForId.isId = true
propForId._internal.isId = true
type FromSnapshotProcessorFn = (sn: any) => any
type ToSnapshotProcessorFn = (sn: any, instance: any) => any
export function sharedInternalModel<

@@ -122,4 +126,4 @@ TProps extends ModelProps,

valueType: boolean
fromSnapshotProcessor: ((sn: any) => any) | undefined
toSnapshotProcessor: ((sn: any, instance: any) => any) | undefined
fromSnapshotProcessor: FromSnapshotProcessorFn | undefined
toSnapshotProcessor: ToSnapshotProcessorFn | undefined
}): any {

@@ -156,3 +160,3 @@ assertIsObject(modelProps, "modelProps")

const p = composedModelProps[k]
return p.isId
return p._internal.isId
})

@@ -169,3 +173,5 @@ if (type === "class") {

const needsTypeChecker = Object.values(composedModelProps).some((mp) => !!mp.typeChecker)
const needsTypeChecker = Object.values(composedModelProps).some(
(mp) => !!mp._internal.typeChecker
)

@@ -178,3 +184,3 @@ // transform id keys (only one really)

let baseProp: AnyModelProp = needsTypeChecker ? tPropForId : propForId
switch (idProp?.setter) {
switch (idProp?._internal.setter) {
case true:

@@ -199,3 +205,3 @@ baseProp = baseProp.withSetter()

for (const [k, mp] of Object.entries(composedModelProps)) {
typeCheckerObj[k] = !mp.typeChecker ? typesUnchecked() : mp.typeChecker
typeCheckerObj[k] = !mp._internal.typeChecker ? typesUnchecked() : mp._internal.typeChecker
}

@@ -280,3 +286,3 @@ dataTypeChecker = typesObject(() => typeCheckerObj) as any

for (const [propName, propData] of Object.entries(modelProps)) {
if (propData.setter === true) {
if (propData._internal.setter === true) {
const setterName = propNameToSetterName(propName)

@@ -298,5 +304,14 @@

const modelPropsFromSnapshotProcessor = getModelPropsFromSnapshotProcessor(composedModelProps)
const modelPropsToSnapshotProcessor = getModelPropsToSnapshotProcessor(composedModelProps)
if (fromSnapshotProcessor) {
const fn = fromSnapshotProcessor
fromSnapshotProcessor = (sn) => ({ ...fn(sn), [modelTypeKey]: sn[modelTypeKey] })
fromSnapshotProcessor = (sn) => {
return {
...fn(sn),
[modelTypeKey]: sn[modelTypeKey],
}
}
}

@@ -306,12 +321,60 @@

const fn = toSnapshotProcessor
toSnapshotProcessor = (sn: any, instance: any) => ({
...fn(sn, instance),
[modelTypeKey]: sn[modelTypeKey],
})
toSnapshotProcessor = (sn, modelInstance) => {
return {
...fn(sn, modelInstance),
[modelTypeKey]: sn[modelTypeKey],
}
}
}
CustomBaseModel.fromSnapshotProcessor = fromSnapshotProcessor
CustomBaseModel.toSnapshotProcessor = toSnapshotProcessor
CustomBaseModel.fromSnapshotProcessor = chainFns(
fromSnapshotProcessor,
modelPropsFromSnapshotProcessor
)
CustomBaseModel.toSnapshotProcessor = chainFns(modelPropsToSnapshotProcessor, toSnapshotProcessor)
return CustomBaseModel
}
function getModelPropsFromSnapshotProcessor(
composedModelProps: ModelProps
): FromSnapshotProcessorFn | undefined {
const propsWithFromSnapshotProcessor = Object.entries(composedModelProps).filter(
([_propName, propData]) => propData._internal.fromSnapshotProcessor
)
if (propsWithFromSnapshotProcessor.length <= 0) {
return undefined
}
return (sn) => {
const newSn = { ...sn }
for (const [propName, propData] of propsWithFromSnapshotProcessor) {
if (propData._internal.fromSnapshotProcessor) {
newSn[propName] = propData._internal.fromSnapshotProcessor(sn[propName])
}
}
return newSn
}
}
function getModelPropsToSnapshotProcessor(
composedModelProps: ModelProps
): ToSnapshotProcessorFn | undefined {
const propsWithToSnapshotProcessor = Object.entries(composedModelProps).filter(
([_propName, propData]) => propData._internal.toSnapshotProcessor
)
if (propsWithToSnapshotProcessor.length <= 0) {
return undefined
}
return (sn) => {
const newSn = { ...sn }
for (const [propName, propData] of propsWithToSnapshotProcessor) {
if (propData._internal.toSnapshotProcessor) {
newSn[propName] = propData._internal.toSnapshotProcessor(sn[propName])
}
}
return newSn
}
}

@@ -166,9 +166,7 @@ import { HookAction } from "../action/hookActions"

*/
export function decoratedModel<MC extends ModelClass<AnyModel>>(
export function decoratedModel<M, MC extends abstract new (...ags: any) => M>(
name: string | undefined,
clazz: MC,
decorators: {
[k in keyof InstanceType<MC>]?:
| ((...args: any[]) => any)
| ReadonlyArray<(...args: any[]) => any>
[k in keyof M]?: ((...args: any[]) => any) | ReadonlyArray<(...args: any[]) => any>
}

@@ -189,3 +187,3 @@ ): MC {

return name ? model(name)(clazz) : clazz
return (name ? model(name)(clazz as unknown as ModelClass<AnyModel>) : clazz) as MC
}
import type { O } from "ts-toolbelt"
import type { LateTypeChecker, TypeChecker } from "../typeChecking/TypeChecker"
import type { SnapshotInOf, SnapshotOutOf } from "../snapshot/SnapshotOf"
import type { LateTypeChecker, TypeChecker } from "../types/TypeChecker"
import { getOrCreate } from "../utils/mapUtils"
import type { IsOptionalValue } from "../utils/types"
import type { Flatten, IsNeverType, IsOptionalValue } from "../utils/types"

@@ -21,28 +22,36 @@ /**

TIsId extends boolean = false,
THasSetter = never
THasSetter = never,
TFromSnapshotOverride = never,
TToSnapshotOverride = never
> {
$valueType: TPropValue
$creationValueType: TPropCreationValue
$transformedValueType: TTransformedValue
$transformedCreationValueType: TTransformedCreationValue
$isOptional: TIsOptional
$isId: TIsId
$hasSetter: THasSetter
_internal: {
$valueType: TPropValue
$creationValueType: TPropCreationValue
$transformedValueType: TTransformedValue
$transformedCreationValueType: TTransformedCreationValue
$isOptional: TIsOptional
$isId: TIsId
$hasSetter: THasSetter
$fromSnapshotOverride: TFromSnapshotOverride
$toSnapshotOverride: TToSnapshotOverride
defaultFn: (() => TPropValue) | typeof noDefaultValue
defaultValue: TPropValue | typeof noDefaultValue
typeChecker: TypeChecker | LateTypeChecker | undefined
setter: boolean | "assign"
isId: boolean
transform:
| {
transform(
original: unknown,
model: object,
propName: PropertyKey,
setOriginalValue: (newOriginalValue: unknown) => void
): unknown
untransform(transformed: unknown, model: object, propName: PropertyKey): unknown
}
| undefined
defaultFn: (() => TPropValue) | typeof noDefaultValue
defaultValue: TPropValue | typeof noDefaultValue
typeChecker: TypeChecker | LateTypeChecker | undefined
setter: boolean | "assign"
isId: boolean
transform:
| {
transform(
original: unknown,
model: object,
propName: PropertyKey,
setOriginalValue: (newOriginalValue: unknown) => void
): unknown
untransform(transformed: unknown, model: object, propName: PropertyKey): unknown
}
| undefined
fromSnapshotProcessor?(sn: unknown): unknown
toSnapshotProcessor?(sn: unknown): unknown
}

@@ -56,3 +65,5 @@ withSetter(): ModelProp<

TIsId,
string
string,
TFromSnapshotOverride,
TToSnapshotOverride
>

@@ -71,3 +82,5 @@ /**

TIsId,
string
string,
TFromSnapshotOverride,
TToSnapshotOverride
>

@@ -91,7 +104,46 @@

TIsId,
THasSetter
THasSetter,
TFromSnapshotOverride,
TToSnapshotOverride
>
withSnapshotProcessor<
FS = TFromSnapshotOverride,
TS = TToSnapshotOverride,
This extends AnyModelProp = this
>(processor: {
fromSnapshot?(sn: FS): ModelPropFromSnapshot<This>
toSnapshot?(sn: ModelPropToSnapshot<This>): TS
}): ModelProp<
TPropValue,
TPropCreationValue,
TTransformedValue,
TTransformedCreationValue,
TIsOptional,
TIsId,
THasSetter,
FS,
TS
>
}
/**
* The snapshot in type of a model property.
*/
export type ModelPropFromSnapshot<MP extends AnyModelProp> = IsNeverType<
MP["_internal"]["$fromSnapshotOverride"],
SnapshotInOf<MP["_internal"]["$creationValueType"]>,
MP["_internal"]["$fromSnapshotOverride"]
>
/**
* The snapshot out type of a model property.
*/
export type ModelPropToSnapshot<MP extends AnyModelProp> = IsNeverType<
MP["_internal"]["$toSnapshotOverride"],
SnapshotOutOf<MP["_internal"]["$valueType"]>,
MP["_internal"]["$toSnapshotOverride"]
>
/**
* A model prop transform.

@@ -112,3 +164,3 @@ */

*/
export type AnyModelProp = ModelProp<any, any, any, any, any, any, any>
export type AnyModelProp = ModelProp<any, any, any, any, any, any, any, any, any>

@@ -123,44 +175,79 @@ /**

export type OptionalModelProps<MP extends ModelProps> = {
[K in keyof MP]: MP[K]["$isOptional"] & K
[K in keyof MP]: MP[K]["_internal"]["$isOptional"] & K
}[keyof MP]
export type ModelPropsToData<MP extends ModelProps> = {
[k in keyof MP]: MP[k]["$valueType"]
}
export type ModelPropsToData<MP extends ModelProps> = Flatten<{
[k in keyof MP]: MP[k]["_internal"]["$valueType"]
}>
export type ModelPropsToSnapshotData<MP extends ModelProps> = Flatten<{
[k in keyof MP]: ModelPropToSnapshot<MP[k]> extends infer R ? R : never
}>
// we don't use O.Optional anymore since it generates unions too heavy
// also if we use pick over the optional props we will loose the ability
// to infer generics
export type ModelPropsToCreationData<MP extends ModelProps> = {
[k in keyof MP]?: MP[k]["$creationValueType"]
} & O.Omit<
export type ModelPropsToCreationData<MP extends ModelProps> = Flatten<
{
[k in keyof MP]: MP[k]["$creationValueType"]
},
OptionalModelProps<MP>
[k in keyof MP]?: MP[k]["_internal"]["$creationValueType"]
} & O.Omit<
{
[k in keyof MP]: MP[k]["_internal"]["$creationValueType"]
},
OptionalModelProps<MP>
>
>
export type ModelPropsToTransformedData<MP extends ModelProps> = {
[k in keyof MP]: MP[k]["$transformedValueType"]
}
// we don't use O.Optional anymore since it generates unions too heavy
// also if we use pick over the optional props we will loose the ability
// to infer generics
export type ModelPropsToSnapshotCreationData<MP extends ModelProps> = Flatten<
{
[k in keyof MP]?: ModelPropFromSnapshot<MP[k]> extends infer R ? R : never
} & O.Omit<
{
[k in keyof MP]: ModelPropFromSnapshot<MP[k]> extends infer R ? R : never
},
{
[K in keyof MP]: IsNeverType<
MP[K]["_internal"]["$fromSnapshotOverride"],
MP[K]["_internal"]["$isOptional"] & K,
IsOptionalValue<MP[K]["_internal"]["$fromSnapshotOverride"], K, never>
>
}[keyof MP]
>
>
export type ModelPropsToTransformedData<MP extends ModelProps> = Flatten<{
[k in keyof MP]: MP[k]["_internal"]["$transformedValueType"]
}>
// we don't use O.Optional anymore since it generates unions too heavy
// also if we use pick over the optional props we will loose the ability
// to infer generics
export type ModelPropsToTransformedCreationData<MP extends ModelProps> = {
[k in keyof MP]?: MP[k]["$transformedCreationValueType"]
} & O.Omit<
export type ModelPropsToTransformedCreationData<MP extends ModelProps> = Flatten<
{
[k in keyof MP]: MP[k]["$transformedCreationValueType"]
},
OptionalModelProps<MP>
[k in keyof MP]?: MP[k]["_internal"]["$transformedCreationValueType"]
} & O.Omit<
{
[k in keyof MP]: MP[k]["_internal"]["$transformedCreationValueType"]
},
OptionalModelProps<MP>
>
>
export type ModelPropsToSetter<MP extends ModelProps> = {
[k in keyof MP as MP[k]["$hasSetter"] & `set${Capitalize<k & string>}`]: (
value: MP[k]["$transformedValueType"]
export type ModelPropsToSetter<MP extends ModelProps> = Flatten<{
[k in keyof MP as MP[k]["_internal"]["$hasSetter"] & `set${Capitalize<k & string>}`]: (
value: MP[k]["_internal"]["$transformedValueType"]
) => void
}
}>
export type ModelIdProp = ModelProp<string, string, string, string, string, true>
export type ModelIdProp = ModelProp<
string,
string | undefined,
string,
string | undefined,
string,
true
>

@@ -172,7 +259,9 @@ /**

export const idProp = {
setter: false,
isId: true,
_internal: {
setter: false,
isId: true,
},
withSetter(mode?: boolean | "assign") {
return { ...this, setter: mode ?? true }
return { ...this, _internal: { ...this._internal, setter: mode ?? true } }
},

@@ -267,24 +356,65 @@ } as any as ModelIdProp

const obj: AnyModelProp = {
$valueType: null as any,
$creationValueType: null as any,
$transformedValueType: null as any,
$transformedCreationValueType: null as any,
$isOptional: null as any,
$isId: null as never,
$hasSetter: null as never,
_internal: {
$valueType: null as any,
$creationValueType: null as any,
$transformedValueType: null as any,
$transformedCreationValueType: null as any,
$isOptional: null as any,
$isId: null as never,
$hasSetter: null as never,
$fromSnapshotOverride: null as never,
$toSnapshotOverride: null as never,
defaultFn: hasDefaultValue && isDefFn ? def : noDefaultValue,
defaultValue: hasDefaultValue && !isDefFn ? def : noDefaultValue,
typeChecker: undefined,
setter: false,
isId: false,
transform: undefined,
defaultFn: hasDefaultValue && isDefFn ? def : noDefaultValue,
defaultValue: hasDefaultValue && !isDefFn ? def : noDefaultValue,
typeChecker: undefined,
setter: false,
isId: false,
transform: undefined,
fromSnapshotProcessor: undefined,
toSnapshotProcessor: undefined,
},
withSetter(mode?: boolean | "assign") {
return { ...this, setter: mode ?? true }
return { ...this, _internal: { ...this._internal, setter: mode ?? true } }
},
withTransform(transform: ModelPropTransform<unknown, unknown>) {
return { ...this, transform: toFullTransform(transform) }
return { ...this, _internal: { ...this._internal, transform: toFullTransform(transform) } }
},
withSnapshotProcessor({ fromSnapshot, toSnapshot }) {
let newFromSnapshot
if (this._internal.fromSnapshotProcessor && fromSnapshot) {
const oldFn = this._internal.fromSnapshotProcessor
const newFn = fromSnapshot
newFromSnapshot = (sn: any) => oldFn(newFn(sn))
} else if (fromSnapshot) {
newFromSnapshot = fromSnapshot
} else {
newFromSnapshot = this._internal.fromSnapshotProcessor
}
let newToSnapshot
if (this._internal.toSnapshotProcessor && toSnapshot) {
const oldFn: any = this._internal.toSnapshotProcessor
const newFn = toSnapshot
newToSnapshot = (sn: any) => newFn(oldFn(sn))
} else if (toSnapshot) {
newToSnapshot = toSnapshot
} else {
newToSnapshot = this._internal.toSnapshotProcessor
}
return {
...this,
_internal: {
...this._internal,
fromSnapshotProcessor: newFromSnapshot,
toSnapshotProcessor: newToSnapshot,
},
}
},
}

@@ -291,0 +421,0 @@

@@ -11,6 +11,3 @@ import { createAtom, IAtom } from "mobx"

/**
* @ignore
*/
export const objectParentsAtoms = new WeakMap<object, IAtom>()
const objectParentsAtoms = new WeakMap<object, IAtom>()

@@ -17,0 +14,0 @@ /**

@@ -145,7 +145,3 @@ import { action, createAtom, IAtom, observable, ObservableSet } from "mobx"

/**
* @ignore
* @internal
*/
export interface DeepObjectChildrenExtension<D> {
interface DeepObjectChildrenExtension<D> {
initData(): D

@@ -152,0 +148,0 @@ addNode(node: any, data: D): void

@@ -271,3 +271,3 @@ import { computed, IComputedValue } from "mobx"

current = modelToDataNode(current[p])
current = modelToDataNode(current[p] as object)
}

@@ -329,3 +329,3 @@

const currentMaybeModel = current[p]
current = modelToDataNode(currentMaybeModel)
current = modelToDataNode(currentMaybeModel as object)

@@ -332,0 +332,0 @@ const expectedId = pathIds[i]

@@ -5,4 +5,4 @@ import { computed } from "mobx"

import type { ModelClass } from "../modelShared/BaseModelShared"
import { typesString } from "../typeChecking/primitives"
import { tProp } from "../typeChecking/tProp"
import { typesString } from "../types/primitiveBased/primitives"
import { tProp } from "../types/tProp"
import { failure } from "../utils"

@@ -9,0 +9,0 @@

@@ -11,3 +11,3 @@ import { isObservableObject } from "mobx"

import type { ModelClass } from "../modelShared/BaseModelShared"
import { getModelInfoForName } from "../modelShared/modelInfo"
import { getModelInfoForName, modelInfoByClass } from "../modelShared/modelInfo"
import { assertTweakedObject } from "../tweaker/core"

@@ -17,3 +17,3 @@ import { assertIsObject, failure, inDevMode, isArray, isPlainObject, lazy } from "../utils"

import { reconcileSnapshot } from "./reconcileSnapshot"
import type { SnapshotInOf } from "./SnapshotOf"
import type { SnapshotInOf, SnapshotOutOf } from "./SnapshotOf"

@@ -27,3 +27,14 @@ /**

*/
export function applySnapshot<T extends object>(node: T, snapshot: SnapshotInOf<T>): void {
export function applySnapshot<T extends object>(node: T, snapshot: SnapshotInOf<T>): void
/**
* Applies a full snapshot over an object, reconciling it with the current contents of the object.
*
* @typeparam T Object type.
* @param node Target object (model object, object or array).
* @param snapshot Snapshot to apply.
*/
export function applySnapshot<T extends object>(node: T, snapshot: SnapshotOutOf<T>): void
export function applySnapshot(node: object, snapshot: unknown): void {
assertTweakedObject(node, "node")

@@ -39,3 +50,6 @@ assertIsObject(snapshot, "snapshot")

*/
export function internalApplySnapshot<T extends object>(this: T, sn: SnapshotInOf<T>): void {
export function internalApplySnapshot<T extends object>(
this: T,
sn: SnapshotInOf<T> | SnapshotOutOf<T>
): void {
const obj = this

@@ -66,2 +80,8 @@

// adapt snapshot to target model if possible
if (isPlainObject(sn) && (sn as any)[modelTypeKey] === undefined && isModel(obj)) {
const modelInfo = modelInfoByClass.get((obj as any).constructor)!
sn = { ...(sn as any), [modelTypeKey]: modelInfo.name }
}
if (isModelSnapshot(sn)) {

@@ -68,0 +88,0 @@ const type = sn[modelTypeKey]

@@ -26,3 +26,3 @@ import { AnyModel } from "../model/BaseModel"

const modelIdPropertyName = getModelIdPropertyName(modelInfo.class as ModelClass<AnyModel>)
if (modelIdPropertyName && !sn[modelIdPropertyName]) {
if (modelIdPropertyName && !(sn as any)[modelIdPropertyName]) {
throw failure(

@@ -29,0 +29,0 @@ `a model snapshot of type '${type}' must contain an id key (${modelIdPropertyName}), but none was found`

import { action, observable, set } from "mobx"
import type { AnyDataModel } from "../dataModel/BaseDataModel"
import type { AnyModel } from "../model/BaseModel"
import { isReservedModelKey } from "../model/metadata"
import { isModelClass } from "../model/utils"
import type { ModelClass } from "../modelShared/BaseModelShared"
import { resolveTypeChecker } from "../types/resolveTypeChecker"
import type { AnyStandardType, TypeToData } from "../types/schemas"
import { isLateTypeChecker, TypeChecker } from "../types/TypeChecker"
import { failure, isMap, isPrimitive, isSet } from "../utils"

@@ -44,28 +50,65 @@ import type { SnapshotInOf, SnapshotInOfModel, SnapshotOutOf } from "./SnapshotOf"

/**
* Deserializers a data structure from its snapshot form.
* Given a type deserializes a data structure from its snapshot form.
*
* @typeparam TType Object type.
* @param type Type.
* @param snapshot Snapshot, even if a primitive.
* @param options Options.
* @returns The deserialized object.
*/
export function fromSnapshot<
TType extends AnyStandardType | ModelClass<AnyModel> | ModelClass<AnyDataModel>
>(
type: TType,
snapshot: SnapshotInOf<TypeToData<TType>>,
options?: Partial<FromSnapshotOptions>
): TypeToData<TType>
/**
* Deserializes a data structure from its snapshot form.
*
* @typeparam T Object type.
* @param snapshot Snapshot, even if a primitive.
* @param [options] Options.
* @param options Options.
* @returns The deserialized object.
*/
export let fromSnapshot = <T>(
export function fromSnapshot<T>(
snapshot: SnapshotInOf<T> | SnapshotOutOf<T>,
options?: Partial<FromSnapshotOptions>
): T => {
const opts = {
generateNewIds: false,
overrideRootModelId: undefined,
...options,
}
): T
const ctx: Partial<FromSnapshotContext> = {
options: opts,
export function fromSnapshot<T>(arg1: any, arg2: any, arg3?: any): T {
let snapshot: any
let options: Partial<FromSnapshotOptions> | undefined
if (isLateTypeChecker(arg1) || arg1 instanceof TypeChecker || isModelClass(arg1)) {
const typeChecker = resolveTypeChecker(arg1)
snapshot = typeChecker.fromSnapshotProcessor ? typeChecker.fromSnapshotProcessor(arg2) : arg2
options = arg3
} else {
snapshot = arg1
options = arg2
}
ctx.snapshotToInitialData = snapshotToInitialData.bind(undefined, ctx as FromSnapshotContext)
return internalFromSnapshot<T>(snapshot, ctx as FromSnapshotContext)
return fromSnapshotAction(snapshot, options)
}
fromSnapshot = action("fromSnapshot", fromSnapshot) as any
const fromSnapshotAction = action(
"fromSnapshot",
<T>(snapshot: SnapshotInOf<T>, options?: Partial<FromSnapshotOptions>): T => {
const opts = {
generateNewIds: false,
overrideRootModelId: undefined,
...options,
}
const ctx: Partial<FromSnapshotContext> = {
options: opts,
}
ctx.snapshotToInitialData = snapshotToInitialData.bind(undefined, ctx as FromSnapshotContext)
return internalFromSnapshot<T>(snapshot, ctx as FromSnapshotContext)
}
)
/**

@@ -72,0 +115,0 @@ * @ignore

import { assertTweakedObject } from "../tweaker/core"
import { resolveTypeChecker } from "../types/resolveTypeChecker"
import { AnyType, TypeToData } from "../types/schemas"
import { failure, isPrimitive } from "../utils"

@@ -6,2 +8,4 @@ import { getInternalSnapshot, reportInternalSnapshotObserved } from "./internal"

const identityFn = (x: any) => x
/**

@@ -16,5 +20,31 @@ * Retrieves an immutable snapshot for a data structure.

*/
export function getSnapshot<T>(nodeOrPrimitive: T): SnapshotOutOf<T> {
export function getSnapshot<T extends AnyType>(
type: T,
nodeOrPrimitive: TypeToData<T>
): SnapshotOutOf<TypeToData<T>>
/**
* Retrieves an immutable snapshot for a data structure.
* Since returned snapshots are immutable they will respect shallow equality, this is,
* if no changes are made then the snapshot will be kept the same.
*
* @typeparam T Object type.
* @param nodeOrPrimitive Data structure, including primtives.
* @returns The snapshot.
*/
export function getSnapshot<T>(nodeOrPrimitive: T): SnapshotOutOf<T>
export function getSnapshot(arg1: any, arg2?: any): any {
let toSnapshotProcessor = identityFn
let nodeOrPrimitive: any
if (arguments.length >= 2) {
toSnapshotProcessor = resolveTypeChecker(arg1).toSnapshotProcessor
nodeOrPrimitive = arg2
} else {
nodeOrPrimitive = arg1
}
if (isPrimitive(nodeOrPrimitive)) {
return nodeOrPrimitive as any
return toSnapshotProcessor(nodeOrPrimitive)
}

@@ -30,3 +60,3 @@

reportInternalSnapshotObserved(snapshot)
return snapshot.transformed
return toSnapshotProcessor(snapshot.transformed)
}

@@ -24,3 +24,3 @@ import { remove, set } from "mobx"

): AnyModel {
const type = sn[modelTypeKey]
const type = sn[modelTypeKey]!

@@ -27,0 +27,0 @@ const modelInfo = getModelInfoForName(type)

@@ -8,7 +8,3 @@ import { set } from "mobx"

/**
* @ignore
* @internal
*/
export type Reconciler = (value: any, sn: any, modelPool: ModelPool, parent: any) => any | undefined
type Reconciler = (value: any, sn: any, modelPool: ModelPool, parent: any) => any | undefined

@@ -15,0 +11,0 @@ const reconcilers: { priority: number; reconciler: Reconciler }[] = []

@@ -24,3 +24,3 @@ import type { Frozen, frozenKey } from "../frozen/Frozen"

items: { [k: string]: SnapshotOutOf<V> }
[modelTypeKey]: string
[modelTypeKey]?: string
[modelIdKey]: string

@@ -31,3 +31,3 @@ }

items: SnapshotOutOf<V>[]
[modelTypeKey]: string
[modelTypeKey]?: string
[modelIdKey]: string

@@ -73,3 +73,3 @@ }

items?: { [k: string]: SnapshotOutOf<V> }
[modelTypeKey]: string
[modelTypeKey]?: string
[modelIdKey]: string

@@ -80,3 +80,3 @@ }

items?: SnapshotOutOf<V>[]
[modelTypeKey]: string
[modelTypeKey]?: string
[modelIdKey]: string

@@ -83,0 +83,0 @@ }

@@ -8,4 +8,4 @@ import { action } from "mobx"

import { unsetInternalSnapshot } from "../snapshot/internal"
import type { AnyStandardType, TypeToData } from "../typeChecking/schemas"
import { typeCheck } from "../typeChecking/typeCheck"
import type { AnyStandardType, TypeToData } from "../types/schemas"
import { typeCheck } from "../types/typeCheck"
import { failure, inDevMode, isMap, isObject, isPrimitive, isSet } from "../utils"

@@ -12,0 +12,0 @@ import { isTweakedObject, tweakedObjects } from "./core"

@@ -10,3 +10,3 @@ import { isModelAutoTypeCheckingEnabled } from "../globalConfig/globalConfig"

import { internalApplySnapshot } from "../snapshot/applySnapshot"
import { invalidateCachedTypeCheckerResult } from "../typeChecking/TypeChecker"
import { invalidateCachedTypeCheckerResult } from "../types/TypeChecker"
import { runWithoutSnapshotOrPatches } from "./core"

@@ -13,0 +13,0 @@ import { isTypeCheckingAllowed } from "./withoutTypeChecking"

@@ -89,3 +89,3 @@ import * as mobx from "mobx"

*/
export function isObject(value: any): value is Object {
export function isObject(value: any): value is Record<PropertyKey, unknown> {
return value !== null && typeof value === "object"

@@ -92,0 +92,0 @@ }

@@ -32,1 +32,11 @@ /**

// type _F = IsOptionalValue<unknown, true, false> // true
/**
* @ignore
*/
export type IsNeverType<T, IfNever, IfNotNever> = [T] extends [never] ? IfNever : IfNotNever
/**
* @ignore
*/
export type Flatten<T> = T extends Record<any, any> ? { [P in keyof T]: T[P] } : T

@@ -7,5 +7,5 @@ import { values } from "mobx"

import { idProp } from "../modelShared/prop"
import { typesArray } from "../typeChecking/array"
import { tProp } from "../typeChecking/tProp"
import { typesUnchecked } from "../typeChecking/unchecked"
import { typesArray } from "../types/arrayBased/array"
import { tProp } from "../types/tProp"
import { typesUnchecked } from "../types/utility/unchecked"

@@ -12,0 +12,0 @@ /**

@@ -7,5 +7,5 @@ import { entries, get, has, keys, remove, set, values } from "mobx"

import { idProp } from "../modelShared/prop"
import { typesRecord } from "../typeChecking/record"
import { tProp } from "../typeChecking/tProp"
import { typesUnchecked } from "../typeChecking/unchecked"
import { typesRecord } from "../types/objectBased/record"
import { tProp } from "../types/tProp"
import { typesUnchecked } from "../types/utility/unchecked"

@@ -12,0 +12,0 @@ /**

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc