mobx-keystone 0.67.4 → 0.68.1-alpha
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/CHANGELOG.md +8 -0
- package/dist/_virtual/_tslib.mjs +12 -0
- package/dist/mobx-keystone.umd.js +5339 -5261
- package/dist/model/utils.d.ts +1 -1
- package/dist/node_modules/fast-deep-equal/es6/index.mjs +68 -0
- package/dist/node_modules/nanoid/non-secure/index.mjs +11 -0
- package/dist/snapshot/index.d.ts +0 -8
- package/dist/{dataModel/_BaseDataModel.d.ts → snapshot/registerDefaultReconcilers.d.ts} +0 -0
- package/dist/{model/_BaseModel.d.ts → snapshot/registerDefaultSnapshotters.d.ts} +0 -0
- package/dist/src/action/actionDecoratorUtils.mjs +25 -0
- package/dist/src/action/applyAction.mjs +54 -0
- package/dist/src/action/applyDelete.mjs +20 -0
- package/dist/src/action/applyMethodCall.mjs +19 -0
- package/dist/src/action/applySet.mjs +25 -0
- package/dist/src/action/builtInActions.mjs +15 -0
- package/dist/src/action/context.mjs +22 -0
- package/dist/src/action/hookActions.mjs +13 -0
- package/dist/src/action/isModelAction.mjs +6 -0
- package/dist/src/action/middleware.mjs +84 -0
- package/dist/src/action/modelAction.mjs +34 -0
- package/dist/src/action/modelFlow.mjs +251 -0
- package/dist/src/action/pendingActions.mjs +30 -0
- package/dist/src/action/protection.mjs +19 -0
- package/dist/src/action/runUnprotected.mjs +24 -0
- package/dist/src/action/wrapInAction.mjs +90 -0
- package/dist/src/actionMiddlewares/actionSerialization/actionSerialization.mjs +131 -0
- package/dist/src/actionMiddlewares/actionSerialization/applySerializedAction.mjs +110 -0
- package/dist/src/actionMiddlewares/actionSerialization/arraySerializer.mjs +15 -0
- package/dist/src/actionMiddlewares/actionSerialization/core.mjs +3 -0
- package/dist/src/actionMiddlewares/actionSerialization/dateSerializer.mjs +15 -0
- package/dist/src/actionMiddlewares/actionSerialization/mapSerializer.mjs +32 -0
- package/dist/src/actionMiddlewares/actionSerialization/objectPathSerializer.mjs +33 -0
- package/dist/src/actionMiddlewares/actionSerialization/objectSnapshotSerializer.mjs +18 -0
- package/dist/src/actionMiddlewares/actionSerialization/plainObjectSerializer.mjs +27 -0
- package/dist/src/actionMiddlewares/actionSerialization/primitiveSerializer.mjs +39 -0
- package/dist/src/actionMiddlewares/actionSerialization/setSerializer.mjs +29 -0
- package/dist/src/actionMiddlewares/actionTrackingMiddleware.mjs +239 -0
- package/dist/src/actionMiddlewares/onActionMiddleware.mjs +44 -0
- package/dist/src/actionMiddlewares/readonlyMiddleware.mjs +49 -0
- package/dist/src/actionMiddlewares/transactionMiddleware.mjs +69 -0
- package/dist/src/actionMiddlewares/undoMiddleware.mjs +472 -0
- package/dist/src/actionMiddlewares/utils.mjs +25 -0
- package/dist/src/computedTree/computedTree.mjs +63 -0
- package/dist/src/context/context.mjs +141 -0
- package/dist/src/dataModel/BaseDataModel.mjs +108 -0
- package/dist/src/dataModel/DataModel.mjs +32 -0
- package/dist/src/dataModel/actions.mjs +12 -0
- package/dist/src/dataModel/getDataModelMetadata.mjs +14 -0
- package/dist/src/dataModel/newDataModel.mjs +31 -0
- package/dist/src/dataModel/utils.mjs +29 -0
- package/dist/src/frozen/Frozen.mjs +68 -0
- package/dist/src/globalConfig/globalConfig.mjs +57 -0
- package/dist/src/index.mjs +97 -0
- package/dist/src/model/BaseModel.mjs +118 -0
- package/dist/src/model/Model.mjs +36 -0
- package/dist/src/model/getModelMetadata.mjs +19 -0
- package/dist/src/model/metadata.mjs +7 -0
- package/dist/src/model/newModel.mjs +95 -0
- package/dist/src/model/utils.mjs +33 -0
- package/dist/src/modelShared/BaseModelShared.mjs +9 -0
- package/dist/src/modelShared/Model.mjs +249 -0
- package/dist/src/modelShared/checkModelDecoratorArgs.mjs +21 -0
- package/dist/src/modelShared/modelClassInitializer.mjs +14 -0
- package/dist/src/modelShared/modelDecorator.mjs +117 -0
- package/dist/src/modelShared/modelInfo.mjs +7 -0
- package/dist/src/modelShared/modelPropsInfo.mjs +9 -0
- package/dist/src/modelShared/modelSymbols.mjs +5 -0
- package/dist/src/modelShared/newModel.mjs +13 -0
- package/dist/src/modelShared/prop.mjs +138 -0
- package/dist/src/modelShared/utils.mjs +10 -0
- package/dist/src/parent/core.mjs +34 -0
- package/dist/src/parent/coreObjectChildren.mjs +98 -0
- package/dist/src/parent/detach.mjs +32 -0
- package/dist/src/parent/findChildren.mjs +16 -0
- package/dist/src/parent/findParent.mjs +30 -0
- package/dist/src/parent/getChildrenObjects.mjs +12 -0
- package/dist/src/parent/onChildAttachedTo.mjs +101 -0
- package/dist/src/parent/path.mjs +149 -0
- package/dist/src/parent/path2.mjs +12 -0
- package/dist/src/parent/setParent.mjs +83 -0
- package/dist/src/parent/walkTree.mjs +104 -0
- package/dist/src/patch/applyPatches.mjs +118 -0
- package/dist/src/patch/emitPatch.mjs +100 -0
- package/dist/src/patch/jsonPatch.mjs +60 -0
- package/dist/src/patch/patchRecorder.mjs +75 -0
- package/dist/src/redux/connectReduxDevTools.mjs +153 -0
- package/dist/src/redux/redux.mjs +60 -0
- package/dist/src/ref/Ref.mjs +38 -0
- package/dist/src/ref/core.mjs +179 -0
- package/dist/src/ref/customRef.mjs +9 -0
- package/dist/src/ref/rootRef.mjs +34 -0
- package/dist/src/rootStore/attachDetach.mjs +53 -0
- package/dist/src/rootStore/rootStore.mjs +50 -0
- package/dist/src/snapshot/SnapshotterAndReconcilerPriority.mjs +9 -0
- package/dist/src/snapshot/applySnapshot.mjs +92 -0
- package/dist/src/snapshot/clone.mjs +29 -0
- package/dist/src/snapshot/fromArraySnapshot.mjs +23 -0
- package/dist/src/snapshot/fromFrozenSnapshot.mjs +13 -0
- package/dist/src/snapshot/fromModelSnapshot.mjs +38 -0
- package/dist/src/snapshot/fromPlainObjectSnapshot.mjs +26 -0
- package/dist/src/snapshot/fromSnapshot.mjs +91 -0
- package/dist/src/snapshot/getSnapshot.mjs +27 -0
- package/dist/src/snapshot/internal.mjs +97 -0
- package/dist/src/snapshot/onSnapshot.mjs +16 -0
- package/dist/src/snapshot/reconcileArraySnapshot.mjs +40 -0
- package/dist/src/snapshot/reconcileFrozenSnapshot.mjs +19 -0
- package/dist/src/snapshot/reconcileModelSnapshot.mjs +77 -0
- package/dist/src/snapshot/reconcilePlainObjectSnapshot.mjs +47 -0
- package/dist/src/snapshot/reconcileSnapshot.mjs +49 -0
- package/dist/src/snapshot/registerDefaultReconcilers.mjs +17 -0
- package/dist/src/snapshot/registerDefaultSnapshotters.mjs +17 -0
- package/dist/src/standardActions/actions.mjs +35 -0
- package/dist/src/standardActions/arrayActions.mjs +66 -0
- package/dist/src/standardActions/objectActions.mjs +36 -0
- package/dist/src/standardActions/standaloneActions.mjs +9 -0
- package/dist/src/transforms/ImmutableDate.mjs +51 -0
- package/dist/src/transforms/asMap.mjs +29 -0
- package/dist/src/transforms/asSet.mjs +12 -0
- package/dist/src/transforms/bigint.mjs +14 -0
- package/dist/src/transforms/date.mjs +27 -0
- package/dist/src/treeUtils/deepEquals.mjs +40 -0
- package/dist/src/treeUtils/draft.mjs +116 -0
- package/dist/src/treeUtils/sandbox.mjs +158 -0
- package/dist/src/tweaker/TweakerPriority.mjs +9 -0
- package/dist/src/tweaker/core.mjs +38 -0
- package/dist/src/tweaker/registerDefaultTweakers.mjs +17 -0
- package/dist/src/tweaker/tweak.mjs +109 -0
- package/dist/src/tweaker/tweakArray.mjs +272 -0
- package/dist/src/tweaker/tweakFrozen.mjs +28 -0
- package/dist/src/tweaker/tweakModel.mjs +26 -0
- package/dist/src/tweaker/tweakPlainObject.mjs +205 -0
- package/dist/src/tweaker/typeChecking.mjs +46 -0
- package/dist/src/tweaker/withoutTypeChecking.mjs +15 -0
- package/dist/src/types/TypeCheckError.mjs +31 -0
- package/dist/src/types/TypeChecker.mjs +165 -0
- package/dist/src/types/arrayBased/array.mjs +68 -0
- package/dist/src/types/arrayBased/tuple.mjs +71 -0
- package/dist/src/types/getTypeInfo.mjs +12 -0
- package/dist/src/types/objectBased/arraySet.mjs +83 -0
- package/dist/src/types/objectBased/dataModelData.mjs +95 -0
- package/dist/src/types/objectBased/model.mjs +133 -0
- package/dist/src/types/objectBased/object.mjs +119 -0
- package/dist/src/types/objectBased/objectMap.mjs +90 -0
- package/dist/src/types/objectBased/record.mjs +80 -0
- package/dist/src/types/objectBased/ref.mjs +62 -0
- package/dist/src/types/primitiveBased/enum.mjs +20 -0
- package/dist/src/types/primitiveBased/primitives.mjs +59 -0
- package/dist/src/types/primitiveBased/refinedPrimitives.mjs +6 -0
- package/dist/src/types/registerDefaultStandardTypeResolvers.mjs +13 -0
- package/dist/src/types/resolveTypeChecker.mjs +52 -0
- package/dist/src/types/tProp.mjs +53 -0
- package/dist/src/types/typeCheck.mjs +11 -0
- package/dist/src/types/types.mjs +69 -0
- package/dist/src/types/utility/maybe.mjs +10 -0
- package/dist/src/types/utility/or.mjs +99 -0
- package/dist/src/types/utility/refinement.mjs +52 -0
- package/dist/src/types/utility/unchecked.mjs +10 -0
- package/dist/src/utils/ModelPool.mjs +50 -0
- package/dist/src/utils/chainFns.mjs +15 -0
- package/dist/src/utils/index.mjs +228 -0
- package/dist/src/utils/mapUtils.mjs +10 -0
- package/dist/src/utils/tag.mjs +16 -0
- package/dist/src/wrappers/ArraySet.mjs +85 -0
- package/dist/src/wrappers/ObjectMap.mjs +96 -0
- package/dist/src/wrappers/asMap.mjs +181 -0
- package/dist/src/wrappers/asSet.mjs +97 -0
- package/dist/tweaker/index.d.ts +0 -4
- package/dist/tweaker/registerDefaultTweakers.d.ts +1 -0
- package/dist/types/registerDefaultStandardTypeResolvers.d.ts +1 -0
- package/package.json +12 -14
- package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +24 -9
- package/src/dataModel/BaseDataModel.ts +0 -3
- package/src/dataModel/utils.ts +4 -4
- package/src/dist/action/actionDecoratorUtils.d.ts +5 -0
- package/src/dist/action/applyAction.d.ts +36 -0
- package/src/dist/action/applyDelete.d.ts +7 -0
- package/src/dist/action/applyMethodCall.d.ts +9 -0
- package/src/dist/action/applySet.d.ts +8 -0
- package/src/dist/action/builtInActions.d.ts +39 -0
- package/src/dist/action/context.d.ts +85 -0
- package/src/dist/action/hookActions.d.ts +32 -0
- package/src/dist/action/index.d.ts +12 -0
- package/src/dist/action/isModelAction.d.ts +7 -0
- package/src/dist/action/middleware.d.ts +34 -0
- package/src/dist/action/modelAction.d.ts +9 -0
- package/src/dist/action/modelFlow.d.ts +34 -0
- package/src/dist/action/pendingActions.d.ts +1 -0
- package/src/dist/action/protection.d.ts +1 -0
- package/src/dist/action/runUnprotected.d.ts +19 -0
- package/src/dist/action/wrapInAction.d.ts +1 -0
- package/src/dist/actionMiddlewares/actionSerialization/actionSerialization.d.ts +84 -0
- package/src/dist/actionMiddlewares/actionSerialization/applySerializedAction.d.ts +45 -0
- package/src/dist/actionMiddlewares/actionSerialization/arraySerializer.d.ts +3 -0
- package/src/dist/actionMiddlewares/actionSerialization/core.d.ts +28 -0
- package/src/dist/actionMiddlewares/actionSerialization/dateSerializer.d.ts +2 -0
- package/src/dist/actionMiddlewares/actionSerialization/index.d.ts +3 -0
- package/src/dist/actionMiddlewares/actionSerialization/mapSerializer.d.ts +6 -0
- package/src/dist/actionMiddlewares/actionSerialization/objectPathSerializer.d.ts +8 -0
- package/src/dist/actionMiddlewares/actionSerialization/objectSnapshotSerializer.d.ts +2 -0
- package/src/dist/actionMiddlewares/actionSerialization/plainObjectSerializer.d.ts +2 -0
- package/src/dist/actionMiddlewares/actionSerialization/primitiveSerializer.d.ts +2 -0
- package/src/dist/actionMiddlewares/actionSerialization/setSerializer.d.ts +3 -0
- package/src/dist/actionMiddlewares/actionTrackingMiddleware.d.ts +122 -0
- package/src/dist/actionMiddlewares/index.d.ts +6 -0
- package/src/dist/actionMiddlewares/onActionMiddleware.d.ts +26 -0
- package/src/dist/actionMiddlewares/readonlyMiddleware.d.ts +35 -0
- package/src/dist/actionMiddlewares/transactionMiddleware.d.ts +21 -0
- package/src/dist/actionMiddlewares/undoMiddleware.d.ts +283 -0
- package/src/dist/actionMiddlewares/utils.d.ts +1 -0
- package/src/dist/computedTree/computedTree.d.ts +16 -0
- package/src/dist/computedTree/index.d.ts +1 -0
- package/src/dist/context/context.d.ts +84 -0
- package/src/dist/context/index.d.ts +1 -0
- package/src/dist/dataModel/BaseDataModel.d.ts +52 -0
- package/src/dist/dataModel/DataModel.d.ts +47 -0
- package/src/dist/dataModel/DataModelConstructorOptions.d.ts +1 -0
- package/src/dist/dataModel/actions.d.ts +1 -0
- package/src/dist/dataModel/getDataModelMetadata.d.ts +19 -0
- package/src/dist/dataModel/index.d.ts +4 -0
- package/src/dist/dataModel/newDataModel.d.ts +1 -0
- package/src/dist/dataModel/utils.d.ts +8 -0
- package/src/dist/frozen/Frozen.d.ts +50 -0
- package/src/dist/frozen/index.d.ts +1 -0
- package/src/dist/globalConfig/globalConfig.d.ts +51 -0
- package/src/dist/globalConfig/index.d.ts +1 -0
- package/src/dist/index.d.ts +23 -0
- package/src/dist/mobx-keystone.umd.js +8201 -0
- package/src/dist/model/BaseModel.d.ts +125 -0
- package/src/dist/model/Model.d.ts +94 -0
- package/src/dist/model/ModelConstructorOptions.d.ts +1 -0
- package/src/dist/model/getModelMetadata.d.ts +27 -0
- package/src/dist/model/index.d.ts +5 -0
- package/src/dist/model/metadata.d.ts +8 -0
- package/src/dist/model/newModel.d.ts +1 -0
- package/src/dist/model/utils.d.ts +8 -0
- package/src/dist/modelShared/BaseModelShared.d.ts +76 -0
- package/src/dist/modelShared/Model.d.ts +15 -0
- package/src/dist/modelShared/checkModelDecoratorArgs.d.ts +1 -0
- package/src/dist/modelShared/index.d.ts +3 -0
- package/src/dist/modelShared/modelClassInitializer.d.ts +1 -0
- package/src/dist/modelShared/modelDecorator.d.ts +24 -0
- package/src/dist/modelShared/modelInfo.d.ts +1 -0
- package/src/dist/modelShared/modelPropsInfo.d.ts +1 -0
- package/src/dist/modelShared/modelSymbols.d.ts +3 -0
- package/src/dist/modelShared/newModel.d.ts +4 -0
- package/src/dist/modelShared/prop.d.ts +179 -0
- package/src/dist/modelShared/utils.d.ts +1 -0
- package/src/dist/parent/core.d.ts +1 -0
- package/src/dist/parent/coreObjectChildren.d.ts +1 -0
- package/src/dist/parent/detach.d.ts +9 -0
- package/src/dist/parent/findChildren.d.ts +13 -0
- package/src/dist/parent/findParent.d.ts +41 -0
- package/src/dist/parent/getChildrenObjects.d.ts +11 -0
- package/src/dist/parent/index.d.ts +9 -0
- package/src/dist/parent/onChildAttachedTo.d.ts +22 -0
- package/src/dist/parent/path.d.ts +107 -0
- package/src/dist/parent/path2.d.ts +16 -0
- package/src/dist/parent/pathTypes.d.ts +12 -0
- package/src/dist/parent/setParent.d.ts +1 -0
- package/src/dist/parent/walkTree.d.ts +25 -0
- package/src/dist/patch/Patch.d.ts +16 -0
- package/src/dist/patch/applyPatches.d.ts +9 -0
- package/src/dist/patch/emitPatch.d.ts +29 -0
- package/src/dist/patch/index.d.ts +5 -0
- package/src/dist/patch/jsonPatch.d.ts +45 -0
- package/src/dist/patch/patchRecorder.d.ts +70 -0
- package/src/dist/redux/connectReduxDevTools.d.ts +12 -0
- package/src/dist/redux/index.d.ts +2 -0
- package/src/dist/redux/redux.d.ts +47 -0
- package/src/dist/ref/Ref.d.ts +53 -0
- package/src/dist/ref/core.d.ts +46 -0
- package/src/dist/ref/customRef.d.ts +38 -0
- package/src/dist/ref/index.d.ts +4 -0
- package/src/dist/ref/rootRef.d.ts +33 -0
- package/src/dist/rootStore/attachDetach.d.ts +1 -0
- package/src/dist/rootStore/index.d.ts +1 -0
- package/src/dist/rootStore/rootStore.d.ts +33 -0
- package/src/dist/snapshot/SnapshotOf.d.ts +47 -0
- package/src/dist/snapshot/SnapshotterAndReconcilerPriority.d.ts +1 -0
- package/src/dist/snapshot/applySnapshot.d.ts +17 -0
- package/src/dist/snapshot/clone.d.ts +18 -0
- package/src/dist/snapshot/fromArraySnapshot.d.ts +1 -0
- package/src/dist/snapshot/fromFrozenSnapshot.d.ts +1 -0
- package/src/dist/snapshot/fromModelSnapshot.d.ts +1 -0
- package/src/dist/snapshot/fromPlainObjectSnapshot.d.ts +1 -0
- package/src/dist/snapshot/fromSnapshot.d.ts +36 -0
- package/src/dist/snapshot/getSnapshot.d.ts +22 -0
- package/src/dist/snapshot/index.d.ts +6 -0
- package/src/dist/snapshot/internal.d.ts +1 -0
- package/src/dist/snapshot/onSnapshot.d.ts +18 -0
- package/src/dist/snapshot/reconcileArraySnapshot.d.ts +1 -0
- package/src/dist/snapshot/reconcileFrozenSnapshot.d.ts +1 -0
- package/src/dist/snapshot/reconcileModelSnapshot.d.ts +1 -0
- package/src/dist/snapshot/reconcilePlainObjectSnapshot.d.ts +1 -0
- package/src/dist/snapshot/reconcileSnapshot.d.ts +1 -0
- package/src/dist/snapshot/registerDefaultReconcilers.d.ts +1 -0
- package/src/dist/snapshot/registerDefaultSnapshotters.d.ts +1 -0
- package/src/dist/standardActions/actions.d.ts +1 -0
- package/src/dist/standardActions/arrayActions.d.ts +18 -0
- package/src/dist/standardActions/index.d.ts +3 -0
- package/src/dist/standardActions/objectActions.d.ts +7 -0
- package/src/dist/standardActions/standaloneActions.d.ts +16 -0
- package/src/dist/transforms/ImmutableDate.d.ts +1 -0
- package/src/dist/transforms/asMap.d.ts +3 -0
- package/src/dist/transforms/asSet.d.ts +2 -0
- package/src/dist/transforms/bigint.d.ts +2 -0
- package/src/dist/transforms/date.d.ts +3 -0
- package/src/dist/transforms/index.d.ts +4 -0
- package/src/dist/treeUtils/deepEquals.d.ts +20 -0
- package/src/dist/treeUtils/draft.d.ts +70 -0
- package/src/dist/treeUtils/index.d.ts +3 -0
- package/src/dist/treeUtils/sandbox.d.ts +85 -0
- package/src/dist/tweaker/TweakerPriority.d.ts +1 -0
- package/src/dist/tweaker/core.d.ts +14 -0
- package/src/dist/tweaker/index.d.ts +2 -0
- package/src/dist/tweaker/registerDefaultTweakers.d.ts +1 -0
- package/src/dist/tweaker/tweak.d.ts +22 -0
- package/src/dist/tweaker/tweakArray.d.ts +1 -0
- package/src/dist/tweaker/tweakFrozen.d.ts +1 -0
- package/src/dist/tweaker/tweakModel.d.ts +1 -0
- package/src/dist/tweaker/tweakPlainObject.d.ts +1 -0
- package/src/dist/tweaker/typeChecking.d.ts +1 -0
- package/src/dist/tweaker/withoutTypeChecking.d.ts +2 -0
- package/src/dist/types/TypeCheckError.d.ts +22 -0
- package/src/dist/types/TypeChecker.d.ts +8 -0
- package/src/dist/types/arrayBased/array.d.ts +23 -0
- package/src/dist/types/arrayBased/tuple.d.ts +24 -0
- package/src/dist/types/getTypeInfo.d.ts +9 -0
- package/src/dist/types/index.d.ts +5 -0
- package/src/dist/types/objectBased/arraySet.d.ts +24 -0
- package/src/dist/types/objectBased/dataModelData.d.ts +44 -0
- package/src/dist/types/objectBased/model.d.ts +43 -0
- package/src/dist/types/objectBased/object.d.ts +63 -0
- package/src/dist/types/objectBased/objectMap.d.ts +24 -0
- package/src/dist/types/objectBased/record.d.ts +24 -0
- package/src/dist/types/objectBased/ref.d.ts +21 -0
- package/src/dist/types/primitiveBased/enum.d.ts +34 -0
- package/src/dist/types/primitiveBased/primitives.d.ts +81 -0
- package/src/dist/types/primitiveBased/refinedPrimitives.d.ts +18 -0
- package/src/dist/types/registerDefaultStandardTypeResolvers.d.ts +1 -0
- package/src/dist/types/resolveTypeChecker.d.ts +1 -0
- package/src/dist/types/schemas.d.ts +53 -0
- package/src/dist/types/tProp.d.ts +92 -0
- package/src/dist/types/typeCheck.d.ts +11 -0
- package/src/dist/types/types.d.ts +51 -0
- package/src/dist/types/utility/maybe.d.ts +29 -0
- package/src/dist/types/utility/or.d.ts +35 -0
- package/src/dist/types/utility/refinement.d.ts +45 -0
- package/src/dist/types/utility/unchecked.d.ts +22 -0
- package/src/dist/utils/ModelPool.d.ts +7 -0
- package/src/dist/utils/chainFns.d.ts +1 -0
- package/src/dist/utils/index.d.ts +6 -0
- package/src/dist/utils/mapUtils.d.ts +2 -0
- package/src/dist/utils/tag.d.ts +13 -0
- package/src/dist/utils/types.d.ts +39 -0
- package/src/dist/wrappers/ArraySet.d.ts +29 -0
- package/src/dist/wrappers/ObjectMap.d.ts +32 -0
- package/src/dist/wrappers/asMap.d.ts +29 -0
- package/src/dist/wrappers/asSet.d.ts +15 -0
- package/src/dist/wrappers/index.d.ts +4 -0
- package/src/model/BaseModel.ts +0 -3
- package/src/model/utils.ts +4 -5
- package/src/modelShared/checkModelDecoratorArgs.ts +6 -6
- package/src/parent/setParent.ts +2 -2
- package/src/rootStore/attachDetach.ts +2 -3
- package/src/snapshot/fromArraySnapshot.ts +11 -6
- package/src/snapshot/fromFrozenSnapshot.ts +11 -6
- package/src/snapshot/fromModelSnapshot.ts +11 -6
- package/src/snapshot/fromPlainObjectSnapshot.ts +11 -6
- package/src/snapshot/fromSnapshot.ts +3 -0
- package/src/snapshot/index.ts +0 -10
- package/src/snapshot/reconcileArraySnapshot.ts +11 -6
- package/src/snapshot/reconcileFrozenSnapshot.ts +11 -6
- package/src/snapshot/reconcileModelSnapshot.ts +11 -6
- package/src/snapshot/reconcilePlainObjectSnapshot.ts +11 -6
- package/src/snapshot/reconcileSnapshot.ts +3 -0
- package/src/snapshot/registerDefaultReconcilers.ts +21 -0
- package/src/snapshot/registerDefaultSnapshotters.ts +21 -0
- package/src/tweaker/index.ts +0 -6
- package/src/tweaker/registerDefaultTweakers.ts +21 -0
- package/src/tweaker/tweak.ts +3 -0
- package/src/tweaker/tweakArray.ts +406 -401
- package/src/tweaker/tweakFrozen.ts +11 -6
- package/src/tweaker/tweakModel.ts +11 -6
- package/src/tweaker/tweakPlainObject.ts +288 -283
- package/src/types/objectBased/model.ts +6 -1
- package/src/types/objectBased/ref.ts +1 -1
- package/src/types/primitiveBased/primitives.ts +177 -166
- package/src/types/registerDefaultStandardTypeResolvers.ts +17 -0
- package/src/types/resolveTypeChecker.ts +7 -1
- package/dist/mobx-keystone.es.js +0 -7856
- package/dist/mobx-keystone.es.mjs +0 -7856
- package/src/dataModel/_BaseDataModel.ts +0 -15
- package/src/model/_BaseModel.ts +0 -15
|
@@ -1,401 +1,406 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IArrayWillChange,
|
|
3
|
-
IArrayWillSplice,
|
|
4
|
-
intercept,
|
|
5
|
-
IObservableArray,
|
|
6
|
-
isObservableArray,
|
|
7
|
-
observable,
|
|
8
|
-
observe,
|
|
9
|
-
set,
|
|
10
|
-
} from "mobx"
|
|
11
|
-
import { assertCanWrite } from "../action/protection"
|
|
12
|
-
import { getGlobalConfig } from "../globalConfig"
|
|
13
|
-
import type { ParentPath } from "../parent/path"
|
|
14
|
-
import { setParent } from "../parent/setParent"
|
|
15
|
-
import { InternalPatchRecorder } from "../patch/emitPatch"
|
|
16
|
-
import type { Patch } from "../patch/Patch"
|
|
17
|
-
import {
|
|
18
|
-
freezeInternalSnapshot,
|
|
19
|
-
getInternalSnapshot,
|
|
20
|
-
setNewInternalSnapshot,
|
|
21
|
-
updateInternalSnapshot,
|
|
22
|
-
} from "../snapshot/internal"
|
|
23
|
-
import { failure, inDevMode, isArray, isPrimitive } from "../utils"
|
|
24
|
-
import { runningWithoutSnapshotOrPatches, tweakedObjects } from "./core"
|
|
25
|
-
import { registerTweaker, tryUntweak, tweak } from "./tweak"
|
|
26
|
-
import { TweakerPriority } from "./TweakerPriority"
|
|
27
|
-
import { runTypeCheckingAfterChange } from "./typeChecking"
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @internal
|
|
31
|
-
*/
|
|
32
|
-
export function tweakArray<T extends any[]>(
|
|
33
|
-
value: T,
|
|
34
|
-
parentPath: ParentPath<any> | undefined,
|
|
35
|
-
doNotTweakChildren: boolean
|
|
36
|
-
): T {
|
|
37
|
-
const originalArr: ReadonlyArray<any> = value
|
|
38
|
-
const arrLn = originalArr.length
|
|
39
|
-
const tweakedArr = isObservableArray(originalArr)
|
|
40
|
-
? originalArr
|
|
41
|
-
: observable.array([], observableOptions)
|
|
42
|
-
if (tweakedArr !== originalArr) {
|
|
43
|
-
tweakedArr.length = originalArr.length
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
let interceptDisposer: () => void
|
|
47
|
-
let observeDisposer: () => void
|
|
48
|
-
|
|
49
|
-
const untweak = () => {
|
|
50
|
-
interceptDisposer()
|
|
51
|
-
observeDisposer()
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
tweakedObjects.set(tweakedArr, untweak)
|
|
55
|
-
setParent({
|
|
56
|
-
value: tweakedArr,
|
|
57
|
-
parentPath,
|
|
58
|
-
indexChangeAllowed: false,
|
|
59
|
-
isDataObject: false,
|
|
60
|
-
// arrays shouldn't be cloned anyway
|
|
61
|
-
cloneIfApplicable: false,
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
const untransformedSn: any[] = []
|
|
65
|
-
untransformedSn.length = arrLn
|
|
66
|
-
|
|
67
|
-
// substitute initial values by proxied values
|
|
68
|
-
for (let i = 0; i < arrLn; i++) {
|
|
69
|
-
const v = originalArr[i]
|
|
70
|
-
|
|
71
|
-
if (isPrimitive(v)) {
|
|
72
|
-
if (!doNotTweakChildren) {
|
|
73
|
-
set(tweakedArr, i, v)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
untransformedSn[i] = v
|
|
77
|
-
} else {
|
|
78
|
-
const path = { parent: tweakedArr, path: i }
|
|
79
|
-
|
|
80
|
-
let tweakedValue
|
|
81
|
-
if (doNotTweakChildren) {
|
|
82
|
-
tweakedValue = v
|
|
83
|
-
setParent({
|
|
84
|
-
value: tweakedValue,
|
|
85
|
-
parentPath: path,
|
|
86
|
-
indexChangeAllowed: false,
|
|
87
|
-
isDataObject: false,
|
|
88
|
-
// the value is already a new value (the result of a fromSnapshot)
|
|
89
|
-
cloneIfApplicable: false,
|
|
90
|
-
})
|
|
91
|
-
} else {
|
|
92
|
-
tweakedValue = tweak(v, path)
|
|
93
|
-
set(tweakedArr, i, tweakedValue)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const valueSn = getInternalSnapshot(tweakedValue)!
|
|
97
|
-
untransformedSn[i] = valueSn.transformed
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
setNewInternalSnapshot(tweakedArr, untransformedSn, undefined)
|
|
102
|
-
|
|
103
|
-
interceptDisposer = intercept(tweakedArr, interceptArrayMutation.bind(undefined, tweakedArr))
|
|
104
|
-
observeDisposer = observe(tweakedArr, arrayDidChange)
|
|
105
|
-
|
|
106
|
-
return tweakedArr as any
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function arrayDidChange(change: any /*IArrayDidChange*/) {
|
|
110
|
-
const arr = change.object
|
|
111
|
-
let { untransformed: oldSnapshot } = getInternalSnapshot(arr as Array<any>)!
|
|
112
|
-
|
|
113
|
-
const patchRecorder = new InternalPatchRecorder()
|
|
114
|
-
|
|
115
|
-
let mutate: ((sn: any[]) => void) | undefined
|
|
116
|
-
|
|
117
|
-
switch (change.type) {
|
|
118
|
-
case "splice":
|
|
119
|
-
{
|
|
120
|
-
const index = change.index
|
|
121
|
-
const addedCount = change.addedCount
|
|
122
|
-
const removedCount = change.removedCount
|
|
123
|
-
|
|
124
|
-
let addedItems: any[] = []
|
|
125
|
-
addedItems.length = addedCount
|
|
126
|
-
for (let i = 0; i < addedCount; i++) {
|
|
127
|
-
const v = change.added[i]
|
|
128
|
-
if (isPrimitive(v)) {
|
|
129
|
-
addedItems[i] = v
|
|
130
|
-
} else {
|
|
131
|
-
addedItems[i] = getInternalSnapshot(v)!.transformed
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const oldLen = oldSnapshot.length
|
|
136
|
-
mutate = (newSnapshot) => {
|
|
137
|
-
newSnapshot.splice(index, removedCount, ...addedItems)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const patches: Patch[] = []
|
|
141
|
-
const invPatches: Patch[] = []
|
|
142
|
-
|
|
143
|
-
// optimization: if we add as many as we remove then replace instead
|
|
144
|
-
if (addedCount === removedCount) {
|
|
145
|
-
for (let i = 0; i < addedCount; i++) {
|
|
146
|
-
const realIndex = index + i
|
|
147
|
-
|
|
148
|
-
const newVal = getValueAfterSplice(
|
|
149
|
-
oldSnapshot,
|
|
150
|
-
realIndex,
|
|
151
|
-
index,
|
|
152
|
-
removedCount,
|
|
153
|
-
addedItems
|
|
154
|
-
)
|
|
155
|
-
const oldVal = oldSnapshot[realIndex]
|
|
156
|
-
|
|
157
|
-
if (newVal !== oldVal) {
|
|
158
|
-
const path = [realIndex]
|
|
159
|
-
// replace 0, 1, 2...
|
|
160
|
-
patches.push({
|
|
161
|
-
op: "replace",
|
|
162
|
-
path,
|
|
163
|
-
value: freezeInternalSnapshot(newVal),
|
|
164
|
-
})
|
|
165
|
-
// replace ...2, 1, 0 since inverse patches are applied in reverse
|
|
166
|
-
invPatches.push({
|
|
167
|
-
op: "replace",
|
|
168
|
-
path,
|
|
169
|
-
value: freezeInternalSnapshot(oldVal),
|
|
170
|
-
})
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
} else {
|
|
174
|
-
const interimLen = oldLen - removedCount
|
|
175
|
-
|
|
176
|
-
// first remove items
|
|
177
|
-
if (removedCount > 0) {
|
|
178
|
-
// optimization, when removing from the end set the length instead
|
|
179
|
-
const removeUsingSetLength = index >= interimLen
|
|
180
|
-
if (removeUsingSetLength) {
|
|
181
|
-
patches.push({
|
|
182
|
-
op: "replace",
|
|
183
|
-
path: ["length"],
|
|
184
|
-
value: interimLen,
|
|
185
|
-
})
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
for (let i = removedCount - 1; i >= 0; i--) {
|
|
189
|
-
const realIndex = index + i
|
|
190
|
-
const path = [realIndex]
|
|
191
|
-
|
|
192
|
-
if (!removeUsingSetLength) {
|
|
193
|
-
// remove ...2, 1, 0
|
|
194
|
-
patches.push({
|
|
195
|
-
op: "remove",
|
|
196
|
-
path,
|
|
197
|
-
})
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// add 0, 1, 2... since inverse patches are applied in reverse
|
|
201
|
-
invPatches.push({
|
|
202
|
-
op: "add",
|
|
203
|
-
path,
|
|
204
|
-
value: freezeInternalSnapshot(oldSnapshot[realIndex]),
|
|
205
|
-
})
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// then add items
|
|
210
|
-
if (addedCount > 0) {
|
|
211
|
-
// optimization, for inverse patches, when adding from the end set the length to restore instead
|
|
212
|
-
const restoreUsingSetLength = index >= interimLen
|
|
213
|
-
if (restoreUsingSetLength) {
|
|
214
|
-
invPatches.push({
|
|
215
|
-
op: "replace",
|
|
216
|
-
path: ["length"],
|
|
217
|
-
value: interimLen,
|
|
218
|
-
})
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
for (let i = 0; i < addedCount; i++) {
|
|
222
|
-
const realIndex = index + i
|
|
223
|
-
const path = [realIndex]
|
|
224
|
-
|
|
225
|
-
// add 0, 1, 2...
|
|
226
|
-
patches.push({
|
|
227
|
-
op: "add",
|
|
228
|
-
path,
|
|
229
|
-
value: freezeInternalSnapshot(
|
|
230
|
-
getValueAfterSplice(oldSnapshot, realIndex, index, removedCount, addedItems)
|
|
231
|
-
),
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
// remove ...2, 1, 0 since inverse patches are applied in reverse
|
|
235
|
-
if (!restoreUsingSetLength) {
|
|
236
|
-
invPatches.push({
|
|
237
|
-
op: "remove",
|
|
238
|
-
path,
|
|
239
|
-
})
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
patchRecorder.record(patches, invPatches)
|
|
246
|
-
}
|
|
247
|
-
break
|
|
248
|
-
|
|
249
|
-
case "update":
|
|
250
|
-
{
|
|
251
|
-
const k = change.index
|
|
252
|
-
const val = change.newValue
|
|
253
|
-
const oldVal = oldSnapshot[k]
|
|
254
|
-
let newVal: any
|
|
255
|
-
if (isPrimitive(val)) {
|
|
256
|
-
newVal = val
|
|
257
|
-
} else {
|
|
258
|
-
const valueSn = getInternalSnapshot(val)!
|
|
259
|
-
newVal = valueSn.transformed
|
|
260
|
-
}
|
|
261
|
-
mutate = (newSnapshot) => {
|
|
262
|
-
newSnapshot[k] = newVal
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const path = [k]
|
|
266
|
-
|
|
267
|
-
patchRecorder.record(
|
|
268
|
-
[
|
|
269
|
-
{
|
|
270
|
-
op: "replace",
|
|
271
|
-
path,
|
|
272
|
-
value: freezeInternalSnapshot(newVal),
|
|
273
|
-
},
|
|
274
|
-
],
|
|
275
|
-
[
|
|
276
|
-
{
|
|
277
|
-
op: "replace",
|
|
278
|
-
path,
|
|
279
|
-
value: freezeInternalSnapshot(oldVal),
|
|
280
|
-
},
|
|
281
|
-
]
|
|
282
|
-
)
|
|
283
|
-
}
|
|
284
|
-
break
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
runTypeCheckingAfterChange(arr, patchRecorder)
|
|
288
|
-
|
|
289
|
-
if (!runningWithoutSnapshotOrPatches && mutate) {
|
|
290
|
-
updateInternalSnapshot(arr, mutate)
|
|
291
|
-
patchRecorder.emit(arr)
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const undefinedInsideArrayErrorMsg =
|
|
296
|
-
"undefined is not supported inside arrays since it is not serializable in JSON, consider using null instead"
|
|
297
|
-
|
|
298
|
-
// TODO: remove array parameter and just use change.object once mobx update event is fixed
|
|
299
|
-
function interceptArrayMutation(
|
|
300
|
-
array: IObservableArray,
|
|
301
|
-
change: IArrayWillChange | IArrayWillSplice
|
|
302
|
-
) {
|
|
303
|
-
assertCanWrite()
|
|
304
|
-
|
|
305
|
-
switch (change.type) {
|
|
306
|
-
case "splice":
|
|
307
|
-
{
|
|
308
|
-
if (inDevMode() && !getGlobalConfig().allowUndefinedArrayElements) {
|
|
309
|
-
const len = change.added.length
|
|
310
|
-
for (let i = 0; i < len; i++) {
|
|
311
|
-
const v = change.added[i]
|
|
312
|
-
if (v === undefined) {
|
|
313
|
-
throw failure(undefinedInsideArrayErrorMsg)
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
for (let i = 0; i < change.removedCount; i++) {
|
|
319
|
-
const removedValue = change.object[change.index + i]
|
|
320
|
-
tweak(removedValue, undefined)
|
|
321
|
-
tryUntweak(removedValue)
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
for (let i = 0; i < change.added.length; i++) {
|
|
325
|
-
change.added[i] = tweak(change.added[i], {
|
|
326
|
-
parent: change.object,
|
|
327
|
-
path: change.index + i,
|
|
328
|
-
})
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// we might also need to update the parent of the next indexes
|
|
332
|
-
const oldNextIndex = change.index + change.removedCount
|
|
333
|
-
const newNextIndex = change.index + change.added.length
|
|
334
|
-
|
|
335
|
-
if (oldNextIndex !== newNextIndex) {
|
|
336
|
-
for (let i = oldNextIndex, j = newNextIndex; i < change.object.length; i++, j++) {
|
|
337
|
-
setParent({
|
|
338
|
-
value: change.object[i],
|
|
339
|
-
parentPath: {
|
|
340
|
-
parent: change.object,
|
|
341
|
-
path: j,
|
|
342
|
-
},
|
|
343
|
-
indexChangeAllowed: true,
|
|
344
|
-
isDataObject: false,
|
|
345
|
-
// just re-indexing
|
|
346
|
-
cloneIfApplicable: false,
|
|
347
|
-
})
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
break
|
|
352
|
-
|
|
353
|
-
case "update":
|
|
354
|
-
if (
|
|
355
|
-
inDevMode() &&
|
|
356
|
-
!getGlobalConfig().allowUndefinedArrayElements &&
|
|
357
|
-
change.newValue === undefined
|
|
358
|
-
) {
|
|
359
|
-
throw failure(undefinedInsideArrayErrorMsg)
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// TODO: should be change.object, but mobx is bugged and doesn't send the proxy
|
|
363
|
-
const oldVal = array[change.index]
|
|
364
|
-
tweak(oldVal, undefined) // set old prop obj parent to undefined
|
|
365
|
-
tryUntweak(oldVal)
|
|
366
|
-
|
|
367
|
-
change.newValue = tweak(change.newValue, { parent: array, path: change.index })
|
|
368
|
-
break
|
|
369
|
-
}
|
|
370
|
-
return change
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
1
|
+
import {
|
|
2
|
+
IArrayWillChange,
|
|
3
|
+
IArrayWillSplice,
|
|
4
|
+
intercept,
|
|
5
|
+
IObservableArray,
|
|
6
|
+
isObservableArray,
|
|
7
|
+
observable,
|
|
8
|
+
observe,
|
|
9
|
+
set,
|
|
10
|
+
} from "mobx"
|
|
11
|
+
import { assertCanWrite } from "../action/protection"
|
|
12
|
+
import { getGlobalConfig } from "../globalConfig"
|
|
13
|
+
import type { ParentPath } from "../parent/path"
|
|
14
|
+
import { setParent } from "../parent/setParent"
|
|
15
|
+
import { InternalPatchRecorder } from "../patch/emitPatch"
|
|
16
|
+
import type { Patch } from "../patch/Patch"
|
|
17
|
+
import {
|
|
18
|
+
freezeInternalSnapshot,
|
|
19
|
+
getInternalSnapshot,
|
|
20
|
+
setNewInternalSnapshot,
|
|
21
|
+
updateInternalSnapshot,
|
|
22
|
+
} from "../snapshot/internal"
|
|
23
|
+
import { failure, inDevMode, isArray, isPrimitive } from "../utils"
|
|
24
|
+
import { runningWithoutSnapshotOrPatches, tweakedObjects } from "./core"
|
|
25
|
+
import { registerTweaker, tryUntweak, tweak } from "./tweak"
|
|
26
|
+
import { TweakerPriority } from "./TweakerPriority"
|
|
27
|
+
import { runTypeCheckingAfterChange } from "./typeChecking"
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export function tweakArray<T extends any[]>(
|
|
33
|
+
value: T,
|
|
34
|
+
parentPath: ParentPath<any> | undefined,
|
|
35
|
+
doNotTweakChildren: boolean
|
|
36
|
+
): T {
|
|
37
|
+
const originalArr: ReadonlyArray<any> = value
|
|
38
|
+
const arrLn = originalArr.length
|
|
39
|
+
const tweakedArr = isObservableArray(originalArr)
|
|
40
|
+
? originalArr
|
|
41
|
+
: observable.array([], observableOptions)
|
|
42
|
+
if (tweakedArr !== originalArr) {
|
|
43
|
+
tweakedArr.length = originalArr.length
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let interceptDisposer: () => void
|
|
47
|
+
let observeDisposer: () => void
|
|
48
|
+
|
|
49
|
+
const untweak = () => {
|
|
50
|
+
interceptDisposer()
|
|
51
|
+
observeDisposer()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
tweakedObjects.set(tweakedArr, untweak)
|
|
55
|
+
setParent({
|
|
56
|
+
value: tweakedArr,
|
|
57
|
+
parentPath,
|
|
58
|
+
indexChangeAllowed: false,
|
|
59
|
+
isDataObject: false,
|
|
60
|
+
// arrays shouldn't be cloned anyway
|
|
61
|
+
cloneIfApplicable: false,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const untransformedSn: any[] = []
|
|
65
|
+
untransformedSn.length = arrLn
|
|
66
|
+
|
|
67
|
+
// substitute initial values by proxied values
|
|
68
|
+
for (let i = 0; i < arrLn; i++) {
|
|
69
|
+
const v = originalArr[i]
|
|
70
|
+
|
|
71
|
+
if (isPrimitive(v)) {
|
|
72
|
+
if (!doNotTweakChildren) {
|
|
73
|
+
set(tweakedArr, i, v)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
untransformedSn[i] = v
|
|
77
|
+
} else {
|
|
78
|
+
const path = { parent: tweakedArr, path: i }
|
|
79
|
+
|
|
80
|
+
let tweakedValue
|
|
81
|
+
if (doNotTweakChildren) {
|
|
82
|
+
tweakedValue = v
|
|
83
|
+
setParent({
|
|
84
|
+
value: tweakedValue,
|
|
85
|
+
parentPath: path,
|
|
86
|
+
indexChangeAllowed: false,
|
|
87
|
+
isDataObject: false,
|
|
88
|
+
// the value is already a new value (the result of a fromSnapshot)
|
|
89
|
+
cloneIfApplicable: false,
|
|
90
|
+
})
|
|
91
|
+
} else {
|
|
92
|
+
tweakedValue = tweak(v, path)
|
|
93
|
+
set(tweakedArr, i, tweakedValue)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const valueSn = getInternalSnapshot(tweakedValue)!
|
|
97
|
+
untransformedSn[i] = valueSn.transformed
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setNewInternalSnapshot(tweakedArr, untransformedSn, undefined)
|
|
102
|
+
|
|
103
|
+
interceptDisposer = intercept(tweakedArr, interceptArrayMutation.bind(undefined, tweakedArr))
|
|
104
|
+
observeDisposer = observe(tweakedArr, arrayDidChange)
|
|
105
|
+
|
|
106
|
+
return tweakedArr as any
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function arrayDidChange(change: any /*IArrayDidChange*/) {
|
|
110
|
+
const arr = change.object
|
|
111
|
+
let { untransformed: oldSnapshot } = getInternalSnapshot(arr as Array<any>)!
|
|
112
|
+
|
|
113
|
+
const patchRecorder = new InternalPatchRecorder()
|
|
114
|
+
|
|
115
|
+
let mutate: ((sn: any[]) => void) | undefined
|
|
116
|
+
|
|
117
|
+
switch (change.type) {
|
|
118
|
+
case "splice":
|
|
119
|
+
{
|
|
120
|
+
const index = change.index
|
|
121
|
+
const addedCount = change.addedCount
|
|
122
|
+
const removedCount = change.removedCount
|
|
123
|
+
|
|
124
|
+
let addedItems: any[] = []
|
|
125
|
+
addedItems.length = addedCount
|
|
126
|
+
for (let i = 0; i < addedCount; i++) {
|
|
127
|
+
const v = change.added[i]
|
|
128
|
+
if (isPrimitive(v)) {
|
|
129
|
+
addedItems[i] = v
|
|
130
|
+
} else {
|
|
131
|
+
addedItems[i] = getInternalSnapshot(v)!.transformed
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const oldLen = oldSnapshot.length
|
|
136
|
+
mutate = (newSnapshot) => {
|
|
137
|
+
newSnapshot.splice(index, removedCount, ...addedItems)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const patches: Patch[] = []
|
|
141
|
+
const invPatches: Patch[] = []
|
|
142
|
+
|
|
143
|
+
// optimization: if we add as many as we remove then replace instead
|
|
144
|
+
if (addedCount === removedCount) {
|
|
145
|
+
for (let i = 0; i < addedCount; i++) {
|
|
146
|
+
const realIndex = index + i
|
|
147
|
+
|
|
148
|
+
const newVal = getValueAfterSplice(
|
|
149
|
+
oldSnapshot,
|
|
150
|
+
realIndex,
|
|
151
|
+
index,
|
|
152
|
+
removedCount,
|
|
153
|
+
addedItems
|
|
154
|
+
)
|
|
155
|
+
const oldVal = oldSnapshot[realIndex]
|
|
156
|
+
|
|
157
|
+
if (newVal !== oldVal) {
|
|
158
|
+
const path = [realIndex]
|
|
159
|
+
// replace 0, 1, 2...
|
|
160
|
+
patches.push({
|
|
161
|
+
op: "replace",
|
|
162
|
+
path,
|
|
163
|
+
value: freezeInternalSnapshot(newVal),
|
|
164
|
+
})
|
|
165
|
+
// replace ...2, 1, 0 since inverse patches are applied in reverse
|
|
166
|
+
invPatches.push({
|
|
167
|
+
op: "replace",
|
|
168
|
+
path,
|
|
169
|
+
value: freezeInternalSnapshot(oldVal),
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
const interimLen = oldLen - removedCount
|
|
175
|
+
|
|
176
|
+
// first remove items
|
|
177
|
+
if (removedCount > 0) {
|
|
178
|
+
// optimization, when removing from the end set the length instead
|
|
179
|
+
const removeUsingSetLength = index >= interimLen
|
|
180
|
+
if (removeUsingSetLength) {
|
|
181
|
+
patches.push({
|
|
182
|
+
op: "replace",
|
|
183
|
+
path: ["length"],
|
|
184
|
+
value: interimLen,
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
for (let i = removedCount - 1; i >= 0; i--) {
|
|
189
|
+
const realIndex = index + i
|
|
190
|
+
const path = [realIndex]
|
|
191
|
+
|
|
192
|
+
if (!removeUsingSetLength) {
|
|
193
|
+
// remove ...2, 1, 0
|
|
194
|
+
patches.push({
|
|
195
|
+
op: "remove",
|
|
196
|
+
path,
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// add 0, 1, 2... since inverse patches are applied in reverse
|
|
201
|
+
invPatches.push({
|
|
202
|
+
op: "add",
|
|
203
|
+
path,
|
|
204
|
+
value: freezeInternalSnapshot(oldSnapshot[realIndex]),
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// then add items
|
|
210
|
+
if (addedCount > 0) {
|
|
211
|
+
// optimization, for inverse patches, when adding from the end set the length to restore instead
|
|
212
|
+
const restoreUsingSetLength = index >= interimLen
|
|
213
|
+
if (restoreUsingSetLength) {
|
|
214
|
+
invPatches.push({
|
|
215
|
+
op: "replace",
|
|
216
|
+
path: ["length"],
|
|
217
|
+
value: interimLen,
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
for (let i = 0; i < addedCount; i++) {
|
|
222
|
+
const realIndex = index + i
|
|
223
|
+
const path = [realIndex]
|
|
224
|
+
|
|
225
|
+
// add 0, 1, 2...
|
|
226
|
+
patches.push({
|
|
227
|
+
op: "add",
|
|
228
|
+
path,
|
|
229
|
+
value: freezeInternalSnapshot(
|
|
230
|
+
getValueAfterSplice(oldSnapshot, realIndex, index, removedCount, addedItems)
|
|
231
|
+
),
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
// remove ...2, 1, 0 since inverse patches are applied in reverse
|
|
235
|
+
if (!restoreUsingSetLength) {
|
|
236
|
+
invPatches.push({
|
|
237
|
+
op: "remove",
|
|
238
|
+
path,
|
|
239
|
+
})
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
patchRecorder.record(patches, invPatches)
|
|
246
|
+
}
|
|
247
|
+
break
|
|
248
|
+
|
|
249
|
+
case "update":
|
|
250
|
+
{
|
|
251
|
+
const k = change.index
|
|
252
|
+
const val = change.newValue
|
|
253
|
+
const oldVal = oldSnapshot[k]
|
|
254
|
+
let newVal: any
|
|
255
|
+
if (isPrimitive(val)) {
|
|
256
|
+
newVal = val
|
|
257
|
+
} else {
|
|
258
|
+
const valueSn = getInternalSnapshot(val)!
|
|
259
|
+
newVal = valueSn.transformed
|
|
260
|
+
}
|
|
261
|
+
mutate = (newSnapshot) => {
|
|
262
|
+
newSnapshot[k] = newVal
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const path = [k]
|
|
266
|
+
|
|
267
|
+
patchRecorder.record(
|
|
268
|
+
[
|
|
269
|
+
{
|
|
270
|
+
op: "replace",
|
|
271
|
+
path,
|
|
272
|
+
value: freezeInternalSnapshot(newVal),
|
|
273
|
+
},
|
|
274
|
+
],
|
|
275
|
+
[
|
|
276
|
+
{
|
|
277
|
+
op: "replace",
|
|
278
|
+
path,
|
|
279
|
+
value: freezeInternalSnapshot(oldVal),
|
|
280
|
+
},
|
|
281
|
+
]
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
break
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
runTypeCheckingAfterChange(arr, patchRecorder)
|
|
288
|
+
|
|
289
|
+
if (!runningWithoutSnapshotOrPatches && mutate) {
|
|
290
|
+
updateInternalSnapshot(arr, mutate)
|
|
291
|
+
patchRecorder.emit(arr)
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const undefinedInsideArrayErrorMsg =
|
|
296
|
+
"undefined is not supported inside arrays since it is not serializable in JSON, consider using null instead"
|
|
297
|
+
|
|
298
|
+
// TODO: remove array parameter and just use change.object once mobx update event is fixed
|
|
299
|
+
function interceptArrayMutation(
|
|
300
|
+
array: IObservableArray,
|
|
301
|
+
change: IArrayWillChange | IArrayWillSplice
|
|
302
|
+
) {
|
|
303
|
+
assertCanWrite()
|
|
304
|
+
|
|
305
|
+
switch (change.type) {
|
|
306
|
+
case "splice":
|
|
307
|
+
{
|
|
308
|
+
if (inDevMode() && !getGlobalConfig().allowUndefinedArrayElements) {
|
|
309
|
+
const len = change.added.length
|
|
310
|
+
for (let i = 0; i < len; i++) {
|
|
311
|
+
const v = change.added[i]
|
|
312
|
+
if (v === undefined) {
|
|
313
|
+
throw failure(undefinedInsideArrayErrorMsg)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
for (let i = 0; i < change.removedCount; i++) {
|
|
319
|
+
const removedValue = change.object[change.index + i]
|
|
320
|
+
tweak(removedValue, undefined)
|
|
321
|
+
tryUntweak(removedValue)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
for (let i = 0; i < change.added.length; i++) {
|
|
325
|
+
change.added[i] = tweak(change.added[i], {
|
|
326
|
+
parent: change.object,
|
|
327
|
+
path: change.index + i,
|
|
328
|
+
})
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// we might also need to update the parent of the next indexes
|
|
332
|
+
const oldNextIndex = change.index + change.removedCount
|
|
333
|
+
const newNextIndex = change.index + change.added.length
|
|
334
|
+
|
|
335
|
+
if (oldNextIndex !== newNextIndex) {
|
|
336
|
+
for (let i = oldNextIndex, j = newNextIndex; i < change.object.length; i++, j++) {
|
|
337
|
+
setParent({
|
|
338
|
+
value: change.object[i],
|
|
339
|
+
parentPath: {
|
|
340
|
+
parent: change.object,
|
|
341
|
+
path: j,
|
|
342
|
+
},
|
|
343
|
+
indexChangeAllowed: true,
|
|
344
|
+
isDataObject: false,
|
|
345
|
+
// just re-indexing
|
|
346
|
+
cloneIfApplicable: false,
|
|
347
|
+
})
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
break
|
|
352
|
+
|
|
353
|
+
case "update":
|
|
354
|
+
if (
|
|
355
|
+
inDevMode() &&
|
|
356
|
+
!getGlobalConfig().allowUndefinedArrayElements &&
|
|
357
|
+
change.newValue === undefined
|
|
358
|
+
) {
|
|
359
|
+
throw failure(undefinedInsideArrayErrorMsg)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// TODO: should be change.object, but mobx is bugged and doesn't send the proxy
|
|
363
|
+
const oldVal = array[change.index]
|
|
364
|
+
tweak(oldVal, undefined) // set old prop obj parent to undefined
|
|
365
|
+
tryUntweak(oldVal)
|
|
366
|
+
|
|
367
|
+
change.newValue = tweak(change.newValue, { parent: array, path: change.index })
|
|
368
|
+
break
|
|
369
|
+
}
|
|
370
|
+
return change
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @internal
|
|
375
|
+
*/
|
|
376
|
+
export function registerArrayTweaker() {
|
|
377
|
+
registerTweaker(TweakerPriority.Array, (value, parentPath) => {
|
|
378
|
+
if (isArray(value)) {
|
|
379
|
+
return tweakArray(value, parentPath, false)
|
|
380
|
+
}
|
|
381
|
+
return undefined
|
|
382
|
+
})
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const observableOptions = {
|
|
386
|
+
deep: false,
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function getValueAfterSplice<T>(
|
|
390
|
+
array: readonly T[],
|
|
391
|
+
i: number,
|
|
392
|
+
index: number,
|
|
393
|
+
remove: number,
|
|
394
|
+
addedItems: readonly T[]
|
|
395
|
+
) {
|
|
396
|
+
const base = i - index
|
|
397
|
+
if (base < 0) {
|
|
398
|
+
return array[i]
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (base < addedItems.length) {
|
|
402
|
+
return addedItems[base]
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return array[i - addedItems.length + remove]
|
|
406
|
+
}
|