@jbrowse/mobx-state-tree 5.6.10 → 5.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/action.js.map +1 -1
- package/dist/core/actionContext.js.map +1 -1
- package/dist/core/flow.js.map +1 -1
- package/dist/core/json-patch.js.map +1 -1
- package/dist/core/mst-operations.js.map +1 -1
- package/dist/core/node/BaseNode.js.map +1 -1
- package/dist/core/node/Hook.js.map +1 -1
- package/dist/core/node/create-node.js.map +1 -1
- package/dist/core/node/identifier-cache.js.map +1 -1
- package/dist/core/node/livelinessChecking.js.map +1 -1
- package/dist/core/node/node-utils.js.map +1 -1
- package/dist/core/node/object-node.d.ts +3 -1
- package/dist/core/node/object-node.js +2 -1
- package/dist/core/node/object-node.js.map +1 -1
- package/dist/core/node/scalar-node.d.ts +3 -1
- package/dist/core/node/scalar-node.js +2 -1
- package/dist/core/node/scalar-node.js.map +1 -1
- package/dist/core/process.js.map +1 -1
- package/dist/core/type/type-checker.js.map +1 -1
- package/dist/core/type/type.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.js.map +1 -1
- package/dist/middlewares/create-action-tracking-middleware.js.map +1 -1
- package/dist/middlewares/createActionTrackingMiddleware2.js.map +1 -1
- package/dist/middlewares/on-action.js.map +1 -1
- package/dist/package.json +1 -0
- package/dist/types/complex-types/array.d.ts +2 -1
- package/dist/types/complex-types/array.js +2 -1
- package/dist/types/complex-types/array.js.map +1 -1
- package/dist/types/complex-types/map.d.ts +2 -1
- package/dist/types/complex-types/map.js +2 -1
- package/dist/types/complex-types/map.js.map +1 -1
- package/dist/types/complex-types/model.d.ts +2 -1
- package/dist/types/complex-types/model.js +2 -1
- package/dist/types/complex-types/model.js.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/primitives.d.ts +2 -1
- package/dist/types/primitives.js +2 -1
- package/dist/types/primitives.js.map +1 -1
- package/dist/types/utility-types/custom.d.ts +2 -1
- package/dist/types/utility-types/custom.js +2 -1
- package/dist/types/utility-types/custom.js.map +1 -1
- package/dist/types/utility-types/enumeration.js.map +1 -1
- package/dist/types/utility-types/frozen.d.ts +2 -1
- package/dist/types/utility-types/frozen.js +2 -1
- package/dist/types/utility-types/frozen.js.map +1 -1
- package/dist/types/utility-types/identifier.d.ts +2 -1
- package/dist/types/utility-types/identifier.js +2 -1
- package/dist/types/utility-types/identifier.js.map +1 -1
- package/dist/types/utility-types/late.js +2 -1
- package/dist/types/utility-types/late.js.map +1 -1
- package/dist/types/utility-types/lazy.d.ts +2 -1
- package/dist/types/utility-types/lazy.js +2 -1
- package/dist/types/utility-types/lazy.js.map +1 -1
- package/dist/types/utility-types/literal.d.ts +2 -1
- package/dist/types/utility-types/literal.js +2 -1
- package/dist/types/utility-types/literal.js.map +1 -1
- package/dist/types/utility-types/maybe.js.map +1 -1
- package/dist/types/utility-types/optional.d.ts +2 -1
- package/dist/types/utility-types/optional.js +2 -1
- package/dist/types/utility-types/optional.js.map +1 -1
- package/dist/types/utility-types/reference.d.ts +2 -1
- package/dist/types/utility-types/reference.js +2 -1
- package/dist/types/utility-types/reference.js.map +1 -1
- package/dist/types/utility-types/refinement.js +2 -1
- package/dist/types/utility-types/refinement.js.map +1 -1
- package/dist/types/utility-types/resilient.js +2 -1
- package/dist/types/utility-types/resilient.js.map +1 -1
- package/dist/types/utility-types/snapshotProcessor.js +3 -2
- package/dist/types/utility-types/snapshotProcessor.js.map +1 -1
- package/dist/types/utility-types/union.d.ts +2 -1
- package/dist/types/utility-types/union.js +2 -1
- package/dist/types/utility-types/union.js.map +1 -1
- package/dist/utils.js.map +1 -1
- package/esm/core/action.d.ts +87 -0
- package/esm/core/action.js +219 -0
- package/esm/core/action.js.map +1 -0
- package/esm/core/actionContext.d.ts +27 -0
- package/esm/core/actionContext.js +37 -0
- package/esm/core/actionContext.js.map +1 -0
- package/esm/core/flow.d.ts +69 -0
- package/esm/core/flow.js +173 -0
- package/esm/core/flow.js.map +1 -0
- package/esm/core/json-patch.d.ts +46 -0
- package/esm/core/json-patch.js +125 -0
- package/esm/core/json-patch.js.map +1 -0
- package/esm/core/mst-operations.d.ts +459 -0
- package/esm/core/mst-operations.js +844 -0
- package/esm/core/mst-operations.js.map +1 -0
- package/esm/core/node/BaseNode.d.ts +62 -0
- package/esm/core/node/BaseNode.js +148 -0
- package/esm/core/node/BaseNode.js.map +1 -0
- package/esm/core/node/Hook.d.ts +17 -0
- package/esm/core/node/Hook.js +12 -0
- package/esm/core/node/Hook.js.map +1 -0
- package/esm/core/node/create-node.d.ts +16 -0
- package/esm/core/node/create-node.js +36 -0
- package/esm/core/node/create-node.js.map +1 -0
- package/esm/core/node/identifier-cache.d.ts +19 -0
- package/esm/core/node/identifier-cache.js +111 -0
- package/esm/core/node/identifier-cache.js.map +1 -0
- package/esm/core/node/livelinessChecking.d.ts +37 -0
- package/esm/core/node/livelinessChecking.js +33 -0
- package/esm/core/node/livelinessChecking.js.map +1 -0
- package/esm/core/node/node-utils.d.ts +83 -0
- package/esm/core/node/node-utils.js +153 -0
- package/esm/core/node/node-utils.js.map +1 -0
- package/esm/core/node/object-node.d.ts +101 -0
- package/esm/core/node/object-node.js +542 -0
- package/esm/core/node/object-node.js.map +1 -0
- package/esm/core/node/scalar-node.d.ts +21 -0
- package/esm/core/node/scalar-node.js +86 -0
- package/esm/core/node/scalar-node.js.map +1 -0
- package/esm/core/process.d.ts +50 -0
- package/esm/core/process.js +35 -0
- package/esm/core/process.js.map +1 -0
- package/esm/core/type/type-checker.d.ts +69 -0
- package/esm/core/type/type-checker.js +144 -0
- package/esm/core/type/type-checker.js.map +1 -0
- package/esm/core/type/type.d.ts +317 -0
- package/esm/core/type/type.js +243 -0
- package/esm/core/type/type.js.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/index.js.map +1 -0
- package/esm/internal.d.ts +39 -0
- package/esm/internal.js +44 -0
- package/esm/internal.js.map +1 -0
- package/esm/middlewares/create-action-tracking-middleware.d.ts +24 -0
- package/esm/middlewares/create-action-tracking-middleware.js +78 -0
- package/esm/middlewares/create-action-tracking-middleware.js.map +1 -0
- package/esm/middlewares/createActionTrackingMiddleware2.d.ts +34 -0
- package/esm/middlewares/createActionTrackingMiddleware2.js +130 -0
- package/esm/middlewares/createActionTrackingMiddleware2.js.map +1 -0
- package/esm/middlewares/on-action.d.ts +87 -0
- package/esm/middlewares/on-action.js +210 -0
- package/esm/middlewares/on-action.js.map +1 -0
- package/esm/types/complex-types/array.d.ts +81 -0
- package/esm/types/complex-types/array.js +347 -0
- package/esm/types/complex-types/array.js.map +1 -0
- package/esm/types/complex-types/map.d.ts +111 -0
- package/esm/types/complex-types/map.js +356 -0
- package/esm/types/complex-types/map.js.map +1 -0
- package/esm/types/complex-types/model.d.ts +193 -0
- package/esm/types/complex-types/model.js +471 -0
- package/esm/types/complex-types/model.js.map +1 -0
- package/esm/types/index.d.ts +33 -0
- package/esm/types/index.js +35 -0
- package/esm/types/index.js.map +1 -0
- package/esm/types/primitives.d.ts +125 -0
- package/esm/types/primitives.js +177 -0
- package/esm/types/primitives.js.map +1 -0
- package/esm/types/utility-types/custom.d.ts +75 -0
- package/esm/types/utility-types/custom.js +106 -0
- package/esm/types/utility-types/custom.js.map +1 -0
- package/esm/types/utility-types/enumeration.d.ts +5 -0
- package/esm/types/utility-types/enumeration.js +31 -0
- package/esm/types/utility-types/enumeration.js.map +1 -0
- package/esm/types/utility-types/frozen.d.ts +24 -0
- package/esm/types/utility-types/frozen.js +92 -0
- package/esm/types/utility-types/frozen.js.map +1 -0
- package/esm/types/utility-types/identifier.d.ts +87 -0
- package/esm/types/utility-types/identifier.js +121 -0
- package/esm/types/utility-types/identifier.js.map +1 -0
- package/esm/types/utility-types/late.d.ts +10 -0
- package/esm/types/utility-types/late.js +106 -0
- package/esm/types/utility-types/late.js.map +1 -0
- package/esm/types/utility-types/lazy.d.ts +23 -0
- package/esm/types/utility-types/lazy.js +72 -0
- package/esm/types/utility-types/lazy.js.map +1 -0
- package/esm/types/utility-types/literal.d.ts +38 -0
- package/esm/types/utility-types/literal.js +58 -0
- package/esm/types/utility-types/literal.js.map +1 -0
- package/esm/types/utility-types/maybe.d.ts +26 -0
- package/esm/types/utility-types/maybe.js +26 -0
- package/esm/types/utility-types/maybe.js.map +1 -0
- package/esm/types/utility-types/optional.d.ts +42 -0
- package/esm/types/utility-types/optional.js +135 -0
- package/esm/types/utility-types/optional.js.map +1 -0
- package/esm/types/utility-types/reference.d.ts +90 -0
- package/esm/types/utility-types/reference.js +383 -0
- package/esm/types/utility-types/reference.js.map +1 -0
- package/esm/types/utility-types/refinement.d.ts +10 -0
- package/esm/types/utility-types/refinement.js +82 -0
- package/esm/types/utility-types/refinement.js.map +1 -0
- package/esm/types/utility-types/resilient.d.ts +18 -0
- package/esm/types/utility-types/resilient.js +118 -0
- package/esm/types/utility-types/resilient.js.map +1 -0
- package/esm/types/utility-types/snapshotProcessor.d.ts +63 -0
- package/esm/types/utility-types/snapshotProcessor.js +159 -0
- package/esm/types/utility-types/snapshotProcessor.js.map +1 -0
- package/esm/types/utility-types/union.d.ts +78 -0
- package/esm/types/utility-types/union.js +240 -0
- package/esm/types/utility-types/union.js.map +1 -0
- package/esm/utils.d.ts +230 -0
- package/esm/utils.js +449 -0
- package/esm/utils.js.map +1 -0
- package/package.json +23 -22
- package/dist/mobx-state-tree.js +0 -86
- package/dist/mobx-state-tree.js.map +0 -1
- package/dist/mobx-state-tree.module.js +0 -84
- package/dist/mobx-state-tree.module.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"on-action.js","sourceRoot":"","sources":["../../src/middlewares/on-action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAElC,OAAO,EAKL,aAAa,EACb,UAAU,EACV,aAAa,EACb,OAAO,EACP,SAAS,EACT,qBAAqB,EACrB,OAAO,EACP,IAAI,EACJ,2BAA2B,EAC3B,uBAAuB,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,OAAO,CAAC,IAAI;gBAClB,IAAI,EAAE,2BAA2B,CAAC,gBAAgB,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC;gBACvE,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,KAAa,EAAE,EAAE,CACjD,iBAAiB,CAAC,UAAU,EAAE,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CACxD;aACF,CAAA;YACD,IAAI,WAAW,EAAE,CAAC;gBAChB,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,CAAA;gBACzB,QAAQ,CAAC,IAAI,CAAC,CAAA;gBACd,OAAO,GAAG,CAAA;YACZ,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,IAAI,CAAC,CAAA;gBACd,OAAO,IAAI,CAAC,OAAO,CAAC,CAAA;YACtB,CAAC;QACH,CAAC;aAAM,CAAC;YACN,OAAO,IAAI,CAAC,OAAO,CAAC,CAAA;QACtB,CAAC;IACH,CAAC,CAAC,CAAA;AACJ,CAAC","sourcesContent":["import { runInAction } from \"mobx\"\n\nimport {\n type AnyNode,\n type IActionContext,\n type IAnyStateTreeNode,\n type IDisposer,\n addMiddleware,\n applyPatch,\n applySnapshot,\n asArray,\n assertArg,\n assertIsStateTreeNode,\n devMode,\n fail,\n getRelativePathBetweenNodes,\n getRunningActionContext,\n getStateTreeNode,\n getType,\n isArray,\n isPlainObject,\n isPrimitive,\n isProtected,\n isRoot,\n isStateTreeNode,\n tryResolve,\n warnError\n} from \"../internal.ts\"\n\nexport interface ISerializedActionCall {\n name: string\n path?: string\n args?: any[]\n}\n\nexport interface IActionRecorder {\n actions: ReadonlyArray<ISerializedActionCall>\n readonly recording: boolean\n stop(): void\n resume(): void\n replay(target: IAnyStateTreeNode): void\n}\n\nfunction serializeArgument(\n node: AnyNode,\n actionName: string,\n index: number,\n arg: any\n): any {\n if (arg instanceof Date) {\n return { $MST_DATE: arg.getTime() }\n }\n if (isPrimitive(arg)) {\n return arg\n }\n // We should not serialize MST nodes, even if we can, because we don't know if the receiving party can handle a raw snapshot instead of an\n // MST type instance. So if one wants to serialize a MST node that was pass in, either explitly pass: 1: an id, 2: a (relative) path, 3: a snapshot\n if (isStateTreeNode(arg)) {\n return serializeTheUnserializable(`[MSTNode: ${getType(arg).name}]`)\n }\n if (typeof arg === \"function\") {\n return serializeTheUnserializable(`[function]`)\n }\n if (typeof arg === \"object\" && !isPlainObject(arg) && !isArray(arg)) {\n return serializeTheUnserializable(\n `[object ${\n (arg && (arg as any).constructor && (arg as any).constructor.name) ||\n \"Complex Object\"\n }]`\n )\n }\n try {\n // Check if serializable, cycle free etc...\n // MWE: there must be a better way....\n JSON.stringify(arg) // or throws\n return arg\n } catch (e) {\n return serializeTheUnserializable(\"\" + e)\n }\n}\n\nfunction deserializeArgument(adm: AnyNode, value: any): any {\n if (value && typeof value === \"object\" && \"$MST_DATE\" in value) {\n return new Date(value[\"$MST_DATE\"])\n }\n return value\n}\n\nfunction serializeTheUnserializable(baseType: string) {\n return {\n $MST_UNSERIALIZABLE: true,\n type: baseType\n }\n}\n\n/**\n * Applies an action or a series of actions in a single MobX transaction.\n * Does not return any value\n * Takes an action description as produced by the `onAction` middleware.\n *\n * @param target\n * @param actions\n */\nexport function applyAction(\n target: IAnyStateTreeNode,\n actions: ISerializedActionCall | ISerializedActionCall[]\n): void {\n // check all arguments\n assertIsStateTreeNode(target, 1)\n assertArg(actions, a => typeof a === \"object\", \"object or array\", 2)\n\n runInAction(() => {\n asArray(actions).forEach(action => baseApplyAction(target, action))\n })\n}\n\nfunction baseApplyAction(\n target: IAnyStateTreeNode,\n action: ISerializedActionCall\n): any {\n const resolvedTarget = tryResolve(target, action.path || \"\")\n if (!resolvedTarget) {\n throw fail(`Invalid action path: ${action.path || \"\"}`)\n }\n const node = getStateTreeNode(resolvedTarget)\n\n // Reserved functions\n if (action.name === \"@APPLY_PATCHES\") {\n return applyPatch.call(null, resolvedTarget, action.args![0])\n }\n if (action.name === \"@APPLY_SNAPSHOT\") {\n return applySnapshot.call(null, resolvedTarget, action.args![0])\n }\n\n if (!(typeof resolvedTarget[action.name] === \"function\")) {\n throw fail(`Action '${action.name}' does not exist in '${node.path}'`)\n }\n return resolvedTarget[action.name](\n ...(action.args ? action.args.map(v => deserializeArgument(node, v)) : [])\n )\n}\n\n/**\n * Small abstraction around `onAction` and `applyAction`, attaches an action listener to a tree and records all the actions emitted.\n * Returns an recorder object with the following signature:\n *\n * Example:\n * ```ts\n * export interface IActionRecorder {\n * // the recorded actions\n * actions: ISerializedActionCall[]\n * // true if currently recording\n * recording: boolean\n * // stop recording actions\n * stop(): void\n * // resume recording actions\n * resume(): void\n * // apply all the recorded actions on the given object\n * replay(target: IAnyStateTreeNode): void\n * }\n * ```\n *\n * The optional filter function allows to skip recording certain actions.\n *\n * @param subject\n * @returns\n */\nexport function recordActions(\n subject: IAnyStateTreeNode,\n filter?: (\n action: ISerializedActionCall,\n actionContext: IActionContext | undefined\n ) => boolean\n): IActionRecorder {\n // check all arguments\n assertIsStateTreeNode(subject, 1)\n\n const actions: ISerializedActionCall[] = []\n const listener = (call: ISerializedActionCall) => {\n const recordThis = filter ? filter(call, getRunningActionContext()) : true\n if (recordThis) {\n actions.push(call)\n }\n }\n\n let disposer: IDisposer | undefined\n const recorder: IActionRecorder = {\n actions,\n get recording() {\n return !!disposer\n },\n stop() {\n if (disposer) {\n disposer()\n disposer = undefined\n }\n },\n resume() {\n if (disposer) {\n return\n }\n disposer = onAction(subject, listener)\n },\n replay(target) {\n applyAction(target, actions)\n }\n }\n\n recorder.resume()\n return recorder\n}\n\n/**\n * Registers a function that will be invoked for each action that is called on the provided model instance, or to any of its children.\n * See [actions](https://github.com/mobxjs/mobx-state-tree#actions) for more details. onAction events are emitted only for the outermost called action in the stack.\n * Action can also be intercepted by middleware using addMiddleware to change the function call before it will be run.\n *\n * Not all action arguments might be serializable. For unserializable arguments, a struct like `{ $MST_UNSERIALIZABLE: true, type: \"someType\" }` will be generated.\n * MST Nodes are considered non-serializable as well (they could be serialized as there snapshot, but it is uncertain whether an replaying party will be able to handle such a non-instantiated snapshot).\n * Rather, when using `onAction` middleware, one should consider in passing arguments which are 1: an id, 2: a (relative) path, or 3: a snapshot. Instead of a real MST node.\n *\n * Example:\n * ```ts\n * const Todo = types.model({\n * task: types.string\n * })\n *\n * const TodoStore = types.model({\n * todos: types.array(Todo)\n * }).actions(self => ({\n * add(todo) {\n * self.todos.push(todo);\n * }\n * }))\n *\n * const s = TodoStore.create({ todos: [] })\n *\n * let disposer = onAction(s, (call) => {\n * console.log(call);\n * })\n *\n * s.add({ task: \"Grab a coffee\" })\n * // Logs: { name: \"add\", path: \"\", args: [{ task: \"Grab a coffee\" }] }\n * ```\n *\n * @param target\n * @param listener\n * @param attachAfter (default false) fires the listener *after* the action has executed instead of before.\n * @returns\n */\nexport function onAction(\n target: IAnyStateTreeNode,\n listener: (call: ISerializedActionCall) => void,\n attachAfter = false\n): IDisposer {\n // check all arguments\n assertIsStateTreeNode(target, 1)\n if (devMode()) {\n if (!isRoot(target)) {\n warnError(\n \"Warning: Attaching onAction listeners to non root nodes is dangerous: No events will be emitted for actions initiated higher up in the tree.\"\n )\n }\n if (!isProtected(target)) {\n warnError(\n \"Warning: Attaching onAction listeners to non protected nodes is dangerous: No events will be emitted for direct modifications without action.\"\n )\n }\n }\n\n return addMiddleware(target, function handler(rawCall, next) {\n if (rawCall.type === \"action\" && rawCall.id === rawCall.rootId) {\n const sourceNode = getStateTreeNode(rawCall.context)\n const info = {\n name: rawCall.name,\n path: getRelativePathBetweenNodes(getStateTreeNode(target), sourceNode),\n args: rawCall.args.map((arg: any, index: number) =>\n serializeArgument(sourceNode, rawCall.name, index, arg)\n )\n }\n if (attachAfter) {\n const res = next(rawCall)\n listener(info)\n return res\n } else {\n listener(info)\n return next(rawCall)\n }\n } else {\n return next(rawCall)\n }\n })\n}\n"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type IArrayDidChange, type IArraySplice, type IArrayWillChange, type IArrayWillSplice, type IObservableArray } from "mobx";
|
|
2
|
+
import { ComplexType } from "../../core/type/type.ts";
|
|
3
|
+
import { type AnyNode, type AnyObjectNode, type ExtractCSTWithSTN, type IAnyType, type IChildNodesMap, type IHooksGetter, type IJsonPatch, type IType, type IValidationContext, type IValidationResult, TypeFlags } from "../../internal.ts";
|
|
4
|
+
/** @hidden */
|
|
5
|
+
export interface IMSTArray<IT extends IAnyType> extends IObservableArray<IT["Type"]> {
|
|
6
|
+
push(...items: IT["Type"][]): number;
|
|
7
|
+
push(...items: ExtractCSTWithSTN<IT>[]): number;
|
|
8
|
+
concat(...items: ConcatArray<IT["Type"]>[]): IT["Type"][];
|
|
9
|
+
concat(...items: ConcatArray<ExtractCSTWithSTN<IT>>[]): IT["Type"][];
|
|
10
|
+
concat(...items: (IT["Type"] | ConcatArray<IT["Type"]>)[]): IT["Type"][];
|
|
11
|
+
concat(...items: (ExtractCSTWithSTN<IT> | ConcatArray<ExtractCSTWithSTN<IT>>)[]): IT["Type"][];
|
|
12
|
+
splice(start: number, deleteCount?: number): IT["Type"][];
|
|
13
|
+
splice(start: number, deleteCount: number, ...items: IT["Type"][]): IT["Type"][];
|
|
14
|
+
splice(start: number, deleteCount: number, ...items: ExtractCSTWithSTN<IT>[]): IT["Type"][];
|
|
15
|
+
unshift(...items: IT["Type"][]): number;
|
|
16
|
+
unshift(...items: ExtractCSTWithSTN<IT>[]): number;
|
|
17
|
+
}
|
|
18
|
+
/** @hidden */
|
|
19
|
+
export interface IArrayType<IT extends IAnyType> extends IType<readonly IT["CreationType"][] | undefined, IT["SnapshotType"][], IMSTArray<IT>> {
|
|
20
|
+
hooks(hooks: IHooksGetter<IMSTArray<IAnyType>>): IArrayType<IT>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
* @hidden
|
|
25
|
+
*/
|
|
26
|
+
export declare class ArrayType<IT extends IAnyType> extends ComplexType<readonly IT["CreationType"][] | undefined, IT["SnapshotType"][], IMSTArray<IT>> {
|
|
27
|
+
private readonly _subType;
|
|
28
|
+
readonly flags = TypeFlags.Array;
|
|
29
|
+
private readonly hookInitializers;
|
|
30
|
+
constructor(name: string, _subType: IT, hookInitializers?: Array<IHooksGetter<IMSTArray<IT>>>);
|
|
31
|
+
hooks(hooks: IHooksGetter<IMSTArray<IT>>): ArrayType<IT>;
|
|
32
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"] | this["T"]): this["N"];
|
|
33
|
+
initializeChildNodes(objNode: this["N"], snapshot?: this["C"]): IChildNodesMap;
|
|
34
|
+
createNewInstance(childNodes: IChildNodesMap): this["T"];
|
|
35
|
+
finalizeNewInstance(node: this["N"], instance: this["T"]): void;
|
|
36
|
+
describe(): string;
|
|
37
|
+
getChildren(node: this["N"]): AnyNode[];
|
|
38
|
+
getChildNode(node: this["N"], key: string): AnyNode;
|
|
39
|
+
willChange(change: IArrayWillChange<AnyNode> | IArrayWillSplice<AnyNode>): IArrayWillChange<AnyNode> | IArrayWillSplice<AnyNode> | null;
|
|
40
|
+
getSnapshot(node: this["N"]): this["S"];
|
|
41
|
+
processInitialSnapshot(childNodes: IChildNodesMap): this["S"];
|
|
42
|
+
didChange(change: IArrayDidChange<AnyNode> | IArraySplice<AnyNode>): void;
|
|
43
|
+
applyPatchLocally(node: this["N"], subpath: string, patch: IJsonPatch): void;
|
|
44
|
+
applySnapshot(node: this["N"], snapshot: this["C"]): void;
|
|
45
|
+
getChildType(): IAnyType;
|
|
46
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
47
|
+
getDefaultSnapshot(): this["C"];
|
|
48
|
+
removeChild(node: this["N"], subpath: string): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
|
|
52
|
+
*
|
|
53
|
+
* This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)
|
|
54
|
+
*
|
|
55
|
+
* Example:
|
|
56
|
+
* ```ts
|
|
57
|
+
* const Todo = types.model({
|
|
58
|
+
* task: types.string
|
|
59
|
+
* })
|
|
60
|
+
*
|
|
61
|
+
* const TodoStore = types.model({
|
|
62
|
+
* todos: types.array(Todo)
|
|
63
|
+
* })
|
|
64
|
+
*
|
|
65
|
+
* const s = TodoStore.create({ todos: [] })
|
|
66
|
+
* unprotect(s) // needed to allow modifying outside of an action
|
|
67
|
+
* s.todos.push({ task: "Grab coffee" })
|
|
68
|
+
* console.log(s.todos[0]) // prints: "Grab coffee"
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @param subtype
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
export declare function array<IT extends IAnyType>(subtype: IT): IArrayType<IT>;
|
|
75
|
+
/**
|
|
76
|
+
* Returns if a given value represents an array type.
|
|
77
|
+
*
|
|
78
|
+
* @param type
|
|
79
|
+
* @returns `true` if the type is an array type.
|
|
80
|
+
*/
|
|
81
|
+
export declare function isArrayType<Items extends IAnyType = IAnyType>(type: IAnyType): type is IArrayType<Items>;
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { _getAdministration, action, intercept, observable, observe } from "mobx";
|
|
2
|
+
import { ComplexType } from "../../core/type/type.js";
|
|
3
|
+
import { EMPTY_ARRAY, EMPTY_OBJECT, ObjectNode, TypeFlags, addHiddenFinalProp, addHiddenWritableProp, assertIsType, convertChildNodesToArray, createActionInvoker, createObjectNode, devMode, fail, getContextForPath, getStateTreeNode, isArray, isNode, isPlainObject, isStateTreeNode, isType, mobxShallow, popContext, typeCheckFailure, typeCheckSuccess, typecheckInternal } from "../../internal.js";
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
* @hidden
|
|
7
|
+
*/
|
|
8
|
+
export class ArrayType extends ComplexType {
|
|
9
|
+
_subType;
|
|
10
|
+
flags = TypeFlags.Array;
|
|
11
|
+
hookInitializers = [];
|
|
12
|
+
constructor(name, _subType, hookInitializers = []) {
|
|
13
|
+
super(name);
|
|
14
|
+
this._subType = _subType;
|
|
15
|
+
this.hookInitializers = hookInitializers;
|
|
16
|
+
}
|
|
17
|
+
hooks(hooks) {
|
|
18
|
+
const hookInitializers = this.hookInitializers.length > 0
|
|
19
|
+
? this.hookInitializers.concat(hooks)
|
|
20
|
+
: [hooks];
|
|
21
|
+
return new ArrayType(this.name, this._subType, hookInitializers);
|
|
22
|
+
}
|
|
23
|
+
instantiate(parent, subpath, environment, initialValue) {
|
|
24
|
+
return createObjectNode(this, parent, subpath, environment, initialValue);
|
|
25
|
+
}
|
|
26
|
+
initializeChildNodes(objNode, snapshot = []) {
|
|
27
|
+
const subType = objNode.type._subType;
|
|
28
|
+
const result = {};
|
|
29
|
+
snapshot.forEach((item, index) => {
|
|
30
|
+
const subpath = "" + index;
|
|
31
|
+
result[subpath] = subType.instantiate(objNode, subpath, undefined, item);
|
|
32
|
+
});
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
createNewInstance(childNodes) {
|
|
36
|
+
const options = { ...mobxShallow, name: this.name };
|
|
37
|
+
return observable.array(convertChildNodesToArray(childNodes), options);
|
|
38
|
+
}
|
|
39
|
+
finalizeNewInstance(node, instance) {
|
|
40
|
+
_getAdministration(instance).dehancer = node.unbox;
|
|
41
|
+
const type = node.type;
|
|
42
|
+
type.hookInitializers.forEach(initializer => {
|
|
43
|
+
const hooks = initializer(instance);
|
|
44
|
+
Object.keys(hooks).forEach(name => {
|
|
45
|
+
const hook = hooks[name];
|
|
46
|
+
const actionInvoker = createActionInvoker(instance, name, hook);
|
|
47
|
+
(!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(instance, name, actionInvoker);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
intercept(instance, this.willChange);
|
|
51
|
+
observe(instance, this.didChange);
|
|
52
|
+
}
|
|
53
|
+
describe() {
|
|
54
|
+
return this.name;
|
|
55
|
+
}
|
|
56
|
+
getChildren(node) {
|
|
57
|
+
return node.storedValue.slice();
|
|
58
|
+
}
|
|
59
|
+
getChildNode(node, key) {
|
|
60
|
+
const index = Number(key);
|
|
61
|
+
if (index < node.storedValue.length) {
|
|
62
|
+
return node.storedValue[index];
|
|
63
|
+
}
|
|
64
|
+
throw fail("Not a child: " + key);
|
|
65
|
+
}
|
|
66
|
+
willChange(change) {
|
|
67
|
+
const node = getStateTreeNode(change.object);
|
|
68
|
+
node.assertWritable({ subpath: "" + change.index });
|
|
69
|
+
const subType = node.type._subType;
|
|
70
|
+
const childNodes = node.getChildren();
|
|
71
|
+
switch (change.type) {
|
|
72
|
+
case "update":
|
|
73
|
+
{
|
|
74
|
+
if (change.newValue === change.object[change.index]) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
const updatedNodes = reconcileArrayChildren(node, subType, [childNodes[change.index]], [change.newValue], [change.index]);
|
|
78
|
+
if (!updatedNodes) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
change.newValue = updatedNodes[0];
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
case "splice":
|
|
85
|
+
{
|
|
86
|
+
const { index, removedCount, added } = change;
|
|
87
|
+
const addedNodes = reconcileArrayChildren(node, subType, childNodes.slice(index, index + removedCount), added, added.map((_, i) => index + i));
|
|
88
|
+
if (!addedNodes) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
change.added = addedNodes;
|
|
92
|
+
// update paths of remaining items
|
|
93
|
+
for (let i = index + removedCount; i < childNodes.length; i++) {
|
|
94
|
+
childNodes[i].setParent(node, "" + (i + added.length - removedCount));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
return change;
|
|
100
|
+
}
|
|
101
|
+
getSnapshot(node) {
|
|
102
|
+
return node.getChildren().map(childNode => childNode.snapshot);
|
|
103
|
+
}
|
|
104
|
+
processInitialSnapshot(childNodes) {
|
|
105
|
+
const processed = [];
|
|
106
|
+
Object.keys(childNodes).forEach(key => {
|
|
107
|
+
processed.push(childNodes[key].getSnapshot());
|
|
108
|
+
});
|
|
109
|
+
return processed;
|
|
110
|
+
}
|
|
111
|
+
didChange(change) {
|
|
112
|
+
const node = getStateTreeNode(change.object);
|
|
113
|
+
switch (change.type) {
|
|
114
|
+
case "update":
|
|
115
|
+
return void node.emitPatch({
|
|
116
|
+
op: "replace",
|
|
117
|
+
path: "" + change.index,
|
|
118
|
+
value: change.newValue.snapshot,
|
|
119
|
+
oldValue: change.oldValue ? change.oldValue.snapshot : undefined
|
|
120
|
+
}, node);
|
|
121
|
+
case "splice":
|
|
122
|
+
for (let i = change.removedCount - 1; i >= 0; i--) {
|
|
123
|
+
node.emitPatch({
|
|
124
|
+
op: "remove",
|
|
125
|
+
path: "" + (change.index + i),
|
|
126
|
+
oldValue: change.removed[i].snapshot
|
|
127
|
+
}, node);
|
|
128
|
+
}
|
|
129
|
+
for (let i = 0; i < change.addedCount; i++) {
|
|
130
|
+
node.emitPatch({
|
|
131
|
+
op: "add",
|
|
132
|
+
path: "" + (change.index + i),
|
|
133
|
+
value: node.getChildNode("" + (change.index + i)).snapshot,
|
|
134
|
+
oldValue: undefined
|
|
135
|
+
}, node);
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
applyPatchLocally(node, subpath, patch) {
|
|
141
|
+
const target = node.storedValue;
|
|
142
|
+
const index = subpath === "-" ? target.length : Number(subpath);
|
|
143
|
+
switch (patch.op) {
|
|
144
|
+
case "replace":
|
|
145
|
+
target[index] = patch.value;
|
|
146
|
+
break;
|
|
147
|
+
case "add":
|
|
148
|
+
target.splice(index, 0, patch.value);
|
|
149
|
+
break;
|
|
150
|
+
case "remove":
|
|
151
|
+
target.splice(index, 1);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
applySnapshot(node, snapshot) {
|
|
156
|
+
typecheckInternal(this, snapshot);
|
|
157
|
+
const target = node.storedValue;
|
|
158
|
+
target.replace(snapshot);
|
|
159
|
+
}
|
|
160
|
+
getChildType() {
|
|
161
|
+
return this._subType;
|
|
162
|
+
}
|
|
163
|
+
isValidSnapshot(value, context) {
|
|
164
|
+
if (!isArray(value)) {
|
|
165
|
+
return typeCheckFailure(context, value, "Value is not an array");
|
|
166
|
+
}
|
|
167
|
+
for (let i = 0; i < value.length; i++) {
|
|
168
|
+
getContextForPath(context, "" + i, this._subType);
|
|
169
|
+
const errors = this._subType.validate(value[i], context);
|
|
170
|
+
popContext(context);
|
|
171
|
+
if (errors.length > 0) {
|
|
172
|
+
return errors;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return typeCheckSuccess();
|
|
176
|
+
}
|
|
177
|
+
getDefaultSnapshot() {
|
|
178
|
+
return EMPTY_ARRAY;
|
|
179
|
+
}
|
|
180
|
+
removeChild(node, subpath) {
|
|
181
|
+
node.storedValue.splice(Number(subpath), 1);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
ArrayType.prototype.applySnapshot = action(ArrayType.prototype.applySnapshot);
|
|
185
|
+
/**
|
|
186
|
+
* `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
|
|
187
|
+
*
|
|
188
|
+
* This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)
|
|
189
|
+
*
|
|
190
|
+
* Example:
|
|
191
|
+
* ```ts
|
|
192
|
+
* const Todo = types.model({
|
|
193
|
+
* task: types.string
|
|
194
|
+
* })
|
|
195
|
+
*
|
|
196
|
+
* const TodoStore = types.model({
|
|
197
|
+
* todos: types.array(Todo)
|
|
198
|
+
* })
|
|
199
|
+
*
|
|
200
|
+
* const s = TodoStore.create({ todos: [] })
|
|
201
|
+
* unprotect(s) // needed to allow modifying outside of an action
|
|
202
|
+
* s.todos.push({ task: "Grab coffee" })
|
|
203
|
+
* console.log(s.todos[0]) // prints: "Grab coffee"
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @param subtype
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
export function array(subtype) {
|
|
210
|
+
assertIsType(subtype, 1);
|
|
211
|
+
return new ArrayType(`${subtype.name}[]`, subtype);
|
|
212
|
+
}
|
|
213
|
+
function reconcileArrayChildren(parent, childType, oldNodes, newValues, newPaths) {
|
|
214
|
+
let nothingChanged = true;
|
|
215
|
+
for (let i = 0;; i++) {
|
|
216
|
+
const hasNewNode = i <= newValues.length - 1;
|
|
217
|
+
const oldNode = oldNodes[i];
|
|
218
|
+
let newValue = hasNewNode ? newValues[i] : undefined;
|
|
219
|
+
const newPath = "" + newPaths[i];
|
|
220
|
+
// for some reason, instead of newValue we got a node, fallback to the storedValue
|
|
221
|
+
// TODO: https://github.com/mobxjs/mobx-state-tree/issues/340#issuecomment-325581681
|
|
222
|
+
if (isNode(newValue)) {
|
|
223
|
+
newValue = newValue.storedValue;
|
|
224
|
+
}
|
|
225
|
+
if (!oldNode && !hasNewNode) {
|
|
226
|
+
// both are empty, end
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
else if (!hasNewNode) {
|
|
230
|
+
// new one does not exists
|
|
231
|
+
nothingChanged = false;
|
|
232
|
+
oldNodes.splice(i, 1);
|
|
233
|
+
if (oldNode instanceof ObjectNode) {
|
|
234
|
+
// since it is going to be returned by pop/splice/shift better create it before killing it
|
|
235
|
+
// so it doesn't end up in an undead state
|
|
236
|
+
oldNode.createObservableInstanceIfNeeded();
|
|
237
|
+
}
|
|
238
|
+
oldNode.die();
|
|
239
|
+
i--;
|
|
240
|
+
}
|
|
241
|
+
else if (!oldNode) {
|
|
242
|
+
// there is no old node, create it
|
|
243
|
+
// check if already belongs to the same parent. if so, avoid pushing item in. only swapping can occur.
|
|
244
|
+
if (isStateTreeNode(newValue) &&
|
|
245
|
+
getStateTreeNode(newValue).parent === parent) {
|
|
246
|
+
// this node is owned by this parent, but not in the reconcilable set, so it must be double
|
|
247
|
+
throw fail(`Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '${parent.path}/${newPath}', but it lives already at '${getStateTreeNode(newValue).path}'`);
|
|
248
|
+
}
|
|
249
|
+
nothingChanged = false;
|
|
250
|
+
const newNode = valueAsNode(childType, parent, newPath, newValue);
|
|
251
|
+
oldNodes.splice(i, 0, newNode);
|
|
252
|
+
}
|
|
253
|
+
else if (areSame(oldNode, newValue)) {
|
|
254
|
+
// both are the same, reconcile
|
|
255
|
+
oldNodes[i] = valueAsNode(childType, parent, newPath, newValue, oldNode);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
// nothing to do, try to reorder
|
|
259
|
+
let oldMatch = undefined;
|
|
260
|
+
// find a possible candidate to reuse
|
|
261
|
+
for (let j = i; j < oldNodes.length; j++) {
|
|
262
|
+
if (areSame(oldNodes[j], newValue)) {
|
|
263
|
+
oldMatch = oldNodes.splice(j, 1)[0];
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
nothingChanged = false;
|
|
268
|
+
const newNode = valueAsNode(childType, parent, newPath, newValue, oldMatch);
|
|
269
|
+
oldNodes.splice(i, 0, newNode);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return nothingChanged ? null : oldNodes;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Convert a value to a node at given parent and subpath. Attempts to reuse old node if possible and given.
|
|
276
|
+
*/
|
|
277
|
+
function valueAsNode(childType, parent, subpath, newValue, oldNode) {
|
|
278
|
+
// ensure the value is valid-ish
|
|
279
|
+
typecheckInternal(childType, newValue);
|
|
280
|
+
function getNewNode() {
|
|
281
|
+
// the new value has a MST node
|
|
282
|
+
if (isStateTreeNode(newValue)) {
|
|
283
|
+
const childNode = getStateTreeNode(newValue);
|
|
284
|
+
childNode.assertAlive(EMPTY_OBJECT);
|
|
285
|
+
// the node lives here
|
|
286
|
+
if (childNode.parent !== null && childNode.parent === parent) {
|
|
287
|
+
childNode.setParent(parent, subpath);
|
|
288
|
+
return childNode;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// there is old node and new one is a value/snapshot
|
|
292
|
+
if (oldNode) {
|
|
293
|
+
return childType.reconcile(oldNode, newValue, parent, subpath);
|
|
294
|
+
}
|
|
295
|
+
// nothing to do, create from scratch
|
|
296
|
+
return childType.instantiate(parent, subpath, undefined, newValue);
|
|
297
|
+
}
|
|
298
|
+
const newNode = getNewNode();
|
|
299
|
+
if (oldNode && oldNode !== newNode) {
|
|
300
|
+
if (oldNode instanceof ObjectNode) {
|
|
301
|
+
// since it is going to be returned by pop/splice/shift better create it before killing it
|
|
302
|
+
// so it doesn't end up in an undead state
|
|
303
|
+
oldNode.createObservableInstanceIfNeeded();
|
|
304
|
+
}
|
|
305
|
+
oldNode.die();
|
|
306
|
+
}
|
|
307
|
+
return newNode;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Check if a node holds a value.
|
|
311
|
+
*/
|
|
312
|
+
function areSame(oldNode, newValue) {
|
|
313
|
+
// never consider dead old nodes for reconciliation
|
|
314
|
+
if (!oldNode.isAlive) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
// the new value has the same node
|
|
318
|
+
if (isStateTreeNode(newValue)) {
|
|
319
|
+
const newNode = getStateTreeNode(newValue);
|
|
320
|
+
return newNode.isAlive && newNode === oldNode;
|
|
321
|
+
}
|
|
322
|
+
// the provided value is the snapshot of the old node
|
|
323
|
+
if (oldNode.snapshot === newValue) {
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
// Non object nodes don't get reconciled
|
|
327
|
+
if (!(oldNode instanceof ObjectNode)) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
const oldNodeType = oldNode.getReconciliationType();
|
|
331
|
+
// new value is a snapshot with the correct identifier
|
|
332
|
+
return (oldNode.identifier !== null &&
|
|
333
|
+
oldNode.identifierAttribute &&
|
|
334
|
+
isPlainObject(newValue) &&
|
|
335
|
+
oldNodeType.is(newValue) &&
|
|
336
|
+
oldNodeType.isMatchingSnapshotId(oldNode, newValue));
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Returns if a given value represents an array type.
|
|
340
|
+
*
|
|
341
|
+
* @param type
|
|
342
|
+
* @returns `true` if the type is an array type.
|
|
343
|
+
*/
|
|
344
|
+
export function isArrayType(type) {
|
|
345
|
+
return isType(type) && (type.flags & TypeFlags.Array) > 0;
|
|
346
|
+
}
|
|
347
|
+
//# sourceMappingURL=array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.js","sourceRoot":"","sources":["../../../src/types/complex-types/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,kBAAkB,EAClB,MAAM,EACN,SAAS,EACT,UAAU,EACV,OAAO,EACR,MAAM,MAAM,CAAA;AAEb,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAGL,WAAW,EACX,YAAY,EAWZ,UAAU,EACV,SAAS,EACT,kBAAkB,EAClB,qBAAqB,EACrB,YAAY,EACZ,wBAAwB,EACxB,mBAAmB,EACnB,gBAAgB,EAChB,OAAO,EACP,IAAI,EACJ,iBAAiB,EACjB,gBAAgB,EAChB,OAAO,EACP,MAAM,EACN,aAAa,EACb,eAAe,EACf,MAAM,EACN,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,mBAAmB,CAAA;AA2C1B;;;GAGG;AACH,MAAM,OAAO,SAA+B,SAAQ,WAInD;IAKoB;IAJV,KAAK,GAAG,SAAS,CAAC,KAAK,CAAA;IACf,gBAAgB,GAAuC,EAAE,CAAA;IAC1E,YACE,IAAY,EACK,QAAY,EAC7B,mBAAuD,EAAE;QAEzD,KAAK,CAAC,IAAI,CAAC,CAAA;QAHM,aAAQ,GAAR,QAAQ,CAAI;QAI7B,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAA;IAC1C,CAAC;I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;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,IAAc;IAEd,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;AAC3D,CAAC","sourcesContent":["import {\n type IArrayDidChange,\n type IArraySplice,\n type IArrayWillChange,\n type IArrayWillSplice,\n type IObservableArray,\n _getAdministration,\n action,\n intercept,\n observable,\n observe\n} from \"mobx\"\n\nimport { ComplexType } from \"../../core/type/type.ts\"\nimport {\n type AnyNode,\n type AnyObjectNode,\n EMPTY_ARRAY,\n EMPTY_OBJECT,\n type ExtractCSTWithSTN,\n type IAnyStateTreeNode,\n type IAnyType,\n type IChildNodesMap,\n type IHooksGetter,\n type IJsonPatch,\n type IStateTreeNode,\n type IType,\n type IValidationContext,\n type IValidationResult,\n ObjectNode,\n TypeFlags,\n addHiddenFinalProp,\n addHiddenWritableProp,\n assertIsType,\n convertChildNodesToArray,\n createActionInvoker,\n createObjectNode,\n devMode,\n fail,\n getContextForPath,\n getStateTreeNode,\n isArray,\n isNode,\n isPlainObject,\n isStateTreeNode,\n isType,\n mobxShallow,\n popContext,\n typeCheckFailure,\n typeCheckSuccess,\n typecheckInternal\n} from \"../../internal.ts\"\n\n/** @hidden */\nexport interface IMSTArray<IT extends IAnyType> extends IObservableArray<\n IT[\"Type\"]\n> {\n // needs to be split or else it will complain about not being compatible with the array interface\n push(...items: IT[\"Type\"][]): number\n push(...items: ExtractCSTWithSTN<IT>[]): number\n\n concat(...items: ConcatArray<IT[\"Type\"]>[]): IT[\"Type\"][]\n concat(...items: ConcatArray<ExtractCSTWithSTN<IT>>[]): IT[\"Type\"][]\n\n concat(...items: (IT[\"Type\"] | ConcatArray<IT[\"Type\"]>)[]): IT[\"Type\"][]\n concat(\n ...items: (ExtractCSTWithSTN<IT> | ConcatArray<ExtractCSTWithSTN<IT>>)[]\n ): IT[\"Type\"][]\n\n splice(start: number, deleteCount?: number): IT[\"Type\"][]\n splice(\n start: number,\n deleteCount: number,\n ...items: IT[\"Type\"][]\n ): IT[\"Type\"][]\n splice(\n start: number,\n deleteCount: number,\n ...items: ExtractCSTWithSTN<IT>[]\n ): IT[\"Type\"][]\n\n unshift(...items: IT[\"Type\"][]): number\n unshift(...items: ExtractCSTWithSTN<IT>[]): number\n}\n\n/** @hidden */\nexport interface IArrayType<IT extends IAnyType> extends IType<\n readonly IT[\"CreationType\"][] | undefined,\n IT[\"SnapshotType\"][],\n IMSTArray<IT>\n> {\n hooks(hooks: IHooksGetter<IMSTArray<IAnyType>>): IArrayType<IT>\n}\n\n/**\n * @internal\n * @hidden\n */\nexport class ArrayType<IT extends IAnyType> extends ComplexType<\n readonly IT[\"CreationType\"][] | undefined,\n IT[\"SnapshotType\"][],\n IMSTArray<IT>\n> {\n readonly flags = TypeFlags.Array\n private readonly hookInitializers: Array<IHooksGetter<IMSTArray<IT>>> = []\n constructor(\n name: string,\n private readonly _subType: IT,\n hookInitializers: Array<IHooksGetter<IMSTArray<IT>>> = []\n ) {\n super(name)\n this.hookInitializers = hookInitializers\n }\n\n hooks(hooks: IHooksGetter<IMSTArray<IT>>) {\n const hookInitializers =\n this.hookInitializers.length > 0\n ? this.hookInitializers.concat(hooks)\n : [hooks]\n return new ArrayType(this.name, this._subType, hookInitializers)\n }\n\n instantiate(\n parent: AnyObjectNode | null,\n subpath: string,\n environment: any,\n initialValue: this[\"C\"] | this[\"T\"]\n ): this[\"N\"] {\n return createObjectNode(this, parent, subpath, environment, initialValue)\n }\n\n initializeChildNodes(\n objNode: this[\"N\"],\n snapshot: this[\"C\"] = []\n ): IChildNodesMap {\n const subType = (objNode.type as this)._subType\n const result: IChildNodesMap = {}\n snapshot.forEach((item, index) => {\n const subpath = \"\" + index\n result[subpath] = subType.instantiate(objNode, subpath, undefined, item)\n })\n return result\n }\n\n createNewInstance(childNodes: IChildNodesMap): this[\"T\"] {\n const options = { ...mobxShallow, name: this.name }\n return observable.array(\n convertChildNodesToArray(childNodes),\n options\n ) as this[\"T\"]\n }\n\n finalizeNewInstance(node: this[\"N\"], instance: this[\"T\"]): void {\n _getAdministration(instance).dehancer = node.unbox\n\n const type = node.type as this\n type.hookInitializers.forEach(initializer => {\n const hooks = initializer(instance)\n Object.keys(hooks).forEach(name => {\n const hook = hooks[name as keyof typeof hooks]!\n const actionInvoker = createActionInvoker(\n instance as IAnyStateTreeNode,\n name,\n hook\n )\n ;(!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(\n instance,\n name,\n actionInvoker\n )\n })\n })\n\n intercept(instance as IObservableArray<AnyNode>, this.willChange)\n observe(instance as IObservableArray<AnyNode>, this.didChange)\n }\n\n describe() {\n return this.name\n }\n\n getChildren(node: this[\"N\"]): AnyNode[] {\n return node.storedValue.slice()\n }\n\n getChildNode(node: this[\"N\"], key: string): AnyNode {\n const index = Number(key)\n if (index < node.storedValue.length) {\n return node.storedValue[index]\n }\n throw fail(\"Not a child: \" + key)\n }\n\n willChange(\n change: IArrayWillChange<AnyNode> | IArrayWillSplice<AnyNode>\n ): IArrayWillChange<AnyNode> | IArrayWillSplice<AnyNode> | null {\n const node = getStateTreeNode(change.object as IStateTreeNode<this>)\n node.assertWritable({ subpath: \"\" + change.index })\n const subType = (node.type as this)._subType\n const childNodes = node.getChildren()\n\n switch (change.type) {\n case \"update\":\n {\n if (change.newValue === change.object[change.index]) {\n return null\n }\n\n const updatedNodes = reconcileArrayChildren(\n node,\n subType,\n [childNodes[change.index]],\n [change.newValue],\n [change.index]\n )\n if (!updatedNodes) {\n return null\n }\n change.newValue = updatedNodes[0]\n }\n break\n case \"splice\":\n {\n const { index, removedCount, added } = change\n\n const addedNodes = reconcileArrayChildren(\n node,\n subType,\n childNodes.slice(index, index + removedCount),\n added,\n added.map((_, i) => index + i)\n )\n if (!addedNodes) {\n return null\n }\n change.added = addedNodes\n\n // update paths of remaining items\n for (let i = index + removedCount; i < childNodes.length; i++) {\n childNodes[i].setParent(\n node,\n \"\" + (i + added.length - removedCount)\n )\n }\n }\n break\n }\n return change\n }\n\n getSnapshot(node: this[\"N\"]): this[\"S\"] {\n return node.getChildren().map(childNode => childNode.snapshot)\n }\n\n processInitialSnapshot(childNodes: IChildNodesMap): this[\"S\"] {\n const processed: this[\"S\"] = []\n Object.keys(childNodes).forEach(key => {\n processed.push(childNodes[key].getSnapshot())\n })\n return processed\n }\n\n didChange(change: IArrayDidChange<AnyNode> | IArraySplice<AnyNode>): void {\n const node = getStateTreeNode(change.object as IAnyStateTreeNode)\n switch (change.type) {\n case \"update\":\n return void node.emitPatch(\n {\n op: \"replace\",\n path: \"\" + change.index,\n value: change.newValue.snapshot,\n oldValue: change.oldValue ? change.oldValue.snapshot : undefined\n },\n node\n )\n case \"splice\":\n for (let i = change.removedCount - 1; i >= 0; i--) {\n node.emitPatch(\n {\n op: \"remove\",\n path: \"\" + (change.index + i),\n oldValue: change.removed[i].snapshot\n },\n node\n )\n }\n for (let i = 0; i < change.addedCount; i++) {\n node.emitPatch(\n {\n op: \"add\",\n path: \"\" + (change.index + i),\n value: node.getChildNode(\"\" + (change.index + i)).snapshot,\n oldValue: undefined\n },\n node\n )\n }\n return\n }\n }\n\n applyPatchLocally(node: this[\"N\"], subpath: string, patch: IJsonPatch): void {\n const target = node.storedValue\n const index = subpath === \"-\" ? target.length : Number(subpath)\n switch (patch.op) {\n case \"replace\":\n target[index] = patch.value\n break\n case \"add\":\n target.splice(index, 0, patch.value)\n break\n case \"remove\":\n target.splice(index, 1)\n break\n }\n }\n\n applySnapshot(node: this[\"N\"], snapshot: this[\"C\"]): void {\n typecheckInternal(this, snapshot)\n const target = node.storedValue\n target.replace(snapshot as any)\n }\n\n getChildType(): IAnyType {\n return this._subType\n }\n\n isValidSnapshot(\n value: this[\"C\"],\n context: IValidationContext\n ): IValidationResult {\n if (!isArray(value)) {\n return typeCheckFailure(context, value, \"Value is not an array\")\n }\n\n for (let i = 0; i < value.length; i++) {\n getContextForPath(context, \"\" + i, this._subType)\n const errors = this._subType.validate(value[i], context)\n popContext(context)\n if (errors.length > 0) {\n return errors\n }\n }\n return typeCheckSuccess()\n }\n\n getDefaultSnapshot(): this[\"C\"] {\n return EMPTY_ARRAY as this[\"C\"]\n }\n\n removeChild(node: this[\"N\"], subpath: string) {\n node.storedValue.splice(Number(subpath), 1)\n }\n}\nArrayType.prototype.applySnapshot = action(ArrayType.prototype.applySnapshot)\n\n/**\n * `types.array` - Creates an index based collection type who's children are all of a uniform declared type.\n *\n * This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)\n *\n * Example:\n * ```ts\n * const Todo = types.model({\n * task: types.string\n * })\n *\n * const TodoStore = types.model({\n * todos: types.array(Todo)\n * })\n *\n * const s = TodoStore.create({ todos: [] })\n * unprotect(s) // needed to allow modifying outside of an action\n * s.todos.push({ task: \"Grab coffee\" })\n * console.log(s.todos[0]) // prints: \"Grab coffee\"\n * ```\n *\n * @param subtype\n * @returns\n */\nexport function array<IT extends IAnyType>(subtype: IT): IArrayType<IT> {\n assertIsType(subtype, 1)\n return new ArrayType<IT>(`${subtype.name}[]`, subtype)\n}\n\nfunction reconcileArrayChildren<TT>(\n parent: AnyObjectNode,\n childType: IType<any, any, TT>,\n oldNodes: AnyNode[],\n newValues: TT[],\n newPaths: (string | number)[]\n): AnyNode[] | null {\n let nothingChanged = true\n\n for (let i = 0; ; i++) {\n const hasNewNode = i <= newValues.length - 1\n const oldNode = oldNodes[i]\n let newValue = hasNewNode ? newValues[i] : undefined\n const newPath = \"\" + newPaths[i]\n\n // for some reason, instead of newValue we got a node, fallback to the storedValue\n // TODO: https://github.com/mobxjs/mobx-state-tree/issues/340#issuecomment-325581681\n if (isNode(newValue)) {\n newValue = newValue.storedValue\n }\n\n if (!oldNode && !hasNewNode) {\n // both are empty, end\n break\n } else if (!hasNewNode) {\n // new one does not exists\n nothingChanged = false\n oldNodes.splice(i, 1)\n if (oldNode instanceof ObjectNode) {\n // since it is going to be returned by pop/splice/shift better create it before killing it\n // so it doesn't end up in an undead state\n oldNode.createObservableInstanceIfNeeded()\n }\n oldNode.die()\n i--\n } else if (!oldNode) {\n // there is no old node, create it\n // check if already belongs to the same parent. if so, avoid pushing item in. only swapping can occur.\n if (\n isStateTreeNode(newValue) &&\n getStateTreeNode(newValue).parent === parent\n ) {\n // this node is owned by this parent, but not in the reconcilable set, so it must be double\n throw fail(\n `Cannot add an object to a state tree if it is already part of the same or another state tree. Tried to assign an object to '${\n parent.path\n }/${newPath}', but it lives already at '${getStateTreeNode(newValue).path}'`\n )\n }\n nothingChanged = false\n const newNode = valueAsNode(childType, parent, newPath, newValue)\n oldNodes.splice(i, 0, newNode)\n } else if (areSame(oldNode, newValue)) {\n // both are the same, reconcile\n oldNodes[i] = valueAsNode(childType, parent, newPath, newValue, oldNode)\n } else {\n // nothing to do, try to reorder\n let oldMatch = undefined\n\n // find a possible candidate to reuse\n for (let j = i; j < oldNodes.length; j++) {\n if (areSame(oldNodes[j], newValue)) {\n oldMatch = oldNodes.splice(j, 1)[0]\n break\n }\n }\n\n nothingChanged = false\n const newNode = valueAsNode(\n childType,\n parent,\n newPath,\n newValue,\n oldMatch\n )\n oldNodes.splice(i, 0, newNode)\n }\n }\n\n return nothingChanged ? null : oldNodes\n}\n\n/**\n * Convert a value to a node at given parent and subpath. Attempts to reuse old node if possible and given.\n */\nfunction valueAsNode(\n childType: IAnyType,\n parent: AnyObjectNode,\n subpath: string,\n newValue: any,\n oldNode?: AnyNode\n) {\n // ensure the value is valid-ish\n typecheckInternal(childType, newValue)\n\n function getNewNode() {\n // the new value has a MST node\n if (isStateTreeNode(newValue)) {\n const childNode = getStateTreeNode(newValue)\n childNode.assertAlive(EMPTY_OBJECT)\n\n // the node lives here\n if (childNode.parent !== null && childNode.parent === parent) {\n childNode.setParent(parent, subpath)\n return childNode\n }\n }\n // there is old node and new one is a value/snapshot\n if (oldNode) {\n return childType.reconcile(oldNode, newValue, parent, subpath)\n }\n\n // nothing to do, create from scratch\n return childType.instantiate(parent, subpath, undefined, newValue)\n }\n\n const newNode = getNewNode()\n if (oldNode && oldNode !== newNode) {\n if (oldNode instanceof ObjectNode) {\n // since it is going to be returned by pop/splice/shift better create it before killing it\n // so it doesn't end up in an undead state\n oldNode.createObservableInstanceIfNeeded()\n }\n oldNode.die()\n }\n return newNode\n}\n\n/**\n * Check if a node holds a value.\n */\nfunction areSame(oldNode: AnyNode, newValue: any) {\n // never consider dead old nodes for reconciliation\n if (!oldNode.isAlive) {\n return false\n }\n\n // the new value has the same node\n if (isStateTreeNode(newValue)) {\n const newNode = getStateTreeNode(newValue)\n return newNode.isAlive && newNode === oldNode\n }\n\n // the provided value is the snapshot of the old node\n if (oldNode.snapshot === newValue) {\n return true\n }\n\n // Non object nodes don't get reconciled\n if (!(oldNode instanceof ObjectNode)) {\n return false\n }\n\n const oldNodeType = oldNode.getReconciliationType()\n // new value is a snapshot with the correct identifier\n return (\n oldNode.identifier !== null &&\n oldNode.identifierAttribute &&\n isPlainObject(newValue) &&\n oldNodeType.is(newValue) &&\n (oldNodeType as any).isMatchingSnapshotId(oldNode, newValue)\n )\n}\n\n/**\n * Returns if a given value represents an array type.\n *\n * @param type\n * @returns `true` if the type is an array type.\n */\nexport function isArrayType<Items extends IAnyType = IAnyType>(\n type: IAnyType\n): type is IArrayType<Items> {\n return isType(type) && (type.flags & TypeFlags.Array) > 0\n}\n"]}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type IInterceptor, type IKeyValueMap, type IMapDidChange, type IMapWillChange, type Lambda, ObservableMap } from "mobx";
|
|
2
|
+
import { ComplexType } from "../../core/type/type.ts";
|
|
3
|
+
import { type AnyNode, type AnyObjectNode, type ExtractCSTWithSTN, type IAnyType, type IChildNodesMap, type IHooksGetter, type IJsonPatch, type IType, type IValidationContext, type IValidationResult, TypeFlags } from "../../internal.ts";
|
|
4
|
+
/** @hidden */
|
|
5
|
+
export interface IMapType<IT extends IAnyType> extends IType<IKeyValueMap<IT["CreationType"]> | undefined, IKeyValueMap<IT["SnapshotType"]>, IMSTMap<IT>> {
|
|
6
|
+
hooks(hooks: IHooksGetter<IMSTMap<IT>>): IMapType<IT>;
|
|
7
|
+
}
|
|
8
|
+
/** @hidden */
|
|
9
|
+
export interface IMSTMap<IT extends IAnyType> {
|
|
10
|
+
clear(): void;
|
|
11
|
+
delete(key: string): boolean;
|
|
12
|
+
forEach(callbackfn: (value: IT["Type"], key: string | number, map: this) => void, thisArg?: any): void;
|
|
13
|
+
get(key: string | number): IT["Type"] | undefined;
|
|
14
|
+
has(key: string | number): boolean;
|
|
15
|
+
set(key: string | number, value: ExtractCSTWithSTN<IT>): this;
|
|
16
|
+
readonly size: number;
|
|
17
|
+
put(value: ExtractCSTWithSTN<IT>): IT["Type"];
|
|
18
|
+
keys(): IterableIterator<string>;
|
|
19
|
+
values(): IterableIterator<IT["Type"]>;
|
|
20
|
+
entries(): IterableIterator<[string, IT["Type"]]>;
|
|
21
|
+
[Symbol.iterator](): IterableIterator<[string, IT["Type"]]>;
|
|
22
|
+
/** Merge another object into this map, returns self. */
|
|
23
|
+
merge(other: IMSTMap<IType<any, any, IT["TypeWithoutSTN"]>> | IKeyValueMap<ExtractCSTWithSTN<IT>> | any): this;
|
|
24
|
+
replace(values: IMSTMap<IType<any, any, IT["TypeWithoutSTN"]>> | IKeyValueMap<ExtractCSTWithSTN<IT>> | any): this;
|
|
25
|
+
toJSON(): IKeyValueMap<IT["SnapshotType"]>;
|
|
26
|
+
toString(): string;
|
|
27
|
+
[Symbol.toStringTag]: "Map";
|
|
28
|
+
/**
|
|
29
|
+
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
|
|
30
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
|
|
31
|
+
* for callback details
|
|
32
|
+
*/
|
|
33
|
+
observe(listener: (changes: IMapDidChange<string, IT["Type"]>) => void, fireImmediately?: boolean): Lambda;
|
|
34
|
+
intercept(handler: IInterceptor<IMapWillChange<string, IT["Type"]>>): Lambda;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
* @hidden
|
|
39
|
+
*/
|
|
40
|
+
export declare enum MapIdentifierMode {
|
|
41
|
+
UNKNOWN = 0,
|
|
42
|
+
YES = 1,
|
|
43
|
+
NO = 2
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
* @hidden
|
|
48
|
+
*/
|
|
49
|
+
export declare class MapType<IT extends IAnyType> extends ComplexType<IKeyValueMap<IT["CreationType"]> | undefined, IKeyValueMap<IT["SnapshotType"]>, IMSTMap<IT>> {
|
|
50
|
+
private readonly _subType;
|
|
51
|
+
identifierMode: MapIdentifierMode;
|
|
52
|
+
mapIdentifierAttribute: string | undefined;
|
|
53
|
+
readonly flags = TypeFlags.Map;
|
|
54
|
+
private readonly hookInitializers;
|
|
55
|
+
constructor(name: string, _subType: IAnyType, hookInitializers?: Array<IHooksGetter<IMSTMap<IT>>>);
|
|
56
|
+
hooks(hooks: IHooksGetter<IMSTMap<IT>>): MapType<IT>;
|
|
57
|
+
instantiate(parent: AnyObjectNode | null, subpath: string, environment: any, initialValue: this["C"] | this["T"]): this["N"];
|
|
58
|
+
private _determineIdentifierMode;
|
|
59
|
+
initializeChildNodes(objNode: this["N"], initialSnapshot?: this["C"]): IChildNodesMap;
|
|
60
|
+
createNewInstance(childNodes: IChildNodesMap): this["T"];
|
|
61
|
+
finalizeNewInstance(node: this["N"], instance: ObservableMap<string, any>): void;
|
|
62
|
+
describe(): string;
|
|
63
|
+
getChildren(node: this["N"]): ReadonlyArray<AnyNode>;
|
|
64
|
+
getChildNode(node: this["N"], key: string): AnyNode;
|
|
65
|
+
willChange(change: IMapWillChange<string, AnyNode>): IMapWillChange<string, AnyNode> | null;
|
|
66
|
+
private processIdentifier;
|
|
67
|
+
getSnapshot(node: this["N"]): this["S"];
|
|
68
|
+
processInitialSnapshot(childNodes: IChildNodesMap): this["S"];
|
|
69
|
+
didChange(change: IMapDidChange<string, AnyNode>): void;
|
|
70
|
+
applyPatchLocally(node: this["N"], subpath: string, patch: IJsonPatch): void;
|
|
71
|
+
applySnapshot(node: this["N"], snapshot: this["C"]): void;
|
|
72
|
+
getChildType(): IAnyType;
|
|
73
|
+
isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult;
|
|
74
|
+
getDefaultSnapshot(): this["C"];
|
|
75
|
+
removeChild(node: this["N"], subpath: string): void;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
|
|
79
|
+
* If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
|
|
80
|
+
*
|
|
81
|
+
* This type will always produce [observable maps](https://mobx.js.org/api.html#observablemap)
|
|
82
|
+
*
|
|
83
|
+
* Example:
|
|
84
|
+
* ```ts
|
|
85
|
+
* const Todo = types.model({
|
|
86
|
+
* id: types.identifier,
|
|
87
|
+
* task: types.string
|
|
88
|
+
* })
|
|
89
|
+
*
|
|
90
|
+
* const TodoStore = types.model({
|
|
91
|
+
* todos: types.map(Todo)
|
|
92
|
+
* })
|
|
93
|
+
*
|
|
94
|
+
* const s = TodoStore.create({ todos: {} })
|
|
95
|
+
* unprotect(s)
|
|
96
|
+
* s.todos.set(17, { task: "Grab coffee", id: 17 })
|
|
97
|
+
* s.todos.put({ task: "Grab cookie", id: 18 }) // put will infer key from the identifier
|
|
98
|
+
* console.log(s.todos.get(17).task) // prints: "Grab coffee"
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @param subtype
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
export declare function map<IT extends IAnyType>(subtype: IT): IMapType<IT>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns if a given value represents a map type.
|
|
107
|
+
*
|
|
108
|
+
* @param type
|
|
109
|
+
* @returns `true` if it is a map type.
|
|
110
|
+
*/
|
|
111
|
+
export declare function isMapType<Items extends IAnyType = IAnyType>(type: IAnyType): type is IMapType<Items>;
|