mobx-keystone 2.4.1 → 2.4.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx-keystone",
3
- "version": "2.4.1",
3
+ "version": "2.4.3",
4
4
  "description": "A MobX powered state management solution based on data trees with first class support for TypeScript, snapshots, patches and much more",
5
5
  "keywords": [
6
6
  "mobx",
@@ -329,12 +329,20 @@ export function sharedInternalModel<
329
329
  // Copy static props from the base without reading its lazy snapshot processor
330
330
  // accessors. This keeps subclass declaration from compiling the parent model's
331
331
  // processor graph before snapshot processing is actually needed.
332
+ // Props are defined rather than assigned, since assigning fails for keys that are
333
+ // non-writable on Function.prototype (e.g. Symbol.metadata, added by standard
334
+ // decorators).
332
335
  for (const key of Reflect.ownKeys(base)) {
333
336
  if (key === "fromSnapshotProcessor" || key === "toSnapshotProcessor") {
334
337
  continue
335
338
  }
336
339
  if (Object.prototype.propertyIsEnumerable.call(base, key)) {
337
- ;(ThisModel as any)[key] = (base as any)[key]
340
+ Object.defineProperty(ThisModel, key, {
341
+ value: (base as any)[key],
342
+ writable: true,
343
+ enumerable: true,
344
+ configurable: true,
345
+ })
338
346
  }
339
347
  }
340
348
  delete (ThisModel as any)[modelUnwrappedClassSymbol]
@@ -1,4 +1,15 @@
1
- import { entries, get, has, keys, remove, values } from "mobx"
1
+ import {
2
+ createAtom,
3
+ entries,
4
+ get,
5
+ has,
6
+ type IAtom,
7
+ type IObjectDidChange,
8
+ keys,
9
+ observe,
10
+ remove,
11
+ values,
12
+ } from "mobx"
2
13
  import { modelAction } from "../action/modelAction"
3
14
  import type { AnyModel } from "../model/BaseModel"
4
15
  import { modelIdKey } from "../model/metadata"
@@ -8,9 +19,46 @@ import { idProp } from "../modelShared/prop"
8
19
  import { typesRecord } from "../types/objectBased/typesRecord"
9
20
  import { tProp } from "../types/tProp"
10
21
  import { typesUnchecked } from "../types/utility/typesUnchecked"
11
- import { namespace, setProtoProp } from "../utils"
22
+ import { hasOwnProp, namespace, setProtoProp } from "../utils"
12
23
  import { setIfDifferent } from "../utils/setIfDifferent"
13
24
 
25
+ const objectPrototype = Object.prototype
26
+
27
+ /**
28
+ * Per items object, an atom per key named like an Object.prototype property
29
+ * (e.g. `toString` or `__proto__`) that changes when that entry is added or removed.
30
+ */
31
+ const inheritedNameKeyAtoms = new WeakMap<object, Map<string, IAtom>>()
32
+
33
+ /**
34
+ * MobX may resolve keys through the prototype chain, so the key it subscribes to for a name
35
+ * inherited from Object.prototype counts as present whether the entry exists or not, and adding
36
+ * or removing the entry does not notify. This subscribes to an atom that does.
37
+ */
38
+ function reportInheritedNameKeyObserved(items: object, key: string): void {
39
+ let atoms = inheritedNameKeyAtoms.get(items)
40
+ if (!atoms) {
41
+ const newAtoms = new Map<string, IAtom>()
42
+ // kept for as long as items (there are only so many inherited names): a computed read in
43
+ // an action can track an atom without MobX reporting it as observed, so there is no
44
+ // reliable point to dispose them
45
+ observe(items, (change: IObjectDidChange) => {
46
+ if (change.type === "add" || change.type === "remove") {
47
+ newAtoms.get(change.name as string)?.reportChanged()
48
+ }
49
+ })
50
+ inheritedNameKeyAtoms.set(items, newAtoms)
51
+ atoms = newAtoms
52
+ }
53
+
54
+ let atom = atoms.get(key)
55
+ if (!atom) {
56
+ atom = createAtom(`ObjectMap.${key}?`)
57
+ atoms.set(key, atom)
58
+ }
59
+ atom.reportObserved()
60
+ }
61
+
14
62
  const objectMapBase = Model({
15
63
  [modelIdKey]: idProp,
16
64
  items: tProp(typesRecord(typesUnchecked<any>()), () => ({})), // will be properly checked by types.objectMap(subType)
@@ -64,7 +112,15 @@ export class ObjectMap<V> extends objectMapBase implements Map<string, V> {
64
112
  }
65
113
 
66
114
  get(key: string): V | undefined {
67
- return get(this.items, key)
115
+ const items = this.items
116
+ const value = get(items, key)
117
+ // MobX may resolve keys through the prototype chain, so properties inherited from
118
+ // Object.prototype can show up as values
119
+ if (value !== undefined && key in objectPrototype && !hasOwnProp(items, key)) {
120
+ reportInheritedNameKeyObserved(items, key)
121
+ return undefined
122
+ }
123
+ return value
68
124
  }
69
125
 
70
126
  getOrInsert(key: string, defaultValue: V): V {
@@ -82,7 +138,17 @@ export class ObjectMap<V> extends objectMapBase implements Map<string, V> {
82
138
  }
83
139
 
84
140
  has(key: string): boolean {
85
- return has(this.items, key)
141
+ const items = this.items
142
+ // this read also subscribes to the key while it is missing
143
+ if (!has(items, key)) {
144
+ return false
145
+ }
146
+ // items inherits from Object.prototype, so any other key MobX reports is an own entry
147
+ if (key in objectPrototype) {
148
+ reportInheritedNameKeyObserved(items, key)
149
+ return hasOwnProp(items, key)
150
+ }
151
+ return true
86
152
  }
87
153
 
88
154
  @modelAction