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,146 +1,146 @@
1
- import { isObservable, toJS } from "mobx"
2
- import { getGlobalConfig } from "../globalConfig"
3
- import type { FrozenData } from "../snapshot"
4
- import { tweak } from "../tweaker/tweak"
5
- import { failure, inDevMode, isPlainObject, isPrimitive } from "../utils"
6
-
7
- /**
8
- * Should freeze and plain json checks be done when creating the frozen object?
9
- */
10
- export enum FrozenCheckMode {
11
- /** Only when in dev mode */
12
- DevModeOnly = "devModeOnly",
13
- /** Always */
14
- On = "on",
15
- /** Never */
16
- Off = "off",
17
- }
18
-
19
- /**
20
- * @ignore
21
- */
22
- export const frozenKey = "$frozen"
23
-
24
- /**
25
- * A class that contains frozen data.
26
- * Use `frozen` to create an instance of this class.
27
- *
28
- * @template T Data type.
29
- */
30
- export class Frozen<T> {
31
- /**
32
- * Frozen data, deeply immutable.
33
- */
34
- readonly data: T
35
-
36
- /**
37
- * Creates an instance of Frozen.
38
- * Do not use directly, use `frozen` instead.
39
- *
40
- * @param dataToFreeze
41
- * @param checkMode
42
- */
43
- constructor(dataToFreeze: T, checkMode: FrozenCheckMode = FrozenCheckMode.DevModeOnly) {
44
- if (isObservable(dataToFreeze)) {
45
- dataToFreeze = toJS(dataToFreeze)
46
- }
47
-
48
- const check =
49
- checkMode === FrozenCheckMode.On || (inDevMode && checkMode === FrozenCheckMode.DevModeOnly)
50
- if (check) {
51
- checkDataIsSerializableAndFreeze(dataToFreeze)
52
- }
53
-
54
- this.data = dataToFreeze
55
-
56
- if (check) {
57
- Object.freeze(this.data)
58
- }
59
-
60
- tweak(this, undefined)
61
- }
62
- }
63
-
64
- /**
65
- * Marks some data as frozen. Frozen data becomes immutable (at least in dev mode), and is not enhanced
66
- * with capabilities such as getting the parent of the objects (except for the root object), it is not
67
- * made deeply observable (though the root object is observable by reference), etc.
68
- * On the other hand, this means it will be much faster to create/access. Use this for big data pieces
69
- * that are unlikely to change unless all of them change (for example lists of points for a polygon, etc).
70
- *
71
- * Note that data passed to frozen must be serializable to JSON, this is:
72
- * - primitive, plain object, or array
73
- * - without cycles
74
- *
75
- * @param data
76
- * @param checkMode
77
- */
78
- export function frozen<T>(
79
- data: T,
80
- checkMode: FrozenCheckMode = FrozenCheckMode.DevModeOnly
81
- ): Frozen<T> {
82
- return new Frozen<T>(data, checkMode)
83
- }
84
-
85
- /**
86
- * Converts some data into a frozen snapshot.
87
- *
88
- * @param data Data to convert.
89
- * @returns The frozen snapshot.
90
- */
91
- export function toFrozenSnapshot<T>(data: T): FrozenData<T> {
92
- return {
93
- [frozenKey]: true,
94
- data,
95
- }
96
- }
97
-
98
- function checkDataIsSerializableAndFreeze(data: any) {
99
- // TODO: detect cycles and throw if present?
100
-
101
- // primitives are ok
102
- if (isPrimitive(data)) {
103
- return
104
- }
105
-
106
- if (Array.isArray(data)) {
107
- const arrLen = data.length
108
- for (let i = 0; i < arrLen; i++) {
109
- const v = data[i]
110
- if (v === undefined && !getGlobalConfig().allowUndefinedArrayElements) {
111
- throw failure(
112
- "undefined is not supported inside arrays since it is not serializable in JSON, consider using null instead"
113
- )
114
- }
115
- checkDataIsSerializableAndFreeze(v)
116
- }
117
- Object.freeze(data)
118
- return
119
- }
120
-
121
- if (isPlainObject(data)) {
122
- const dataKeys = Object.keys(data)
123
- const dataKeysLen = dataKeys.length
124
- for (let i = 0; i < dataKeysLen; i++) {
125
- const k = dataKeys[i]
126
- const v = data[k]
127
-
128
- checkDataIsSerializableAndFreeze(k)
129
- checkDataIsSerializableAndFreeze(v)
130
- }
131
- Object.freeze(data)
132
- return
133
- }
134
-
135
- throw failure(`frozen data must be plainly serializable to JSON, but ${data} is not`)
136
- }
137
-
138
- /**
139
- * Checks if an snapshot is an snapshot for a frozen data.
140
- *
141
- * @param snapshot
142
- * @returns
143
- */
144
- export function isFrozenSnapshot<T = unknown>(snapshot: unknown): snapshot is FrozenData<T> {
145
- return isPlainObject(snapshot) && frozenKey in snapshot
146
- }
1
+ import { isObservable, toJS } from "mobx"
2
+ import { getGlobalConfig } from "../globalConfig"
3
+ import type { FrozenData } from "../snapshot"
4
+ import { tweak } from "../tweaker/tweak"
5
+ import { failure, inDevMode, isPlainObject, isPrimitive } from "../utils"
6
+
7
+ /**
8
+ * Should freeze and plain json checks be done when creating the frozen object?
9
+ */
10
+ export enum FrozenCheckMode {
11
+ /** Only when in dev mode */
12
+ DevModeOnly = "devModeOnly",
13
+ /** Always */
14
+ On = "on",
15
+ /** Never */
16
+ Off = "off",
17
+ }
18
+
19
+ /**
20
+ * @ignore
21
+ */
22
+ export const frozenKey = "$frozen"
23
+
24
+ /**
25
+ * A class that contains frozen data.
26
+ * Use `frozen` to create an instance of this class.
27
+ *
28
+ * @template T Data type.
29
+ */
30
+ export class Frozen<T> {
31
+ /**
32
+ * Frozen data, deeply immutable.
33
+ */
34
+ readonly data: T
35
+
36
+ /**
37
+ * Creates an instance of Frozen.
38
+ * Do not use directly, use `frozen` instead.
39
+ *
40
+ * @param dataToFreeze
41
+ * @param checkMode
42
+ */
43
+ constructor(dataToFreeze: T, checkMode: FrozenCheckMode = FrozenCheckMode.DevModeOnly) {
44
+ if (isObservable(dataToFreeze)) {
45
+ dataToFreeze = toJS(dataToFreeze)
46
+ }
47
+
48
+ const check =
49
+ checkMode === FrozenCheckMode.On || (inDevMode && checkMode === FrozenCheckMode.DevModeOnly)
50
+ if (check) {
51
+ checkDataIsSerializableAndFreeze(dataToFreeze)
52
+ }
53
+
54
+ this.data = dataToFreeze
55
+
56
+ if (check) {
57
+ Object.freeze(this.data)
58
+ }
59
+
60
+ tweak(this, undefined)
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Marks some data as frozen. Frozen data becomes immutable (at least in dev mode), and is not enhanced
66
+ * with capabilities such as getting the parent of the objects (except for the root object), it is not
67
+ * made deeply observable (though the root object is observable by reference), etc.
68
+ * On the other hand, this means it will be much faster to create/access. Use this for big data pieces
69
+ * that are unlikely to change unless all of them change (for example lists of points for a polygon, etc).
70
+ *
71
+ * Note that data passed to frozen must be serializable to JSON, this is:
72
+ * - primitive, plain object, or array
73
+ * - without cycles
74
+ *
75
+ * @param data
76
+ * @param checkMode
77
+ */
78
+ export function frozen<T>(
79
+ data: T,
80
+ checkMode: FrozenCheckMode = FrozenCheckMode.DevModeOnly
81
+ ): Frozen<T> {
82
+ return new Frozen<T>(data, checkMode)
83
+ }
84
+
85
+ /**
86
+ * Converts some data into a frozen snapshot.
87
+ *
88
+ * @param data Data to convert.
89
+ * @returns The frozen snapshot.
90
+ */
91
+ export function toFrozenSnapshot<T>(data: T): FrozenData<T> {
92
+ return {
93
+ [frozenKey]: true,
94
+ data,
95
+ }
96
+ }
97
+
98
+ function checkDataIsSerializableAndFreeze(data: any) {
99
+ // TODO: detect cycles and throw if present?
100
+
101
+ // primitives are ok
102
+ if (isPrimitive(data)) {
103
+ return
104
+ }
105
+
106
+ if (Array.isArray(data)) {
107
+ const arrLen = data.length
108
+ for (let i = 0; i < arrLen; i++) {
109
+ const v = data[i]
110
+ if (v === undefined && !getGlobalConfig().allowUndefinedArrayElements) {
111
+ throw failure(
112
+ "undefined is not supported inside arrays since it is not serializable in JSON, consider using null instead"
113
+ )
114
+ }
115
+ checkDataIsSerializableAndFreeze(v)
116
+ }
117
+ Object.freeze(data)
118
+ return
119
+ }
120
+
121
+ if (isPlainObject(data)) {
122
+ const dataKeys = Object.keys(data)
123
+ const dataKeysLen = dataKeys.length
124
+ for (let i = 0; i < dataKeysLen; i++) {
125
+ const k = dataKeys[i]
126
+ const v = data[k]
127
+
128
+ checkDataIsSerializableAndFreeze(k)
129
+ checkDataIsSerializableAndFreeze(v)
130
+ }
131
+ Object.freeze(data)
132
+ return
133
+ }
134
+
135
+ throw failure(`frozen data must be plainly serializable to JSON, but ${data} is not`)
136
+ }
137
+
138
+ /**
139
+ * Checks if an snapshot is an snapshot for a frozen data.
140
+ *
141
+ * @param snapshot
142
+ * @returns
143
+ */
144
+ export function isFrozenSnapshot<T = unknown>(snapshot: unknown): snapshot is FrozenData<T> {
145
+ return isPlainObject(snapshot) && frozenKey in snapshot
146
+ }
@@ -1,107 +1,107 @@
1
- import { nanoid } from "nanoid/non-secure"
2
- import { failure, inDevMode } from "../utils"
3
-
4
- /**
5
- * Model auto type-checking mode.
6
- */
7
- export enum ModelAutoTypeCheckingMode {
8
- /**
9
- * Auto type check models only in dev mode
10
- */
11
- DevModeOnly = "devModeOnly",
12
- /**
13
- * Auto type check models no matter the current environment
14
- */
15
- AlwaysOn = "alwaysOn",
16
- /**
17
- * Do not auto type check models no matter the current environment
18
- */
19
- AlwaysOff = "alwaysOff",
20
- }
21
-
22
- /**
23
- * Global config object.
24
- */
25
- export interface GlobalConfig {
26
- /**
27
- * Model auto type-checking mode.
28
- */
29
- modelAutoTypeChecking: ModelAutoTypeCheckingMode
30
-
31
- /**
32
- * ID generator function for model ids.
33
- */
34
- modelIdGenerator(): string
35
-
36
- /**
37
- * Allow array elements to be `undefined`.
38
- */
39
- allowUndefinedArrayElements: boolean
40
-
41
- /**
42
- * Enables/disables warnings related to duplicated model names (defaults to `true`).
43
- * Usually set to false in test environments (e.g. vitest).
44
- */
45
- showDuplicateModelNameWarnings: boolean
46
- }
47
-
48
- let localId = 0
49
- const localBaseId = nanoid()
50
-
51
- function defaultModelIdGenerator(): string {
52
- // we use base 36 for local id since it is short and fast
53
- const id = localId.toString(36) + "-" + localBaseId
54
- localId++
55
- return id
56
- }
57
-
58
- // defaults
59
- let globalConfig: GlobalConfig = {
60
- modelAutoTypeChecking: ModelAutoTypeCheckingMode.DevModeOnly,
61
- modelIdGenerator: defaultModelIdGenerator,
62
- allowUndefinedArrayElements: false,
63
- showDuplicateModelNameWarnings: true,
64
- }
65
-
66
- /**
67
- * Partially sets the current global config.
68
- *
69
- * @param config Partial object with the new configurations. Options not included in the object won't be changed.
70
- */
71
- export function setGlobalConfig(config: Partial<GlobalConfig>) {
72
- globalConfig = Object.freeze({
73
- ...globalConfig,
74
- ...config,
75
- })
76
- }
77
-
78
- /**
79
- * Returns the current global config object.
80
- *
81
- * @returns
82
- */
83
- export function getGlobalConfig(): Readonly<GlobalConfig> {
84
- return globalConfig
85
- }
86
-
87
- /**
88
- * @internal
89
- *
90
- * Returns if the auto type checking for models is enabled.
91
- *
92
- * @returns
93
- */
94
- export function isModelAutoTypeCheckingEnabled() {
95
- switch (getGlobalConfig().modelAutoTypeChecking) {
96
- case ModelAutoTypeCheckingMode.DevModeOnly:
97
- return inDevMode
98
- case ModelAutoTypeCheckingMode.AlwaysOff:
99
- return false
100
- case ModelAutoTypeCheckingMode.AlwaysOn:
101
- return true
102
- default:
103
- throw failure(
104
- `invalid 'modelAutoTypeChecking' config value - ${globalConfig.modelAutoTypeChecking}`
105
- )
106
- }
107
- }
1
+ import { nanoid } from "nanoid/non-secure"
2
+ import { failure, inDevMode } from "../utils"
3
+
4
+ /**
5
+ * Model auto type-checking mode.
6
+ */
7
+ export enum ModelAutoTypeCheckingMode {
8
+ /**
9
+ * Auto type check models only in dev mode
10
+ */
11
+ DevModeOnly = "devModeOnly",
12
+ /**
13
+ * Auto type check models no matter the current environment
14
+ */
15
+ AlwaysOn = "alwaysOn",
16
+ /**
17
+ * Do not auto type check models no matter the current environment
18
+ */
19
+ AlwaysOff = "alwaysOff",
20
+ }
21
+
22
+ /**
23
+ * Global config object.
24
+ */
25
+ export interface GlobalConfig {
26
+ /**
27
+ * Model auto type-checking mode.
28
+ */
29
+ modelAutoTypeChecking: ModelAutoTypeCheckingMode
30
+
31
+ /**
32
+ * ID generator function for model ids.
33
+ */
34
+ modelIdGenerator(): string
35
+
36
+ /**
37
+ * Allow array elements to be `undefined`.
38
+ */
39
+ allowUndefinedArrayElements: boolean
40
+
41
+ /**
42
+ * Enables/disables warnings related to duplicated model names (defaults to `true`).
43
+ * Usually set to false in test environments (e.g. vitest).
44
+ */
45
+ showDuplicateModelNameWarnings: boolean
46
+ }
47
+
48
+ let localId = 0
49
+ const localBaseId = nanoid()
50
+
51
+ function defaultModelIdGenerator(): string {
52
+ // we use base 36 for local id since it is short and fast
53
+ const id = localId.toString(36) + "-" + localBaseId
54
+ localId++
55
+ return id
56
+ }
57
+
58
+ // defaults
59
+ let globalConfig: GlobalConfig = {
60
+ modelAutoTypeChecking: ModelAutoTypeCheckingMode.DevModeOnly,
61
+ modelIdGenerator: defaultModelIdGenerator,
62
+ allowUndefinedArrayElements: false,
63
+ showDuplicateModelNameWarnings: true,
64
+ }
65
+
66
+ /**
67
+ * Partially sets the current global config.
68
+ *
69
+ * @param config Partial object with the new configurations. Options not included in the object won't be changed.
70
+ */
71
+ export function setGlobalConfig(config: Partial<GlobalConfig>) {
72
+ globalConfig = Object.freeze({
73
+ ...globalConfig,
74
+ ...config,
75
+ })
76
+ }
77
+
78
+ /**
79
+ * Returns the current global config object.
80
+ *
81
+ * @returns
82
+ */
83
+ export function getGlobalConfig(): Readonly<GlobalConfig> {
84
+ return globalConfig
85
+ }
86
+
87
+ /**
88
+ * @internal
89
+ *
90
+ * Returns if the auto type checking for models is enabled.
91
+ *
92
+ * @returns
93
+ */
94
+ export function isModelAutoTypeCheckingEnabled() {
95
+ switch (getGlobalConfig().modelAutoTypeChecking) {
96
+ case ModelAutoTypeCheckingMode.DevModeOnly:
97
+ return inDevMode
98
+ case ModelAutoTypeCheckingMode.AlwaysOff:
99
+ return false
100
+ case ModelAutoTypeCheckingMode.AlwaysOn:
101
+ return true
102
+ default:
103
+ throw failure(
104
+ `invalid 'modelAutoTypeChecking' config value - ${globalConfig.modelAutoTypeChecking}`
105
+ )
106
+ }
107
+ }
package/src/index.ts CHANGED
@@ -1,24 +1,24 @@
1
- export * from "./action"
2
- export * from "./actionMiddlewares"
3
- export * from "./computedTree"
4
- export * from "./context"
5
- export * from "./dataModel"
6
- export * from "./deepChange"
7
- export * from "./frozen"
8
- export * from "./globalConfig"
9
- export * from "./model"
10
- export * from "./modelShared"
11
- export * from "./parent"
12
- export * from "./patch"
13
- export * from "./redux"
14
- export * from "./ref"
15
- export * from "./rootStore"
16
- export * from "./snapshot"
17
- export * from "./standardActions"
18
- export * from "./transforms"
19
- export * from "./treeUtils"
20
- export * from "./tweaker"
21
- export * from "./types"
22
- export * from "./utils"
23
- export * from "./utils/tag"
24
- export * from "./wrappers"
1
+ export * from "./action"
2
+ export * from "./actionMiddlewares"
3
+ export * from "./computedTree"
4
+ export * from "./context"
5
+ export * from "./dataModel"
6
+ export * from "./deepChange"
7
+ export * from "./frozen"
8
+ export * from "./globalConfig"
9
+ export * from "./model"
10
+ export * from "./modelShared"
11
+ export * from "./parent"
12
+ export * from "./patch"
13
+ export * from "./redux"
14
+ export * from "./ref"
15
+ export * from "./rootStore"
16
+ export * from "./snapshot"
17
+ export * from "./standardActions"
18
+ export * from "./transforms"
19
+ export * from "./treeUtils"
20
+ export * from "./tweaker"
21
+ export * from "./types"
22
+ export * from "./utils"
23
+ export * from "./utils/tag"
24
+ export * from "./wrappers"