mobx-keystone 1.25.0 → 2.1.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 (141) hide show
  1. package/CHANGELOG.md +145 -0
  2. package/dist/mobx-keystone.esm.js +3627 -1540
  3. package/dist/mobx-keystone.esm.mjs +3627 -1540
  4. package/dist/mobx-keystone.umd.js +3638 -1543
  5. package/dist/types/actionMiddlewares/actionSerialization/dateSerializer.d.ts +1 -1
  6. package/dist/types/actionMiddlewares/transactionMiddleware.d.ts +1 -1
  7. package/dist/types/actionMiddlewares/undoMiddleware.d.ts +17 -10
  8. package/dist/types/dataModel/dataModelInstanceCache.d.ts +1 -0
  9. package/dist/types/deepChange/onDeepChange.d.ts +9 -0
  10. package/dist/types/parent/arrayPaths.d.ts +1 -0
  11. package/dist/types/parent/detach.d.ts +1 -1
  12. package/dist/types/parent/findChildren.d.ts +1 -1
  13. package/dist/types/parent/treeOrder.d.ts +1 -0
  14. package/dist/types/patch/applyPatches.d.ts +3 -0
  15. package/dist/types/redux/connectReduxDevTools.d.ts +2 -1
  16. package/dist/types/ref/modelRefIdIndex.d.ts +1 -0
  17. package/dist/types/ref/nodesById.d.ts +1 -0
  18. package/dist/types/ref/refIdIndex.d.ts +1 -0
  19. package/dist/types/snapshot/SnapshotOf.d.ts +2 -2
  20. package/dist/types/snapshot/applySnapshot.d.ts +1 -0
  21. package/dist/types/snapshot/fromSnapshot.d.ts +0 -3
  22. package/dist/types/standardActions/arrayActions.d.ts +1 -1
  23. package/dist/types/treeUtils/sandbox.d.ts +0 -4
  24. package/dist/types/tweaker/changeRollback.d.ts +1 -0
  25. package/dist/types/tweaker/tweakedChangeListeners.d.ts +1 -0
  26. package/dist/types/types/TypeCheckError.d.ts +1 -0
  27. package/dist/types/types/utility/typesRefinement.d.ts +2 -2
  28. package/dist/types/utils/ModelPool.d.ts +7 -1
  29. package/dist/types/utils/chunks.d.ts +1 -0
  30. package/dist/types/utils/forEachWithDelayedThrow.d.ts +1 -0
  31. package/dist/types/utils/index.d.ts +13 -0
  32. package/dist/types/utils/mapUtils.d.ts +2 -2
  33. package/dist/types/utils/setLike.d.ts +1 -0
  34. package/package.json +2 -2
  35. package/src/action/applyAction.ts +1 -1
  36. package/src/action/middleware.ts +17 -36
  37. package/src/action/modelFlow.ts +4 -5
  38. package/src/action/mutationBatch.ts +10 -10
  39. package/src/action/pendingActions.ts +3 -4
  40. package/src/actionMiddlewares/actionSerialization/actionSerialization.ts +19 -14
  41. package/src/actionMiddlewares/actionSerialization/applySerializedAction.ts +29 -20
  42. package/src/actionMiddlewares/actionSerialization/arraySerializer.ts +1 -1
  43. package/src/actionMiddlewares/actionSerialization/dateSerializer.ts +4 -3
  44. package/src/actionMiddlewares/actionSerialization/mapSerializer.ts +4 -12
  45. package/src/actionMiddlewares/actionSerialization/setSerializer.ts +4 -13
  46. package/src/actionMiddlewares/actionTrackingMiddleware.ts +11 -13
  47. package/src/actionMiddlewares/transactionMiddleware.ts +55 -25
  48. package/src/actionMiddlewares/undoMiddleware.ts +112 -59
  49. package/src/actionMiddlewares/utils.ts +2 -1
  50. package/src/computedTree/computedTree.ts +14 -3
  51. package/src/context/context.ts +17 -45
  52. package/src/dataModel/BaseDataModel.ts +20 -16
  53. package/src/dataModel/dataModelInstanceCache.ts +8 -0
  54. package/src/deepChange/onDeepChange.ts +179 -24
  55. package/src/frozen/Frozen.ts +50 -33
  56. package/src/model/BaseModel.ts +10 -13
  57. package/src/model/newModel.ts +21 -24
  58. package/src/model/utils.ts +2 -2
  59. package/src/modelShared/modelClassInitializer.ts +3 -2
  60. package/src/modelShared/modelDecorator.ts +39 -39
  61. package/src/modelShared/modelInfo.ts +1 -1
  62. package/src/modelShared/prop.ts +6 -9
  63. package/src/modelShared/sharedInternalModel.ts +16 -3
  64. package/src/parent/arrayPaths.ts +242 -0
  65. package/src/parent/coreObjectChildren.ts +300 -48
  66. package/src/parent/detach.ts +1 -1
  67. package/src/parent/findChildren.ts +4 -7
  68. package/src/parent/onChildAttachedTo.ts +157 -55
  69. package/src/parent/path.ts +15 -5
  70. package/src/parent/setParent.ts +34 -15
  71. package/src/parent/treeOrder.ts +53 -0
  72. package/src/parent/walkTree.ts +12 -101
  73. package/src/patch/applyPatches.ts +74 -34
  74. package/src/patch/emitPatch.ts +142 -26
  75. package/src/patch/jsonPatch.ts +5 -0
  76. package/src/patch/patchRecorder.ts +18 -14
  77. package/src/redux/connectReduxDevTools.ts +24 -18
  78. package/src/redux/redux.ts +5 -21
  79. package/src/ref/core.ts +81 -29
  80. package/src/ref/modelRefIdIndex.ts +131 -0
  81. package/src/ref/nodesById.ts +60 -0
  82. package/src/ref/refIdIndex.ts +209 -0
  83. package/src/rootStore/attachDetach.ts +27 -16
  84. package/src/rootStore/rootStore.ts +10 -6
  85. package/src/snapshot/SnapshotOf.ts +2 -2
  86. package/src/snapshot/applySnapshot.ts +27 -19
  87. package/src/snapshot/clone.ts +1 -2
  88. package/src/snapshot/cloneTreeValue.ts +1 -2
  89. package/src/snapshot/fromModelSnapshot.ts +6 -2
  90. package/src/snapshot/fromPlainObjectSnapshot.ts +3 -8
  91. package/src/snapshot/fromSnapshot.ts +2 -6
  92. package/src/snapshot/internal.ts +89 -19
  93. package/src/snapshot/onSnapshot.ts +12 -1
  94. package/src/snapshot/reconcileModelSnapshot.ts +6 -6
  95. package/src/snapshot/reconcilePlainObjectSnapshot.ts +5 -4
  96. package/src/standardActions/arrayActions.ts +12 -2
  97. package/src/standardActions/objectActions.ts +3 -2
  98. package/src/transforms/ImmutableDate.ts +4 -0
  99. package/src/transforms/asMap.ts +3 -1
  100. package/src/treeUtils/deepEquals.ts +15 -13
  101. package/src/treeUtils/draft.ts +8 -0
  102. package/src/treeUtils/sandbox.ts +31 -27
  103. package/src/tweaker/changeRollback.ts +218 -0
  104. package/src/tweaker/core.ts +33 -22
  105. package/src/tweaker/treeNodeMetadata.ts +12 -2
  106. package/src/tweaker/tweak.ts +183 -19
  107. package/src/tweaker/tweakArray.ts +176 -33
  108. package/src/tweaker/tweakPlainObject.ts +117 -39
  109. package/src/tweaker/tweakedChangeListeners.ts +61 -0
  110. package/src/tweaker/typeChecking.ts +25 -15
  111. package/src/types/TypeCheckError.ts +17 -19
  112. package/src/types/arrayBased/typesArray.ts +10 -11
  113. package/src/types/createCachedTypeCheck.ts +541 -88
  114. package/src/types/objectBased/typesDataModelData.ts +4 -2
  115. package/src/types/objectBased/typesModel.ts +4 -2
  116. package/src/types/objectBased/typesObject.ts +20 -16
  117. package/src/types/objectBased/typesRecord.ts +8 -18
  118. package/src/types/objectBased/typesRef.ts +2 -2
  119. package/src/types/primitiveBased/typesEnum.ts +4 -2
  120. package/src/types/primitiveBased/typesPrimitive.ts +9 -4
  121. package/src/types/typeCheckErrorUtils.ts +1 -0
  122. package/src/types/utility/typesCodecSupport.ts +97 -56
  123. package/src/types/utility/typesMap.ts +24 -41
  124. package/src/types/utility/typesRefinement.ts +3 -2
  125. package/src/types/utility/typesSet.ts +24 -0
  126. package/src/utils/ModelPool.ts +78 -22
  127. package/src/utils/chainFns.ts +4 -0
  128. package/src/utils/chunks.ts +271 -0
  129. package/src/utils/decorators.ts +1 -1
  130. package/src/utils/errorDiagnostics.ts +6 -2
  131. package/src/utils/forEachWithDelayedThrow.ts +83 -0
  132. package/src/utils/index.ts +71 -2
  133. package/src/utils/mapUtils.ts +3 -7
  134. package/src/utils/setIfDifferent.ts +3 -2
  135. package/src/utils/setLike.ts +134 -0
  136. package/src/wrappers/ArraySet.ts +5 -9
  137. package/src/wrappers/ObjectMap.ts +15 -11
  138. package/src/wrappers/asMap.ts +30 -18
  139. package/src/wrappers/asSet.ts +36 -12
  140. package/dist/types/action/modelFlowPromiseGenerator.d.ts +0 -1
  141. package/src/action/modelFlowPromiseGenerator.ts +0 -127
@@ -11,7 +11,14 @@ import {
11
11
  transaction,
12
12
  untracked,
13
13
  } from "mobx"
14
- import { assertIsObservableArray, assertIsSet, failure, getMobxVersion, inDevMode } from "../utils"
14
+ import {
15
+ assertIsObservableArray,
16
+ assertIsSet,
17
+ failure,
18
+ getMobxVersion,
19
+ inDevMode,
20
+ isEqualOrBothNaN,
21
+ } from "../utils"
15
22
  import { tag } from "../utils/tag"
16
23
 
17
24
  const observableSetBackedByObservableArray = <T>(
@@ -26,9 +33,9 @@ const observableSetBackedByObservableArray = <T>(
26
33
  const set = transaction(() =>
27
34
  untracked(() => {
28
35
  if (getMobxVersion() >= 6) {
29
- return observable.set(array)
36
+ return observable.set(array, { deep: false })
30
37
  } else {
31
- const set = observable.set()
38
+ const set = observable.set<T>(undefined, { deep: false })
32
39
  runInAction(() => {
33
40
  array.forEach((item) => {
34
41
  set.add(item)
@@ -98,28 +105,45 @@ const observableSetBackedByObservableArray = <T>(
98
105
  intercept(
99
106
  set,
100
107
  action((change: ISetWillChange<T>) => {
101
- if (setAlreadyChanged) {
102
- return null
103
- }
104
-
105
108
  if (arrayAlreadyChanged) {
106
109
  return change
107
110
  }
108
111
 
112
+ if (setAlreadyChanged) {
113
+ return null
114
+ }
115
+
109
116
  setAlreadyChanged = true
110
117
 
111
118
  try {
112
119
  switch (change.type) {
113
120
  case "add": {
114
- array.push(change.newValue)
121
+ if (!set.has(change.newValue)) {
122
+ const oldLength = array.length
123
+ array.push(change.newValue)
124
+ if (array.length === oldLength) return null
125
+ const storedValue = array[array.length - 1]
126
+ if (getMobxVersion() >= 6) {
127
+ change.newValue = storedValue
128
+ } else if (!isEqualOrBothNaN(storedValue, change.newValue)) {
129
+ // MobX 4/5 ignore replacement values returned by set interceptors.
130
+ arrayAlreadyChanged = true
131
+ try {
132
+ set.add(storedValue)
133
+ } finally {
134
+ arrayAlreadyChanged = false
135
+ }
136
+ return null
137
+ }
138
+ }
115
139
  break
116
140
  }
117
141
 
118
142
  case "delete": {
119
- const i = array.indexOf(change.oldValue)
120
- if (i >= 0) {
121
- array.splice(i, 1)
122
- }
143
+ const i = Number.isNaN(change.oldValue)
144
+ ? array.findIndex(Number.isNaN)
145
+ : array.indexOf(change.oldValue)
146
+ if (i >= 0 && array.splice(i, 1).length === 0) return null
123
147
  break
124
148
  }
125
149
 
@@ -1 +0,0 @@
1
- export declare function promiseGenerator(this: Promise<any>): any;
@@ -1,127 +0,0 @@
1
- // biome-ignore-all lint: transpiled TypeScript ES5 helper kept verbatim; its generated shape intentionally violates style and complexity rules.
2
-
3
- /*
4
- function* promiseGenerator<T>(
5
- this: Promise<T>
6
- ) {
7
- const ret: T = yield this
8
- return ret
9
- }
10
- */
11
-
12
- // above code but compiled by TS for ES5
13
- // so we don't include a dependency to regenerator runtime
14
-
15
- const __generator = function (thisArg: any, body: any) {
16
- let _: any = {
17
- label: 0,
18
- sent: function () {
19
- if (t[0] & 1) throw t[1]
20
- return t[1]
21
- },
22
- trys: [],
23
- ops: [],
24
- },
25
- f: any,
26
- y: any,
27
- t: any,
28
- g: any
29
- return (
30
- (g = { next: verb(0), throw: verb(1), return: verb(2) }),
31
- typeof Symbol === "function" &&
32
- (g[Symbol.iterator] = function () {
33
- return this
34
- }),
35
- g
36
- )
37
- function verb(n: any) {
38
- return function (v: any) {
39
- return step([n, v])
40
- }
41
- }
42
- function step(op: any) {
43
- if (f) throw new TypeError("Generator is already executing.")
44
- while (_)
45
- try {
46
- if (
47
- ((f = 1),
48
- y &&
49
- (t =
50
- op[0] & 2
51
- ? y["return"]
52
- : op[0]
53
- ? y["throw"] || ((t = y["return"]) && t.call(y), 0)
54
- : y.next) &&
55
- !(t = t.call(y, op[1])).done)
56
- )
57
- return t
58
- if (((y = 0), t)) op = [op[0] & 2, t.value]
59
- switch (op[0]) {
60
- case 0:
61
- case 1:
62
- t = op
63
- break
64
- case 4:
65
- _.label++
66
- return { value: op[1], done: false }
67
- case 5:
68
- _.label++
69
- y = op[1]
70
- op = [0]
71
- continue
72
- case 7:
73
- op = _.ops.pop()
74
- _.trys.pop()
75
- continue
76
- default:
77
- if (
78
- !((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
79
- (op[0] === 6 || op[0] === 2)
80
- ) {
81
- _ = 0
82
- continue
83
- }
84
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
85
- _.label = op[1]
86
- break
87
- }
88
- if (op[0] === 6 && _.label < t[1]) {
89
- _.label = t[1]
90
- t = op
91
- break
92
- }
93
- if (t && _.label < t[2]) {
94
- _.label = t[2]
95
- _.ops.push(op)
96
- break
97
- }
98
- if (t[2]) _.ops.pop()
99
- _.trys.pop()
100
- continue
101
- }
102
- op = body.call(thisArg, _)
103
- } catch (e) {
104
- op = [6, e]
105
- y = 0
106
- } finally {
107
- f = t = 0
108
- }
109
- if (op[0] & 5) throw op[1]
110
- return { value: op[0] ? op[1] : void 0, done: true }
111
- }
112
- }
113
-
114
- export function promiseGenerator(this: Promise<any>) {
115
- let ret
116
- return __generator(this, function (this: any, _a: any) {
117
- switch (_a.label) {
118
- case 0:
119
- return [4 /*yield*/, this]
120
- case 1:
121
- ret = _a.sent()
122
- return [2 /*return*/, ret]
123
- default:
124
- return
125
- }
126
- })
127
- }