mobx 6.1.8 → 6.9.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 (91) hide show
  1. package/CHANGELOG.md +224 -7
  2. package/README.md +54 -52
  3. package/dist/api/annotation.d.ts +6 -12
  4. package/dist/api/autorun.d.ts +4 -4
  5. package/dist/api/flow.d.ts +1 -0
  6. package/dist/api/intercept.d.ts +5 -5
  7. package/dist/api/object-api.d.ts +2 -0
  8. package/dist/api/observable.d.ts +5 -1
  9. package/dist/api/observe.d.ts +4 -4
  10. package/dist/api/tojs.d.ts +4 -1
  11. package/dist/api/when.d.ts +8 -0
  12. package/dist/core/atom.d.ts +1 -1
  13. package/dist/core/computedvalue.d.ts +0 -1
  14. package/dist/core/globalstate.d.ts +4 -0
  15. package/dist/core/reaction.d.ts +2 -2
  16. package/dist/errors.d.ts +4 -2
  17. package/dist/internal.d.ts +1 -0
  18. package/dist/mobx.cjs.development.js +1477 -1719
  19. package/dist/mobx.cjs.development.js.map +1 -1
  20. package/dist/mobx.cjs.production.min.js +1 -1
  21. package/dist/mobx.cjs.production.min.js.map +1 -1
  22. package/dist/mobx.d.ts +1 -1
  23. package/dist/mobx.esm.development.js +1475 -1720
  24. package/dist/mobx.esm.development.js.map +1 -1
  25. package/dist/mobx.esm.js +1492 -1726
  26. package/dist/mobx.esm.js.map +1 -1
  27. package/dist/mobx.esm.production.min.js +1 -1
  28. package/dist/mobx.esm.production.min.js.map +1 -1
  29. package/dist/mobx.umd.development.js +1477 -1719
  30. package/dist/mobx.umd.development.js.map +1 -1
  31. package/dist/mobx.umd.production.min.js +1 -1
  32. package/dist/mobx.umd.production.min.js.map +1 -1
  33. package/dist/types/actionannotation.d.ts +7 -1
  34. package/dist/types/autoannotation.d.ts +3 -0
  35. package/dist/types/observablemap.d.ts +5 -4
  36. package/dist/types/observableobject.d.ts +6 -9
  37. package/dist/types/observableset.d.ts +4 -4
  38. package/dist/types/observablevalue.d.ts +3 -5
  39. package/dist/utils/utils.d.ts +4 -4
  40. package/package.json +1 -1
  41. package/src/api/action.ts +8 -3
  42. package/src/api/annotation.ts +14 -44
  43. package/src/api/autorun.ts +38 -17
  44. package/src/api/computed.ts +5 -2
  45. package/src/api/configure.ts +6 -2
  46. package/src/api/decorators.ts +1 -1
  47. package/src/api/extendobservable.ts +12 -6
  48. package/src/api/extras.ts +4 -2
  49. package/src/api/flow.ts +17 -5
  50. package/src/api/intercept-read.ts +4 -2
  51. package/src/api/intercept.ts +10 -7
  52. package/src/api/iscomputed.ts +14 -8
  53. package/src/api/isobservable.ts +10 -4
  54. package/src/api/makeObservable.ts +35 -36
  55. package/src/api/object-api.ts +42 -14
  56. package/src/api/observable.ts +39 -25
  57. package/src/api/observe.ts +9 -6
  58. package/src/api/tojs.ts +20 -10
  59. package/src/api/trace.ts +6 -2
  60. package/src/api/when.ts +34 -9
  61. package/src/core/action.ts +11 -5
  62. package/src/core/atom.ts +9 -2
  63. package/src/core/computedvalue.ts +48 -27
  64. package/src/core/derivation.ts +34 -12
  65. package/src/core/globalstate.ts +25 -7
  66. package/src/core/observable.ts +23 -13
  67. package/src/core/reaction.ts +16 -7
  68. package/src/core/spy.ts +20 -7
  69. package/src/errors.ts +16 -3
  70. package/src/internal.ts +1 -0
  71. package/src/mobx.ts +6 -2
  72. package/src/types/actionannotation.ts +30 -52
  73. package/src/types/autoannotation.ts +107 -0
  74. package/src/types/computedannotation.ts +7 -37
  75. package/src/types/dynamicobject.ts +12 -6
  76. package/src/types/flowannotation.ts +40 -41
  77. package/src/types/intercept-utils.ts +9 -3
  78. package/src/types/legacyobservablearray.ts +26 -3
  79. package/src/types/listen-utils.ts +6 -2
  80. package/src/types/modifiers.ts +52 -16
  81. package/src/types/observableannotation.ts +7 -34
  82. package/src/types/observablearray.ts +106 -45
  83. package/src/types/observablemap.ts +71 -35
  84. package/src/types/observableobject.ts +88 -82
  85. package/src/types/observableset.ts +32 -13
  86. package/src/types/observablevalue.ts +16 -10
  87. package/src/types/overrideannotation.ts +4 -2
  88. package/src/types/type-utils.ts +34 -12
  89. package/src/utils/comparer.ts +5 -1
  90. package/src/utils/eq.ts +45 -15
  91. package/src/utils/utils.ts +39 -16
package/src/utils/eq.ts CHANGED
@@ -21,18 +21,28 @@ export function deepEqual(a: any, b: any, depth: number = -1): boolean {
21
21
  function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
22
22
  // Identical objects are equal. `0 === -0`, but they aren't identical.
23
23
  // See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
24
- if (a === b) return a !== 0 || 1 / a === 1 / b
24
+ if (a === b) {
25
+ return a !== 0 || 1 / a === 1 / b
26
+ }
25
27
  // `null` or `undefined` only equal to itself (strict comparison).
26
- if (a == null || b == null) return false
28
+ if (a == null || b == null) {
29
+ return false
30
+ }
27
31
  // `NaN`s are equivalent, but non-reflexive.
28
- if (a !== a) return b !== b
32
+ if (a !== a) {
33
+ return b !== b
34
+ }
29
35
  // Exhaust primitive checks
30
36
  const type = typeof a
31
- if (!isFunction(type) && type !== "object" && typeof b != "object") return false
37
+ if (type !== "function" && type !== "object" && typeof b != "object") {
38
+ return false
39
+ }
32
40
 
33
41
  // Compare `[[Class]]` names.
34
42
  const className = toString.call(a)
35
- if (className !== toString.call(b)) return false
43
+ if (className !== toString.call(b)) {
44
+ return false
45
+ }
36
46
  switch (className) {
37
47
  // Strings, numbers, regular expressions, dates, and booleans are compared by value.
38
48
  case "[object RegExp]":
@@ -44,7 +54,9 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
44
54
  case "[object Number]":
45
55
  // `NaN`s are equivalent, but non-reflexive.
46
56
  // Object(NaN) is equivalent to NaN.
47
- if (+a !== +a) return +b !== +b
57
+ if (+a !== +a) {
58
+ return +b !== +b
59
+ }
48
60
  // An `egal` comparison is performed for other numeric values.
49
61
  return +a === 0 ? 1 / +a === 1 / b : +a === +b
50
62
  case "[object Date]":
@@ -72,7 +84,9 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
72
84
 
73
85
  const areArrays = className === "[object Array]"
74
86
  if (!areArrays) {
75
- if (typeof a != "object" || typeof b != "object") return false
87
+ if (typeof a != "object" || typeof b != "object") {
88
+ return false
89
+ }
76
90
 
77
91
  // Objects with different constructors are not equivalent, but `Object`s or `Array`s
78
92
  // from different frames are.
@@ -110,7 +124,9 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
110
124
  while (length--) {
111
125
  // Linear search. Performance is inversely proportional to the number of
112
126
  // unique nested structures.
113
- if (aStack[length] === a) return bStack[length] === b
127
+ if (aStack[length] === a) {
128
+ return bStack[length] === b
129
+ }
114
130
  }
115
131
 
116
132
  // Add the first object to the stack of traversed objects.
@@ -121,10 +137,14 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
121
137
  if (areArrays) {
122
138
  // Compare array lengths to determine if a deep comparison is necessary.
123
139
  length = a.length
124
- if (length !== b.length) return false
140
+ if (length !== b.length) {
141
+ return false
142
+ }
125
143
  // Deep compare the contents, ignoring non-numeric properties.
126
144
  while (length--) {
127
- if (!eq(a[length], b[length], depth - 1, aStack, bStack)) return false
145
+ if (!eq(a[length], b[length], depth - 1, aStack, bStack)) {
146
+ return false
147
+ }
128
148
  }
129
149
  } else {
130
150
  // Deep compare objects.
@@ -132,11 +152,15 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
132
152
  let key
133
153
  length = keys.length
134
154
  // Ensure that both objects contain the same number of properties before comparing deep equality.
135
- if (Object.keys(b).length !== length) return false
155
+ if (Object.keys(b).length !== length) {
156
+ return false
157
+ }
136
158
  while (length--) {
137
159
  // Deep compare each member
138
160
  key = keys[length]
139
- if (!(hasProp(b, key) && eq(a[key], b[key], depth - 1, aStack, bStack))) return false
161
+ if (!(hasProp(b, key) && eq(a[key], b[key], depth - 1, aStack, bStack))) {
162
+ return false
163
+ }
140
164
  }
141
165
  }
142
166
  // Remove the first object from the stack of traversed objects.
@@ -146,8 +170,14 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
146
170
  }
147
171
 
148
172
  function unwrap(a: any) {
149
- if (isObservableArray(a)) return a.slice()
150
- if (isES6Map(a) || isObservableMap(a)) return Array.from(a.entries())
151
- if (isES6Set(a) || isObservableSet(a)) return Array.from(a.entries())
173
+ if (isObservableArray(a)) {
174
+ return a.slice()
175
+ }
176
+ if (isES6Map(a) || isObservableMap(a)) {
177
+ return Array.from(a.entries())
178
+ }
179
+ if (isES6Set(a) || isObservableSet(a)) {
180
+ return Array.from(a.entries())
181
+ }
152
182
  return a
153
183
  }
@@ -49,7 +49,9 @@ export function getNextId() {
49
49
  export function once(func: Lambda): Lambda {
50
50
  let invoked = false
51
51
  return function () {
52
- if (invoked) return
52
+ if (invoked) {
53
+ return
54
+ }
53
55
  invoked = true
54
56
  return (func as any).apply(this, arguments)
55
57
  }
@@ -80,19 +82,32 @@ export function isObject(value: any): value is Object {
80
82
  return value !== null && typeof value === "object"
81
83
  }
82
84
 
83
- export function isPlainObject(value) {
84
- if (!isObject(value)) return false
85
+ export function isPlainObject(value: any) {
86
+ if (!isObject(value)) {
87
+ return false
88
+ }
85
89
  const proto = Object.getPrototypeOf(value)
86
- if (proto == null) return true
87
- return proto.constructor?.toString() === plainObjectString
90
+ if (proto == null) {
91
+ return true
92
+ }
93
+ const protoConstructor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor
94
+ return (
95
+ typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
96
+ )
88
97
  }
89
98
 
90
99
  // https://stackoverflow.com/a/37865170
91
100
  export function isGenerator(obj: any): boolean {
92
101
  const constructor = obj?.constructor
93
- if (!constructor) return false
94
- if ("GeneratorFunction" === constructor.name || "GeneratorFunction" === constructor.displayName)
102
+ if (!constructor) {
103
+ return false
104
+ }
105
+ if (
106
+ "GeneratorFunction" === constructor.name ||
107
+ "GeneratorFunction" === constructor.displayName
108
+ ) {
95
109
  return true
110
+ }
96
111
  return false
97
112
  }
98
113
 
@@ -125,11 +140,11 @@ export function createInstanceofPredicate<T>(
125
140
  } as any
126
141
  }
127
142
 
128
- export function isES6Map(thing): boolean {
143
+ export function isES6Map(thing: any): thing is Map<any, any> {
129
144
  return thing instanceof Map
130
145
  }
131
146
 
132
- export function isES6Set(thing): thing is Set<any> {
147
+ export function isES6Set(thing: any): thing is Set<any> {
133
148
  return thing instanceof Set
134
149
  }
135
150
 
@@ -138,18 +153,22 @@ const hasGetOwnPropertySymbols = typeof Object.getOwnPropertySymbols !== "undefi
138
153
  /**
139
154
  * Returns the following: own enumerable keys and symbols.
140
155
  */
141
- export function getPlainObjectKeys(object) {
156
+ export function getPlainObjectKeys(object: any) {
142
157
  const keys = Object.keys(object)
143
158
  // Not supported in IE, so there are not going to be symbol props anyway...
144
- if (!hasGetOwnPropertySymbols) return keys
159
+ if (!hasGetOwnPropertySymbols) {
160
+ return keys
161
+ }
145
162
  const symbols = Object.getOwnPropertySymbols(object)
146
- if (!symbols.length) return keys
163
+ if (!symbols.length) {
164
+ return keys
165
+ }
147
166
  return [...keys, ...symbols.filter(s => objectPrototype.propertyIsEnumerable.call(object, s))]
148
167
  }
149
168
 
150
169
  // From Immer utils
151
170
  // Returns all own keys, including non-enumerable and symbolic
152
- export const ownKeys: (target: any) => PropertyKey[] =
171
+ export const ownKeys: (target: any) => Array<string | symbol> =
153
172
  typeof Reflect !== "undefined" && Reflect.ownKeys
154
173
  ? Reflect.ownKeys
155
174
  : hasGetOwnPropertySymbols
@@ -157,12 +176,16 @@ export const ownKeys: (target: any) => PropertyKey[] =
157
176
  : /* istanbul ignore next */ Object.getOwnPropertyNames
158
177
 
159
178
  export function stringifyKey(key: any): string {
160
- if (typeof key === "string") return key
161
- if (typeof key === "symbol") return key.toString()
179
+ if (typeof key === "string") {
180
+ return key
181
+ }
182
+ if (typeof key === "symbol") {
183
+ return key.toString()
184
+ }
162
185
  return new String(key).toString()
163
186
  }
164
187
 
165
- export function toPrimitive(value) {
188
+ export function toPrimitive(value: any) {
166
189
  return value === null ? null : typeof value === "object" ? "" + value : value
167
190
  }
168
191