mobx-keystone 1.20.0 → 1.22.0

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.
Files changed (174) hide show
  1. package/CHANGELOG.md +864 -853
  2. package/README.md +61 -61
  3. package/dist/mobx-keystone.esm.js +11336 -11148
  4. package/dist/mobx-keystone.esm.mjs +11336 -11148
  5. package/dist/mobx-keystone.umd.js +12229 -11738
  6. package/dist/types/actionMiddlewares/undoMiddleware.d.ts +1 -1
  7. package/dist/types/modelShared/prop.d.ts +5 -0
  8. package/dist/types/redux/connectReduxDevTools.d.ts +17 -4
  9. package/dist/types/ref/Ref.d.ts +1 -1
  10. package/dist/types/treeUtils/sandbox.d.ts +1 -1
  11. package/dist/types/types/TypeChecker.d.ts +1 -1
  12. package/dist/types/types/arrayBased/typesArray.d.ts +1 -1
  13. package/dist/types/types/arrayBased/typesTuple.d.ts +1 -1
  14. package/dist/types/types/objectBased/typesArraySet.d.ts +1 -1
  15. package/dist/types/types/objectBased/typesDataModelData.d.ts +1 -1
  16. package/dist/types/types/objectBased/typesModel.d.ts +1 -1
  17. package/dist/types/types/objectBased/typesObject.d.ts +3 -3
  18. package/dist/types/types/objectBased/typesObjectMap.d.ts +1 -1
  19. package/dist/types/types/objectBased/typesRecord.d.ts +1 -1
  20. package/dist/types/types/primitiveBased/typesPrimitive.d.ts +1 -1
  21. package/dist/types/types/types.d.ts +3 -3
  22. package/dist/types/types/utility/typesCodec.d.ts +1 -6
  23. package/dist/types/types/utility/typesCodecCore.d.ts +1 -1
  24. package/dist/types/types/utility/typesOr.d.ts +1 -1
  25. package/dist/types/types/utility/typesRefinement.d.ts +1 -1
  26. package/dist/types/types/utility/typesSkipCheck.d.ts +1 -1
  27. package/dist/types/types/utility/typesTag.d.ts +1 -1
  28. package/dist/types/wrappers/ObjectMap.d.ts +2 -0
  29. package/dist/types/wrappers/asMap.d.ts +10 -6
  30. package/package.json +14 -12
  31. package/src/action/applyAction.ts +125 -114
  32. package/src/action/applyMethodCall.ts +37 -37
  33. package/src/action/isModelAction.ts +16 -16
  34. package/src/action/middleware.ts +137 -137
  35. package/src/action/modelFlow.ts +240 -240
  36. package/src/action/modelFlowPromiseGenerator.ts +127 -125
  37. package/src/action/pendingActions.ts +42 -42
  38. package/src/action/wrapInAction.ts +156 -156
  39. package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +1 -1
  40. package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +172 -172
  41. package/src/actionMiddlewares/actionSerialization/arraySerializer.ts +20 -20
  42. package/src/actionMiddlewares/actionSerialization/dateSerializer.ts +17 -17
  43. package/src/actionMiddlewares/actionSerialization/mapSerializer.ts +42 -42
  44. package/src/actionMiddlewares/actionSerialization/objectPathSerializer.ts +50 -50
  45. package/src/actionMiddlewares/actionSerialization/objectSnapshotSerializer.ts +21 -21
  46. package/src/actionMiddlewares/actionSerialization/plainObjectSerializer.ts +24 -32
  47. package/src/actionMiddlewares/actionSerialization/primitiveSerializer.ts +51 -51
  48. package/src/actionMiddlewares/actionSerialization/setSerializer.ts +37 -37
  49. package/src/actionMiddlewares/actionTrackingMiddleware.ts +3 -3
  50. package/src/actionMiddlewares/onActionMiddleware.ts +2 -2
  51. package/src/actionMiddlewares/readonlyMiddleware.ts +2 -2
  52. package/src/actionMiddlewares/transactionMiddleware.ts +4 -4
  53. package/src/actionMiddlewares/undoMiddleware.ts +804 -796
  54. package/src/actionMiddlewares/utils.ts +2 -2
  55. package/src/computedTree/computedTree.ts +143 -143
  56. package/src/context/context.ts +1 -1
  57. package/src/dataModel/BaseDataModel.ts +194 -192
  58. package/src/dataModel/actions.ts +1 -1
  59. package/src/dataModel/newDataModel.ts +57 -57
  60. package/src/deepChange/index.ts +1 -1
  61. package/src/deepChange/onDeepChange.ts +244 -244
  62. package/src/frozen/Frozen.ts +146 -146
  63. package/src/globalConfig/globalConfig.ts +107 -107
  64. package/src/index.ts +24 -24
  65. package/src/model/BaseModel.ts +275 -273
  66. package/src/model/ModelConstructorOptions.ts +15 -15
  67. package/src/model/getModelMetadata.ts +51 -51
  68. package/src/model/index.ts +6 -6
  69. package/src/model/mixins.ts +288 -288
  70. package/src/model/newModel.ts +301 -301
  71. package/src/model/utils.ts +249 -249
  72. package/src/modelShared/modelDecorator.ts +260 -253
  73. package/src/modelShared/modelInfo.ts +50 -50
  74. package/src/modelShared/modelSymbols.ts +3 -3
  75. package/src/modelShared/newModel.ts +25 -25
  76. package/src/modelShared/prop.ts +713 -684
  77. package/src/modelShared/sharedInternalModel.ts +427 -417
  78. package/src/modelShared/utils.ts +24 -24
  79. package/src/parent/core.ts +65 -65
  80. package/src/parent/coreObjectChildren.ts +180 -180
  81. package/src/parent/detach.ts +47 -47
  82. package/src/parent/findParent.ts +1 -1
  83. package/src/parent/getChildrenObjects.ts +25 -25
  84. package/src/parent/path.ts +386 -381
  85. package/src/parent/setParent.ts +149 -149
  86. package/src/parent/walkTree.ts +1 -1
  87. package/src/patch/applyPatches.ts +187 -187
  88. package/src/patch/jsonPatch.ts +108 -108
  89. package/src/patch/patchRecorder.ts +6 -1
  90. package/src/redux/connectReduxDevTools.ts +39 -7
  91. package/src/redux/redux.ts +6 -2
  92. package/src/ref/Ref.ts +1 -1
  93. package/src/ref/core.ts +339 -345
  94. package/src/ref/customRef.ts +3 -3
  95. package/src/ref/rootRef.ts +100 -100
  96. package/src/rootStore/attachDetach.ts +89 -89
  97. package/src/rootStore/rootStore.ts +1 -1
  98. package/src/snapshot/SnapshotOf.ts +104 -104
  99. package/src/snapshot/SnapshotProcessingError.ts +45 -45
  100. package/src/snapshot/applySnapshot.ts +177 -181
  101. package/src/snapshot/cloneTreeValue.ts +30 -30
  102. package/src/snapshot/fromArraySnapshot.ts +27 -33
  103. package/src/snapshot/fromModelSnapshot.ts +70 -70
  104. package/src/snapshot/fromPlainObjectSnapshot.ts +47 -42
  105. package/src/snapshot/fromSnapshot.ts +198 -198
  106. package/src/snapshot/getSnapshot.ts +80 -79
  107. package/src/snapshot/index.ts +8 -8
  108. package/src/snapshot/internal.ts +230 -230
  109. package/src/snapshot/reconcileArraySnapshot.ts +70 -70
  110. package/src/snapshot/reconcileModelSnapshot.ts +155 -150
  111. package/src/snapshot/reconcilePlainObjectSnapshot.ts +72 -72
  112. package/src/snapshot/reconcileSnapshot.ts +88 -88
  113. package/src/standardActions/actions.ts +64 -64
  114. package/src/standardActions/objectActions.ts +60 -60
  115. package/src/transforms/ImmutableDate.ts +93 -93
  116. package/src/transforms/asMap.ts +50 -50
  117. package/src/transforms/asSet.ts +22 -22
  118. package/src/transforms/bigint.ts +20 -20
  119. package/src/transforms/date.ts +40 -40
  120. package/src/treeUtils/deepEquals.ts +55 -55
  121. package/src/treeUtils/sandbox.ts +6 -3
  122. package/src/tweaker/TweakerPriority.ts +9 -9
  123. package/src/tweaker/tweak.ts +4 -3
  124. package/src/tweaker/tweakArray.ts +530 -475
  125. package/src/tweaker/tweakFrozen.ts +42 -42
  126. package/src/tweaker/tweakModel.ts +1 -1
  127. package/src/tweaker/tweakPlainObject.ts +355 -355
  128. package/src/tweaker/typeChecking.ts +101 -101
  129. package/src/types/SnapshotTypeMismatchError.ts +46 -46
  130. package/src/types/TypeCheckError.ts +183 -184
  131. package/src/types/TypeChecker.ts +202 -185
  132. package/src/types/arrayBased/typesArray.ts +136 -137
  133. package/src/types/arrayBased/typesTuple.ts +138 -139
  134. package/src/types/createPerEntryCachedCheck.ts +166 -160
  135. package/src/types/index.ts +6 -6
  136. package/src/types/objectBased/typesArraySet.ts +133 -134
  137. package/src/types/objectBased/typesDataModelData.ts +203 -204
  138. package/src/types/objectBased/typesModel.ts +220 -221
  139. package/src/types/objectBased/typesObject.ts +308 -310
  140. package/src/types/objectBased/typesObjectMap.ts +142 -143
  141. package/src/types/objectBased/typesRecord.ts +156 -157
  142. package/src/types/objectBased/typesRef.ts +89 -89
  143. package/src/types/primitiveBased/typesEnum.ts +63 -63
  144. package/src/types/primitiveBased/typesPrimitive.ts +219 -220
  145. package/src/types/registerDefaultStandardTypeResolvers.ts +21 -21
  146. package/src/types/resolveTypeChecker.ts +78 -78
  147. package/src/types/schemas.ts +208 -208
  148. package/src/types/tProp.ts +277 -268
  149. package/src/types/typeCheck.ts +21 -21
  150. package/src/types/typeCheckErrorUtils.ts +23 -23
  151. package/src/types/types.ts +142 -142
  152. package/src/types/utility/typeToStoredData.ts +37 -37
  153. package/src/types/utility/typesBigInt.ts +15 -15
  154. package/src/types/utility/typesCodec.ts +41 -38
  155. package/src/types/utility/typesCodecCore.ts +178 -178
  156. package/src/types/utility/typesCodecSupport.ts +806 -806
  157. package/src/types/utility/typesDate.ts +27 -27
  158. package/src/types/utility/typesMap.ts +253 -222
  159. package/src/types/utility/typesOr.ts +263 -259
  160. package/src/types/utility/typesRefinement.ts +131 -125
  161. package/src/types/utility/typesSet.ts +110 -110
  162. package/src/types/utility/typesSkipCheck.ts +69 -70
  163. package/src/types/utility/typesTag.ts +88 -82
  164. package/src/types/utility/typesUnchecked.ts +40 -40
  165. package/src/utils/AnyFunction.ts +4 -4
  166. package/src/utils/ModelPool.ts +59 -59
  167. package/src/utils/chainFns.ts +18 -18
  168. package/src/utils/decorators.ts +274 -274
  169. package/src/utils/errorDiagnostics.ts +204 -204
  170. package/src/utils/index.ts +443 -372
  171. package/src/wrappers/ArraySet.ts +156 -156
  172. package/src/wrappers/ObjectMap.ts +155 -136
  173. package/src/wrappers/asMap.ts +65 -30
  174. package/src/wrappers/asSet.ts +3 -3
@@ -1,178 +1,178 @@
1
- import type { ModelPropTransform } from "../../modelShared/prop"
2
- import { lazy } from "../../utils"
3
- import { getTypeInfo } from "../getTypeInfo"
4
- import { resolveStandardType, resolveTypeChecker } from "../resolveTypeChecker"
5
- import type {
6
- AnyStandardType,
7
- AnyType,
8
- CodecType,
9
- TypeToData,
10
- TypeToSnapshotIn,
11
- TypeToSnapshotOut,
12
- } from "../schemas"
13
- import { TypeCheckError } from "../TypeCheckError"
14
- import {
15
- lateTypeChecker,
16
- TypeChecker,
17
- TypeCheckerBaseType,
18
- TypeInfo,
19
- TypeInfoGen,
20
- } from "../TypeChecker"
21
- import type { TypeToStoredData } from "./typeToStoredData"
22
-
23
- export interface RuntimeAdapter<Stored, Runtime> {
24
- toRuntime(stored: Stored, setStored?: (stored: Stored) => void): Runtime
25
- toStored(runtime: Runtime): Stored
26
- toStoredIfCached(runtime: Runtime): { found: true; value: Stored } | { found: false }
27
- }
28
-
29
- export interface ResolvedCodecSupport {
30
- hasCodec: boolean
31
- storedType: AnyStandardType
32
- adapter: RuntimeAdapter<any, any>
33
- }
34
-
35
- interface CodecMetadata {
36
- readonly typeName: string
37
- readonly encodedType: AnyType
38
- readonly is: (value: unknown) => boolean
39
- readonly transform: ModelPropTransform<unknown, unknown>
40
- }
41
-
42
- type CodecConfig<TEncodedType extends AnyType, TRuntime> = {
43
- typeName: string
44
- encodedType: TEncodedType
45
- is(value: unknown): value is TRuntime
46
- } & ModelPropTransform<TypeToData<TEncodedType>, TRuntime>
47
-
48
- export type CodecFromEncoded<TEncodedType extends AnyType, TRuntime> = CodecType<
49
- TRuntime,
50
- TypeToSnapshotIn<TEncodedType>,
51
- TypeToSnapshotOut<TEncodedType>,
52
- TypeToStoredData<TEncodedType>
53
- >
54
-
55
- const codecMetadataByType = new WeakMap<AnyStandardType, CodecMetadata>()
56
-
57
- export const identityRuntimeAdapter: RuntimeAdapter<any, any> = {
58
- toRuntime(stored) {
59
- return stored
60
- },
61
-
62
- toStored(runtime) {
63
- return runtime
64
- },
65
-
66
- toStoredIfCached(runtime) {
67
- return {
68
- found: true,
69
- value: runtime,
70
- }
71
- },
72
- }
73
-
74
- /**
75
- * `types.codec` type info.
76
- */
77
- export class CodecTypeInfo extends TypeInfo {
78
- readonly kind = "codec"
79
-
80
- get encodedTypeInfo(): TypeInfo {
81
- return getTypeInfo(this.encodedType)
82
- }
83
-
84
- constructor(
85
- thisType: AnyStandardType,
86
- readonly typeName: string,
87
- readonly encodedType: AnyStandardType
88
- ) {
89
- super(thisType)
90
- }
91
- }
92
-
93
- function setCodecMetadata(type: AnyStandardType, metadata: CodecMetadata) {
94
- codecMetadataByType.set(type, metadata)
95
- }
96
-
97
- export function getCodecMetadata(type: AnyStandardType) {
98
- return codecMetadataByType.get(type)
99
- }
100
-
101
- export function createCodecType<TEncodedType extends AnyType, TRuntime>(
102
- { typeName, encodedType, is, transform, untransform }: CodecConfig<TEncodedType, TRuntime>,
103
- baseType: TypeCheckerBaseType
104
- ): CodecFromEncoded<TEncodedType, TRuntime> {
105
- const resolvedEncodedType = lazy(() => resolveStandardType(encodedType))
106
- const typeInfoGen: TypeInfoGen = (t) => new CodecTypeInfo(t, typeName, resolvedEncodedType())
107
- const codecMetadata: CodecMetadata = {
108
- typeName,
109
- encodedType,
110
- is,
111
- transform: { transform, untransform },
112
- }
113
-
114
- const codecType = lateTypeChecker(() => {
115
- const encodedTypeChecker = resolveTypeChecker(encodedType)
116
-
117
- const thisTc: TypeChecker = new TypeChecker(
118
- baseType,
119
-
120
- (value, path, typeCheckedValue) => {
121
- if (!is(value)) {
122
- return new TypeCheckError({
123
- path,
124
- expectedTypeName: typeName,
125
- actualValue: value,
126
- typeCheckedValue,
127
- })
128
- }
129
-
130
- return encodedTypeChecker.check(
131
- untransform({
132
- transformedValue: value as TRuntime,
133
- cacheTransformedValue: () => {},
134
- }) as TypeToData<TEncodedType>,
135
- path,
136
- typeCheckedValue
137
- )
138
- },
139
-
140
- () => typeName,
141
- typeInfoGen,
142
-
143
- (value) => (is(value) ? thisTc : null),
144
-
145
- (snapshot) => {
146
- const processedSnapshot = encodedTypeChecker.fromSnapshotProcessor(snapshot)
147
- return transform({
148
- originalValue: processedSnapshot as TypeToData<TEncodedType>,
149
- cachedTransformedValue: undefined,
150
- setOriginalValue: () => {},
151
- })
152
- },
153
-
154
- (value) => {
155
- const storedValue = untransform({
156
- transformedValue: value as TRuntime,
157
- cacheTransformedValue: () => {},
158
- })
159
-
160
- return encodedTypeChecker.toSnapshotProcessor(storedValue)
161
- }
162
- )
163
-
164
- setCodecMetadata(thisTc as unknown as AnyStandardType, codecMetadata)
165
-
166
- return thisTc
167
- }, typeInfoGen) as unknown as AnyStandardType
168
-
169
- setCodecMetadata(codecType, codecMetadata)
170
-
171
- return codecType as any
172
- }
173
-
174
- export function typesCodec<TEncodedType extends AnyType, TRuntime>(
175
- config: CodecConfig<TEncodedType, TRuntime>
176
- ): CodecFromEncoded<TEncodedType, TRuntime> {
177
- return createCodecType(config, TypeCheckerBaseType.Any)
178
- }
1
+ import type { ModelPropTransform } from "../../modelShared/prop"
2
+ import { lazy } from "../../utils"
3
+ import { getTypeInfo } from "../getTypeInfo"
4
+ import { resolveStandardType, resolveTypeChecker } from "../resolveTypeChecker"
5
+ import type {
6
+ AnyStandardType,
7
+ AnyType,
8
+ CodecType,
9
+ TypeToData,
10
+ TypeToSnapshotIn,
11
+ TypeToSnapshotOut,
12
+ } from "../schemas"
13
+ import { TypeCheckError } from "../TypeCheckError"
14
+ import {
15
+ lateTypeChecker,
16
+ TypeChecker,
17
+ TypeCheckerBaseType,
18
+ TypeInfo,
19
+ type TypeInfoGen,
20
+ } from "../TypeChecker"
21
+ import type { TypeToStoredData } from "./typeToStoredData"
22
+
23
+ export interface RuntimeAdapter<Stored, Runtime> {
24
+ toRuntime(stored: Stored, setStored?: (stored: Stored) => void): Runtime
25
+ toStored(runtime: Runtime): Stored
26
+ toStoredIfCached(runtime: Runtime): { found: true; value: Stored } | { found: false }
27
+ }
28
+
29
+ export interface ResolvedCodecSupport {
30
+ hasCodec: boolean
31
+ storedType: AnyStandardType
32
+ adapter: RuntimeAdapter<any, any>
33
+ }
34
+
35
+ interface CodecMetadata {
36
+ readonly typeName: string
37
+ readonly encodedType: AnyType
38
+ readonly is: (value: unknown) => boolean
39
+ readonly transform: ModelPropTransform<unknown, unknown>
40
+ }
41
+
42
+ type CodecConfig<TEncodedType extends AnyType, TRuntime> = {
43
+ typeName: string
44
+ encodedType: TEncodedType
45
+ is(value: unknown): value is TRuntime
46
+ } & ModelPropTransform<TypeToData<TEncodedType>, TRuntime>
47
+
48
+ export type CodecFromEncoded<TEncodedType extends AnyType, TRuntime> = CodecType<
49
+ TRuntime,
50
+ TypeToSnapshotIn<TEncodedType>,
51
+ TypeToSnapshotOut<TEncodedType>,
52
+ TypeToStoredData<TEncodedType>
53
+ >
54
+
55
+ const codecMetadataByType = new WeakMap<AnyStandardType, CodecMetadata>()
56
+
57
+ export const identityRuntimeAdapter: RuntimeAdapter<any, any> = {
58
+ toRuntime(stored) {
59
+ return stored
60
+ },
61
+
62
+ toStored(runtime) {
63
+ return runtime
64
+ },
65
+
66
+ toStoredIfCached(runtime) {
67
+ return {
68
+ found: true,
69
+ value: runtime,
70
+ }
71
+ },
72
+ }
73
+
74
+ /**
75
+ * `types.codec` type info.
76
+ */
77
+ export class CodecTypeInfo extends TypeInfo {
78
+ readonly kind = "codec"
79
+ readonly typeName: string
80
+ readonly encodedType: AnyStandardType
81
+
82
+ get encodedTypeInfo(): TypeInfo {
83
+ return getTypeInfo(this.encodedType)
84
+ }
85
+
86
+ constructor(thisType: AnyStandardType, typeName: string, encodedType: AnyStandardType) {
87
+ super(thisType)
88
+ this.typeName = typeName
89
+ this.encodedType = encodedType
90
+ }
91
+ }
92
+
93
+ function setCodecMetadata(type: AnyStandardType, metadata: CodecMetadata) {
94
+ codecMetadataByType.set(type, metadata)
95
+ }
96
+
97
+ export function getCodecMetadata(type: AnyStandardType) {
98
+ return codecMetadataByType.get(type)
99
+ }
100
+
101
+ export function createCodecType<TEncodedType extends AnyType, TRuntime>(
102
+ { typeName, encodedType, is, transform, untransform }: CodecConfig<TEncodedType, TRuntime>,
103
+ baseType: TypeCheckerBaseType
104
+ ): CodecFromEncoded<TEncodedType, TRuntime> {
105
+ const resolvedEncodedType = lazy(() => resolveStandardType(encodedType))
106
+ const typeInfoGen: TypeInfoGen = (t) => new CodecTypeInfo(t, typeName, resolvedEncodedType())
107
+ const codecMetadata: CodecMetadata = {
108
+ typeName,
109
+ encodedType,
110
+ is,
111
+ transform: { transform, untransform },
112
+ }
113
+
114
+ const codecType = lateTypeChecker(() => {
115
+ const encodedTypeChecker = resolveTypeChecker(encodedType)
116
+
117
+ const thisTc: TypeChecker = new TypeChecker(
118
+ baseType,
119
+
120
+ (value, path, typeCheckedValue) => {
121
+ if (!is(value)) {
122
+ return new TypeCheckError({
123
+ path,
124
+ expectedTypeName: typeName,
125
+ actualValue: value,
126
+ typeCheckedValue,
127
+ })
128
+ }
129
+
130
+ return encodedTypeChecker.check(
131
+ untransform({
132
+ transformedValue: value as TRuntime,
133
+ cacheTransformedValue: () => {},
134
+ }) as TypeToData<TEncodedType>,
135
+ path,
136
+ typeCheckedValue
137
+ )
138
+ },
139
+
140
+ () => typeName,
141
+ typeInfoGen,
142
+
143
+ (value) => (is(value) ? thisTc : null),
144
+
145
+ (snapshot) => {
146
+ const processedSnapshot = encodedTypeChecker.fromSnapshotProcessor(snapshot)
147
+ return transform({
148
+ originalValue: processedSnapshot as TypeToData<TEncodedType>,
149
+ cachedTransformedValue: undefined,
150
+ setOriginalValue: () => {},
151
+ })
152
+ },
153
+
154
+ (value) => {
155
+ const storedValue = untransform({
156
+ transformedValue: value as TRuntime,
157
+ cacheTransformedValue: () => {},
158
+ })
159
+
160
+ return encodedTypeChecker.toSnapshotProcessor(storedValue)
161
+ }
162
+ )
163
+
164
+ setCodecMetadata(thisTc as unknown as AnyStandardType, codecMetadata)
165
+
166
+ return thisTc
167
+ }, typeInfoGen) as unknown as AnyStandardType
168
+
169
+ setCodecMetadata(codecType, codecMetadata)
170
+
171
+ return codecType as any
172
+ }
173
+
174
+ export function typesCodec<TEncodedType extends AnyType, TRuntime>(
175
+ config: CodecConfig<TEncodedType, TRuntime>
176
+ ): CodecFromEncoded<TEncodedType, TRuntime> {
177
+ return createCodecType(config, TypeCheckerBaseType.Any)
178
+ }