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,372 +1,443 @@
1
- import * as mobx from "mobx"
2
- import {
3
- IObservableArray,
4
- isObservableArray,
5
- isObservableMap,
6
- isObservableObject,
7
- isObservableSet,
8
- ObservableMap,
9
- ObservableSet,
10
- } from "mobx"
11
- import { JSONPrimitiveValue, PrimitiveValue } from "./types"
12
-
13
- /**
14
- * A mobx-keystone error.
15
- */
16
- export class MobxKeystoneError extends Error {
17
- constructor(msg: string) {
18
- super(msg)
19
-
20
- // Set the prototype explicitly.
21
- Object.setPrototypeOf(this, new.target.prototype)
22
- }
23
- }
24
-
25
- /**
26
- * @internal
27
- */
28
- export function failure(msg: string) {
29
- return new MobxKeystoneError(msg)
30
- }
31
-
32
- const writableHiddenPropDescriptor: PropertyDescriptor = {
33
- enumerable: false,
34
- writable: true,
35
- configurable: false,
36
- value: undefined,
37
- }
38
-
39
- /**
40
- * @internal
41
- */
42
- export function addHiddenProp(object: any, propName: PropertyKey, value: any, writable = true) {
43
- if (writable) {
44
- Object.defineProperty(object, propName, writableHiddenPropDescriptor)
45
- object[propName] = value
46
- } else {
47
- Object.defineProperty(object, propName, {
48
- enumerable: false,
49
- writable,
50
- configurable: true,
51
- value,
52
- })
53
- }
54
- }
55
-
56
- /**
57
- * @internal
58
- */
59
- export function makePropReadonly<T>(object: T, propName: keyof T, enumerable: boolean) {
60
- const propDesc = Object.getOwnPropertyDescriptor(object, propName)
61
- if (propDesc) {
62
- propDesc.enumerable = enumerable
63
- if (propDesc.get) {
64
- delete propDesc.set
65
- } else {
66
- propDesc.writable = false
67
- }
68
- Object.defineProperty(object, propName, propDesc)
69
- }
70
- }
71
-
72
- /**
73
- * @internal
74
- */
75
- export function isPlainObject(value: unknown): value is Record<PropertyKey, unknown> {
76
- if (!isObject(value)) {
77
- return false
78
- }
79
- const proto = Object.getPrototypeOf(value)
80
- return proto === Object.prototype || proto === null
81
- }
82
-
83
- /**
84
- * @internal
85
- */
86
- export function isObject(value: unknown): value is Record<PropertyKey, unknown> {
87
- return value !== null && typeof value === "object"
88
- }
89
-
90
- /**
91
- * @internal
92
- */
93
- export function isPrimitive(value: unknown): value is PrimitiveValue {
94
- switch (typeof value) {
95
- case "number":
96
- case "string":
97
- case "boolean":
98
- case "undefined":
99
- case "bigint":
100
- return true
101
- default:
102
- return value === null
103
- }
104
- }
105
-
106
- /**
107
- * @internal
108
- */
109
- export function isJSONPrimitive(value: unknown): value is JSONPrimitiveValue {
110
- switch (typeof value) {
111
- case "number":
112
- return Number.isFinite(value)
113
- case "string":
114
- case "boolean":
115
- return true
116
- default:
117
- return value === null
118
- }
119
- }
120
-
121
- /**
122
- * @internal
123
- */
124
- export function deleteFromArray<T>(array: T[], value: T): boolean {
125
- const index = array.indexOf(value)
126
- if (index >= 0) {
127
- array.splice(index, 1)
128
- return true
129
- }
130
- return false
131
- }
132
-
133
- /**
134
- * @internal
135
- */
136
- export function isMap(val: unknown): val is Map<any, any> | ObservableMap {
137
- return val instanceof Map || isObservableMap(val)
138
- }
139
-
140
- /**
141
- * @internal
142
- */
143
- export function isSet(val: unknown): val is Set<any> | ObservableSet {
144
- return val instanceof Set || isObservableSet(val)
145
- }
146
-
147
- /**
148
- * @internal
149
- */
150
- export function isArray(val: unknown): val is any[] | IObservableArray {
151
- return Array.isArray(val) || isObservableArray(val)
152
- }
153
-
154
- /**
155
- * @internal
156
- */
157
- export const inDevMode = process.env.NODE_ENV !== "production"
158
-
159
- /**
160
- * @internal
161
- */
162
- export function assertIsObject(value: unknown, argName: string): asserts value is object {
163
- if (!isObject(value)) {
164
- throw failure(`${argName} must be an object`)
165
- }
166
- }
167
-
168
- /**
169
- * @internal
170
- */
171
- export function assertIsPlainObject(value: unknown, argName: string): asserts value is object {
172
- if (!isPlainObject(value)) {
173
- throw failure(`${argName} must be a plain object`)
174
- }
175
- }
176
-
177
- /**
178
- * @internal
179
- */
180
- export function assertIsObservableObject(value: unknown, argName: string): asserts value is object {
181
- if (!isObservableObject(value)) {
182
- throw failure(`${argName} must be an observable object`)
183
- }
184
- }
185
-
186
- /**
187
- * @internal
188
- */
189
- export function assertIsObservableArray(
190
- value: unknown,
191
- argName: string
192
- ): asserts value is IObservableArray {
193
- if (!isObservableArray(value)) {
194
- throw failure(`${argName} must be an observable array`)
195
- }
196
- }
197
-
198
- /**
199
- * @internal
200
- */
201
- export function assertIsMap(
202
- value: unknown,
203
- argName: string
204
- ): asserts value is Map<any, any> | ObservableMap {
205
- if (!isMap(value)) {
206
- throw failure(`${argName} must be a map`)
207
- }
208
- }
209
-
210
- /**
211
- * @internal
212
- */
213
- export function assertIsSet(
214
- value: unknown,
215
- argName: string
216
- ): asserts value is Set<any> | ObservableSet {
217
- if (!isSet(value)) {
218
- throw failure(`${argName} must be a set`)
219
- }
220
- }
221
-
222
- /**
223
- * @internal
224
- */
225
- export function assertIsFunction(value: unknown, argName: string): asserts value is Function {
226
- if (typeof value !== "function") {
227
- throw failure(`${argName} must be a function`)
228
- }
229
- }
230
-
231
- /**
232
- * @internal
233
- */
234
- export function assertIsPrimitive(
235
- value: unknown,
236
- argName: string
237
- ): asserts value is PrimitiveValue {
238
- if (!isPrimitive(value)) {
239
- throw failure(`${argName} must be a primitive`)
240
- }
241
- }
242
-
243
- /**
244
- * @internal
245
- */
246
- export function assertIsString(value: unknown, argName: string): asserts value is string {
247
- if (typeof value !== "string") {
248
- throw failure(`${argName} must be a string`)
249
- }
250
- }
251
-
252
- /**
253
- * @internal
254
- */
255
- export const runAfterNewSymbol = Symbol("runAfterNew")
256
-
257
- /**
258
- * @internal
259
- */
260
- export const runBeforeOnInitSymbol = Symbol("runBeforeOnInit")
261
-
262
- type LateInitializationFunctionsArray = ((instance: any) => void)[]
263
-
264
- /**
265
- * @internal
266
- */
267
- export function addLateInitializationFunction(
268
- target: any,
269
- symbol: symbol,
270
- fn: (instance: any) => void
271
- ) {
272
- let array: LateInitializationFunctionsArray | undefined = target[symbol]
273
- // biome-ignore lint/suspicious/noPrototypeBuiltins: support old browsers
274
- if (!(array && Object.prototype.hasOwnProperty.call(target, symbol))) {
275
- // leave base array unmodified, create new array in the derived class
276
- array = array ? array.slice() : []
277
- addHiddenProp(target, symbol, array)
278
- }
279
- array.push(fn)
280
- }
281
-
282
- /**
283
- * @internal
284
- */
285
- export function runLateInitializationFunctions(target: any, symbol: symbol): void {
286
- const fns: LateInitializationFunctionsArray | undefined = target[symbol]
287
- if (fns) {
288
- for (const fn of fns) {
289
- fn(target)
290
- }
291
- }
292
- }
293
-
294
- const warningsAlreadyDisplayed = new Set<string>()
295
-
296
- /**
297
- * @internal
298
- */
299
- export function logWarning(type: "warn" | "error", msg: string, uniqueKey?: string): void {
300
- if (uniqueKey) {
301
- if (warningsAlreadyDisplayed.has(uniqueKey)) {
302
- return
303
- }
304
- warningsAlreadyDisplayed.add(uniqueKey)
305
- }
306
-
307
- msg = "[mobx-keystone] " + msg
308
- switch (type) {
309
- case "warn":
310
- console.warn(msg)
311
- break
312
- case "error":
313
- console.error(msg)
314
- break
315
- default:
316
- throw failure(`unknown log type - ${type}`)
317
- }
318
- }
319
-
320
- /**
321
- * @internal
322
- */
323
- export function lazy<A extends unknown[], R>(getter: (...args: A) => R): typeof getter {
324
- let memoizedValue: R
325
- let memoized = false
326
-
327
- return (...args: A): R => {
328
- if (!memoized) {
329
- memoizedValue = getter(...args)
330
- memoized = true
331
- }
332
- return memoizedValue
333
- }
334
- }
335
-
336
- /**
337
- * @internal
338
- */
339
- export const identityFn = <T>(x: T): T => x
340
-
341
- /**
342
- * @internal
343
- */
344
- export const mobx6 = {
345
- makeObservable: (mobx as any)[
346
- // just to ensure import * is kept properly
347
- String.fromCharCode("l".charCodeAt(0) + 1) + "akeObservable"
348
- ],
349
- }
350
-
351
- /**
352
- * @internal
353
- */
354
- export function propNameToSetterName(propName: string): string {
355
- return `set${propName[0].toUpperCase()}${propName.slice(1)}`
356
- }
357
-
358
- /**
359
- * @internal
360
- */
361
- export function getMobxVersion(): number {
362
- if (mobx6.makeObservable!) {
363
- return 6
364
- } else {
365
- return 5
366
- }
367
- }
368
-
369
- /**
370
- * @internal
371
- */
372
- export const namespace = "mobx-keystone"
1
+ import * as mobx from "mobx"
2
+ import {
3
+ type IObservableArray,
4
+ isObservableArray,
5
+ isObservableMap,
6
+ isObservableObject,
7
+ isObservableSet,
8
+ type ObservableMap,
9
+ type ObservableSet,
10
+ } from "mobx"
11
+ import type { JSONPrimitiveValue, PrimitiveValue } from "./types"
12
+
13
+ /**
14
+ * A mobx-keystone error.
15
+ */
16
+ export class MobxKeystoneError extends Error {
17
+ constructor(msg: string) {
18
+ super(msg)
19
+
20
+ // Set the prototype explicitly.
21
+ Object.setPrototypeOf(this, new.target.prototype)
22
+ }
23
+ }
24
+
25
+ /**
26
+ * @internal
27
+ */
28
+ export function failure(msg: string) {
29
+ return new MobxKeystoneError(msg)
30
+ }
31
+
32
+ const writableHiddenPropDescriptor: PropertyDescriptor = {
33
+ enumerable: false,
34
+ writable: true,
35
+ configurable: false,
36
+ value: undefined,
37
+ }
38
+
39
+ /**
40
+ * @internal
41
+ */
42
+ export function addHiddenProp(object: any, propName: PropertyKey, value: any, writable = true) {
43
+ if (writable) {
44
+ Object.defineProperty(object, propName, writableHiddenPropDescriptor)
45
+ object[propName] = value
46
+ } else {
47
+ Object.defineProperty(object, propName, {
48
+ enumerable: false,
49
+ writable,
50
+ configurable: true,
51
+ value,
52
+ })
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @internal
58
+ */
59
+ export function makePropReadonly<T>(object: T, propName: keyof T, enumerable: boolean) {
60
+ const propDesc = Object.getOwnPropertyDescriptor(object, propName)
61
+ if (propDesc) {
62
+ propDesc.enumerable = enumerable
63
+ if (propDesc.get) {
64
+ delete propDesc.set
65
+ } else {
66
+ propDesc.writable = false
67
+ }
68
+ Object.defineProperty(object, propName, propDesc)
69
+ }
70
+ }
71
+
72
+ /**
73
+ * @internal
74
+ */
75
+ export function isPlainObject(value: unknown): value is Record<PropertyKey, unknown> {
76
+ if (!isObject(value)) {
77
+ return false
78
+ }
79
+ const proto = Object.getPrototypeOf(value)
80
+ return proto === Object.prototype || proto === null
81
+ }
82
+
83
+ /**
84
+ * @internal
85
+ */
86
+ export function isObject(value: unknown): value is Record<PropertyKey, unknown> {
87
+ return value !== null && typeof value === "object"
88
+ }
89
+
90
+ /**
91
+ * @internal
92
+ */
93
+ export let hasOwnProp: (object: object, propName: PropertyKey) => boolean = (
94
+ object,
95
+ propName
96
+ ): boolean => {
97
+ const impl: (object: object, propName: PropertyKey) => boolean =
98
+ typeof Object.hasOwn === "function"
99
+ ? Object.hasOwn
100
+ : (object, propName) => {
101
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: legacy fallback for runtimes without Object.hasOwn.
102
+ return Object.prototype.hasOwnProperty.call(object, propName)
103
+ }
104
+
105
+ hasOwnProp = impl
106
+ return impl(object, propName)
107
+ }
108
+
109
+ /**
110
+ * @internal
111
+ */
112
+ export function setOwnProp(target: any, propName: PropertyKey, value: unknown): void {
113
+ if (propName !== "__proto__") {
114
+ target[propName] = value
115
+ return
116
+ }
117
+
118
+ Object.defineProperty(target, "__proto__", {
119
+ enumerable: true,
120
+ writable: true,
121
+ configurable: true,
122
+ value,
123
+ })
124
+ }
125
+
126
+ /**
127
+ * @internal
128
+ */
129
+ export function copyOwnEnumerableProps<TTarget extends Record<string, any>>(
130
+ target: TTarget,
131
+ source: Record<string, any>,
132
+ mapValue?: (value: unknown, key: string) => unknown
133
+ ): TTarget {
134
+ const keys = Object.keys(source)
135
+ const len = keys.length
136
+ for (let i = 0; i < len; i++) {
137
+ const key = keys[i]
138
+ const value = source[key]
139
+ setOwnProp(target, key, mapValue ? mapValue(value, key) : value)
140
+ }
141
+
142
+ return target
143
+ }
144
+
145
+ /**
146
+ * @internal
147
+ */
148
+ export function clonePlainObject<T extends Record<string, any>>(source: T): T {
149
+ return copyOwnEnumerableProps({} as T, source)
150
+ }
151
+
152
+ /**
153
+ * @internal
154
+ */
155
+ export function isPrimitive(value: unknown): value is PrimitiveValue {
156
+ switch (typeof value) {
157
+ case "number":
158
+ case "string":
159
+ case "boolean":
160
+ case "undefined":
161
+ case "bigint":
162
+ return true
163
+ default:
164
+ return value === null
165
+ }
166
+ }
167
+
168
+ /**
169
+ * @internal
170
+ */
171
+ export function isJSONPrimitive(value: unknown): value is JSONPrimitiveValue {
172
+ switch (typeof value) {
173
+ case "number":
174
+ return Number.isFinite(value)
175
+ case "string":
176
+ case "boolean":
177
+ return true
178
+ default:
179
+ return value === null
180
+ }
181
+ }
182
+
183
+ /**
184
+ * @internal
185
+ */
186
+ export function deleteFromArray<T>(array: T[], value: T): boolean {
187
+ const index = array.indexOf(value)
188
+ if (index >= 0) {
189
+ array.splice(index, 1)
190
+ return true
191
+ }
192
+ return false
193
+ }
194
+
195
+ /**
196
+ * @internal
197
+ */
198
+ export function isMap(val: unknown): val is Map<any, any> | ObservableMap {
199
+ return val instanceof Map || isObservableMap(val)
200
+ }
201
+
202
+ /**
203
+ * @internal
204
+ */
205
+ export function isSet(val: unknown): val is Set<any> | ObservableSet {
206
+ return val instanceof Set || isObservableSet(val)
207
+ }
208
+
209
+ /**
210
+ * @internal
211
+ */
212
+ export function isArray(val: unknown): val is any[] | IObservableArray {
213
+ return Array.isArray(val) || isObservableArray(val)
214
+ }
215
+
216
+ declare const process:
217
+ | {
218
+ env?: Record<string, string | undefined>
219
+ }
220
+ | undefined
221
+
222
+ /**
223
+ * @internal
224
+ */
225
+ export const inDevMode =
226
+ process?.env?.NODE_ENV !== undefined && process.env.NODE_ENV !== "production"
227
+
228
+ /**
229
+ * @internal
230
+ */
231
+ export function assertIsObject(value: unknown, argName: string): asserts value is object {
232
+ if (!isObject(value)) {
233
+ throw failure(`${argName} must be an object`)
234
+ }
235
+ }
236
+
237
+ /**
238
+ * @internal
239
+ */
240
+ export function assertIsPlainObject(value: unknown, argName: string): asserts value is object {
241
+ if (!isPlainObject(value)) {
242
+ throw failure(`${argName} must be a plain object`)
243
+ }
244
+ }
245
+
246
+ /**
247
+ * @internal
248
+ */
249
+ export function assertIsObservableObject(value: unknown, argName: string): asserts value is object {
250
+ if (!isObservableObject(value)) {
251
+ throw failure(`${argName} must be an observable object`)
252
+ }
253
+ }
254
+
255
+ /**
256
+ * @internal
257
+ */
258
+ export function assertIsObservableArray(
259
+ value: unknown,
260
+ argName: string
261
+ ): asserts value is IObservableArray {
262
+ if (!isObservableArray(value)) {
263
+ throw failure(`${argName} must be an observable array`)
264
+ }
265
+ }
266
+
267
+ /**
268
+ * @internal
269
+ */
270
+ export function assertIsMap(
271
+ value: unknown,
272
+ argName: string
273
+ ): asserts value is Map<any, any> | ObservableMap {
274
+ if (!isMap(value)) {
275
+ throw failure(`${argName} must be a map`)
276
+ }
277
+ }
278
+
279
+ /**
280
+ * @internal
281
+ */
282
+ export function assertIsSet(
283
+ value: unknown,
284
+ argName: string
285
+ ): asserts value is Set<any> | ObservableSet {
286
+ if (!isSet(value)) {
287
+ throw failure(`${argName} must be a set`)
288
+ }
289
+ }
290
+
291
+ /**
292
+ * @internal
293
+ */
294
+ export function assertIsFunction(value: unknown, argName: string): asserts value is Function {
295
+ if (typeof value !== "function") {
296
+ throw failure(`${argName} must be a function`)
297
+ }
298
+ }
299
+
300
+ /**
301
+ * @internal
302
+ */
303
+ export function assertIsPrimitive(
304
+ value: unknown,
305
+ argName: string
306
+ ): asserts value is PrimitiveValue {
307
+ if (!isPrimitive(value)) {
308
+ throw failure(`${argName} must be a primitive`)
309
+ }
310
+ }
311
+
312
+ /**
313
+ * @internal
314
+ */
315
+ export function assertIsString(value: unknown, argName: string): asserts value is string {
316
+ if (typeof value !== "string") {
317
+ throw failure(`${argName} must be a string`)
318
+ }
319
+ }
320
+
321
+ /**
322
+ * @internal
323
+ */
324
+ export const runAfterNewSymbol = Symbol("runAfterNew")
325
+
326
+ /**
327
+ * @internal
328
+ */
329
+ export const runBeforeOnInitSymbol = Symbol("runBeforeOnInit")
330
+
331
+ type LateInitializationFunctionsArray = ((instance: any) => void)[]
332
+
333
+ /**
334
+ * @internal
335
+ */
336
+ export function addLateInitializationFunction(
337
+ target: any,
338
+ symbol: symbol,
339
+ fn: (instance: any) => void
340
+ ) {
341
+ let array: LateInitializationFunctionsArray | undefined = target[symbol]
342
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: support old browsers
343
+ if (!(array && Object.prototype.hasOwnProperty.call(target, symbol))) {
344
+ // leave base array unmodified, create new array in the derived class
345
+ array = array ? array.slice() : []
346
+ addHiddenProp(target, symbol, array)
347
+ }
348
+ array.push(fn)
349
+ }
350
+
351
+ /**
352
+ * @internal
353
+ */
354
+ export function runLateInitializationFunctions(target: any, symbol: symbol): void {
355
+ const fns: LateInitializationFunctionsArray | undefined = target[symbol]
356
+ if (fns) {
357
+ for (const fn of fns) {
358
+ fn(target)
359
+ }
360
+ }
361
+ }
362
+
363
+ const warningsAlreadyDisplayed = new Set<string>()
364
+
365
+ /**
366
+ * @internal
367
+ */
368
+ export function logWarning(type: "warn" | "error", msg: string, uniqueKey?: string): void {
369
+ if (uniqueKey) {
370
+ if (warningsAlreadyDisplayed.has(uniqueKey)) {
371
+ return
372
+ }
373
+ warningsAlreadyDisplayed.add(uniqueKey)
374
+ }
375
+
376
+ msg = "[mobx-keystone] " + msg
377
+ switch (type) {
378
+ case "warn":
379
+ // biome-ignore lint/suspicious/noConsole: this internal helper intentionally emits user-facing warnings.
380
+ console.warn(msg)
381
+ break
382
+ case "error":
383
+ // biome-ignore lint/suspicious/noConsole: this internal helper intentionally emits user-facing errors.
384
+ console.error(msg)
385
+ break
386
+ default:
387
+ throw failure(`unknown log type - ${type}`)
388
+ }
389
+ }
390
+
391
+ /**
392
+ * @internal
393
+ */
394
+ export function lazy<A extends unknown[], R>(getter: (...args: A) => R): typeof getter {
395
+ let memoizedValue: R
396
+ let memoized = false
397
+
398
+ return (...args: A): R => {
399
+ if (!memoized) {
400
+ memoizedValue = getter(...args)
401
+ memoized = true
402
+ }
403
+ return memoizedValue
404
+ }
405
+ }
406
+
407
+ /**
408
+ * @internal
409
+ */
410
+ export const identityFn = <T>(x: T): T => x
411
+
412
+ /**
413
+ * @internal
414
+ */
415
+ export const mobx6 = {
416
+ makeObservable: (mobx as any)[
417
+ // just to ensure import * is kept properly
418
+ String.fromCharCode("l".charCodeAt(0) + 1) + "akeObservable"
419
+ ],
420
+ }
421
+
422
+ /**
423
+ * @internal
424
+ */
425
+ export function propNameToSetterName(propName: string): string {
426
+ return `set${propName[0].toUpperCase()}${propName.slice(1)}`
427
+ }
428
+
429
+ /**
430
+ * @internal
431
+ */
432
+ export function getMobxVersion(): number {
433
+ if (mobx6.makeObservable!) {
434
+ return 6
435
+ } else {
436
+ return 5
437
+ }
438
+ }
439
+
440
+ /**
441
+ * @internal
442
+ */
443
+ export const namespace = "mobx-keystone"