mobx 6.16.1 → 7.0.1

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 (100) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +9 -13
  3. package/dist/api/action.d.ts +6 -4
  4. package/dist/api/annotation.d.ts +0 -1
  5. package/dist/api/computed.d.ts +4 -7
  6. package/dist/api/configure.d.ts +0 -1
  7. package/dist/api/decoratorannotation.d.ts +3 -0
  8. package/dist/api/decorators.d.ts +0 -19
  9. package/dist/api/flow.d.ts +4 -5
  10. package/dist/api/makeObservable.d.ts +2 -3
  11. package/dist/api/observable.d.ts +9 -15
  12. package/dist/core/atom.d.ts +1 -4
  13. package/dist/core/computedvalue.d.ts +5 -33
  14. package/dist/core/derivation.d.ts +1 -7
  15. package/dist/core/globalstate.d.ts +7 -2
  16. package/dist/core/observable.d.ts +9 -1
  17. package/dist/core/reaction.d.ts +1 -9
  18. package/dist/errors.d.ts +6 -4
  19. package/dist/internal.d.ts +1 -4
  20. package/dist/mobx.cjs.development.js +1704 -2484
  21. package/dist/mobx.cjs.development.js.map +1 -1
  22. package/dist/mobx.cjs.production.min.js +1 -1
  23. package/dist/mobx.cjs.production.min.js.map +1 -1
  24. package/dist/mobx.d.ts +2 -2
  25. package/dist/mobx.esm.development.js +1693 -2483
  26. package/dist/mobx.esm.development.js.map +1 -1
  27. package/dist/mobx.esm.js +1643 -2436
  28. package/dist/mobx.esm.js.map +1 -1
  29. package/dist/mobx.esm.production.min.js +1 -1
  30. package/dist/mobx.esm.production.min.js.map +1 -1
  31. package/dist/mobx.mjs +5308 -0
  32. package/dist/mobx.mjs.map +1 -0
  33. package/dist/mobx.umd.development.js +1707 -2487
  34. package/dist/mobx.umd.development.js.map +1 -1
  35. package/dist/mobx.umd.production.min.js +1 -1
  36. package/dist/mobx.umd.production.min.js.map +1 -1
  37. package/dist/types/actionannotation.d.ts +1 -0
  38. package/dist/types/computedannotation.d.ts +1 -0
  39. package/dist/types/decorator_fills.d.ts +2 -0
  40. package/dist/types/flowannotation.d.ts +1 -0
  41. package/dist/types/observableannotation.d.ts +5 -0
  42. package/dist/types/observablearray.d.ts +2 -5
  43. package/dist/types/observablemap.d.ts +1 -8
  44. package/dist/types/observableobject.d.ts +2 -13
  45. package/dist/types/observableset.d.ts +1 -3
  46. package/dist/types/observablevalue.d.ts +4 -6
  47. package/dist/types/overrideannotation.d.ts +1 -2
  48. package/dist/utils/comparer.d.ts +4 -11
  49. package/dist/utils/utils.d.ts +0 -2
  50. package/package.json +25 -15
  51. package/src/api/action.ts +27 -35
  52. package/src/api/annotation.ts +0 -1
  53. package/src/api/autorun.ts +2 -4
  54. package/src/api/computed.ts +21 -28
  55. package/src/api/configure.ts +1 -15
  56. package/src/api/decoratorannotation.ts +20 -0
  57. package/src/api/decorators.ts +1 -85
  58. package/src/api/extras.ts +1 -1
  59. package/src/api/flow.ts +26 -29
  60. package/src/api/intercept.ts +4 -3
  61. package/src/api/makeObservable.ts +45 -17
  62. package/src/api/object-api.ts +1 -1
  63. package/src/api/observable.ts +34 -67
  64. package/src/api/observe.ts +82 -3
  65. package/src/api/when.ts +4 -3
  66. package/src/core/action.ts +12 -5
  67. package/src/core/atom.ts +16 -14
  68. package/src/core/computedvalue.ts +36 -69
  69. package/src/core/derivation.ts +16 -15
  70. package/src/core/globalstate.ts +16 -13
  71. package/src/core/observable.ts +65 -73
  72. package/src/core/reaction.ts +19 -25
  73. package/src/core/spy.ts +3 -3
  74. package/src/errors.ts +23 -12
  75. package/src/internal.ts +1 -4
  76. package/src/mobx.ts +21 -10
  77. package/src/types/actionannotation.ts +5 -9
  78. package/src/types/autoannotation.ts +8 -11
  79. package/src/types/computedannotation.ts +13 -11
  80. package/src/types/decorator_fills.ts +4 -0
  81. package/src/types/dynamicobject.ts +0 -29
  82. package/src/types/flowannotation.ts +7 -4
  83. package/src/types/modifiers.ts +5 -0
  84. package/src/types/observableannotation.ts +6 -7
  85. package/src/types/observablearray.ts +8 -61
  86. package/src/types/observablemap.ts +4 -29
  87. package/src/types/observableobject.ts +5 -70
  88. package/src/types/observableset.ts +9 -52
  89. package/src/types/observablevalue.ts +7 -29
  90. package/src/types/overrideannotation.ts +7 -22
  91. package/src/types/type-utils.ts +4 -2
  92. package/src/utils/comparer.ts +4 -17
  93. package/src/utils/iterable.ts +3 -4
  94. package/src/utils/utils.ts +4 -46
  95. package/dist/api/trace.d.ts +0 -3
  96. package/dist/types/legacyobservablearray.d.ts +0 -14
  97. package/dist/utils/global.d.ts +0 -1
  98. package/src/api/trace.ts +0 -35
  99. package/src/types/legacyobservablearray.ts +0 -155
  100. package/src/utils/global.ts +0 -20
@@ -7,8 +7,6 @@ import {
7
7
  isSpyEnabled,
8
8
  hasListeners,
9
9
  IListenable,
10
- registerListener,
11
- Lambda,
12
10
  spyReportStart,
13
11
  notifyListeners,
14
12
  spyReportEnd,
@@ -17,8 +15,6 @@ import {
17
15
  hasInterceptors,
18
16
  interceptChange,
19
17
  IInterceptable,
20
- IInterceptor,
21
- registerInterceptor,
22
18
  checkIfStateModificationsAreAllowed,
23
19
  untracked,
24
20
  transaction,
@@ -27,7 +23,6 @@ import {
27
23
  DELETE,
28
24
  ADD,
29
25
  die,
30
- isFunction,
31
26
  initObservable
32
27
  } from "../internal"
33
28
 
@@ -55,16 +50,14 @@ export type ISetWillDeleteChange<T = any> = {
55
50
  type: "delete"
56
51
  object: ObservableSet<T>
57
52
  oldValue: T
58
- };
53
+ }
59
54
  export type ISetWillAddChange<T = any> = {
60
55
  type: "add"
61
56
  object: ObservableSet<T>
62
57
  newValue: T
63
- };
58
+ }
64
59
 
65
- export type ISetWillChange<T = any> =
66
- | ISetWillDeleteChange<T>
67
- | ISetWillAddChange<T>
60
+ export type ISetWillChange<T = any> = ISetWillDeleteChange<T> | ISetWillAddChange<T>
68
61
 
69
62
  export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillChange>, IListenable {
70
63
  [$mobx] = ObservableSetMarker
@@ -80,9 +73,6 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
80
73
  enhancer: IEnhancer<T> = deepEnhancer,
81
74
  public name_ = __DEV__ ? "ObservableSet@" + getNextId() : "ObservableSet"
82
75
  ) {
83
- if (!isFunction(Set)) {
84
- die(22)
85
- }
86
76
  this.enhancer_ = (newV, oldV) => enhancer(newV, oldV, name_)
87
77
  initObservable(() => {
88
78
  this.atom_ = createAtom(this.name_)
@@ -133,8 +123,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
133
123
  }
134
124
 
135
125
  // implemented reassignment same as it's done for ObservableMap
136
- value = change.newValue!;
137
-
126
+ value = change.newValue!
138
127
  }
139
128
  if (!this.has(value)) {
140
129
  transaction(() => {
@@ -244,21 +233,11 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
244
233
  }
245
234
 
246
235
  intersection<U>(otherSet: ReadonlySetLike<U> | Set<U>): Set<T & U> {
247
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
248
- return otherSet.intersection(this)
249
- } else {
250
- const dehancedSet = new Set(this)
251
- return dehancedSet.intersection(otherSet)
252
- }
236
+ return new Set(this).intersection(otherSet)
253
237
  }
254
238
 
255
239
  union<U>(otherSet: ReadonlySetLike<U> | Set<U>): Set<T | U> {
256
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
257
- return otherSet.union(this)
258
- } else {
259
- const dehancedSet = new Set(this)
260
- return dehancedSet.union(otherSet)
261
- }
240
+ return new Set(this).union(otherSet)
262
241
  }
263
242
 
264
243
  difference<U>(otherSet: ReadonlySetLike<U>): Set<T> {
@@ -266,12 +245,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
266
245
  }
267
246
 
268
247
  symmetricDifference<U>(otherSet: ReadonlySetLike<U> | Set<U>): Set<T | U> {
269
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
270
- return otherSet.symmetricDifference(this)
271
- } else {
272
- const dehancedSet = new Set(this)
273
- return dehancedSet.symmetricDifference(otherSet)
274
- }
248
+ return new Set(this).symmetricDifference(otherSet)
275
249
  }
276
250
 
277
251
  isSubsetOf(otherSet: ReadonlySetLike<unknown>): boolean {
@@ -283,12 +257,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
283
257
  }
284
258
 
285
259
  isDisjointFrom(otherSet: ReadonlySetLike<unknown> | Set<unknown>): boolean {
286
- if (isES6Set(otherSet) && !isObservableSet(otherSet)) {
287
- return otherSet.isDisjointFrom(this)
288
- } else {
289
- const dehancedSet = new Set(this)
290
- return dehancedSet.isDisjointFrom(otherSet)
291
- }
260
+ return new Set(this).isDisjointFrom(otherSet)
292
261
  }
293
262
 
294
263
  replace(other: ObservableSet<T> | IObservableSetInitialValues<T>): ObservableSet<T> {
@@ -304,24 +273,12 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
304
273
  this.clear()
305
274
  other.forEach(value => this.add(value))
306
275
  } else if (other !== null && other !== undefined) {
307
- die("Cannot initialize set from " + other)
276
+ die(41, other)
308
277
  }
309
278
  })
310
279
 
311
280
  return this
312
281
  }
313
- observe_(listener: (changes: ISetDidChange<T>) => void, fireImmediately?: boolean): Lambda {
314
- // ... 'fireImmediately' could also be true?
315
- if (__DEV__ && fireImmediately === true) {
316
- die("`observe` doesn't support fireImmediately=true in combination with sets.")
317
- }
318
- return registerListener(this, listener)
319
- }
320
-
321
- intercept_(handler: IInterceptor<ISetWillChange<T>>): Lambda {
322
- return registerInterceptor(this, handler)
323
- }
324
-
325
282
  toJSON(): T[] {
326
283
  return Array.from(this)
327
284
  }
@@ -3,11 +3,9 @@ import {
3
3
  IEnhancer,
4
4
  IInterceptable,
5
5
  IEqualsComparer,
6
- IInterceptor,
7
6
  IListenable,
8
- Lambda,
9
7
  checkIfStateModificationsAreAllowed,
10
- comparer,
8
+ compareDefault,
11
9
  createInstanceofPredicate,
12
10
  getNextId,
13
11
  hasInterceptors,
@@ -15,8 +13,6 @@ import {
15
13
  interceptChange,
16
14
  isSpyEnabled,
17
15
  notifyListeners,
18
- registerInterceptor,
19
- registerListener,
20
16
  spyReport,
21
17
  spyReportEnd,
22
18
  spyReportStart,
@@ -69,13 +65,13 @@ export class ObservableValue<T>
69
65
 
70
66
  constructor(
71
67
  value: T,
72
- public enhancer: IEnhancer<T>,
68
+ public enhancer_: IEnhancer<T>,
73
69
  public name_ = __DEV__ ? "ObservableValue@" + getNextId() : "ObservableValue",
74
70
  notifySpy = true,
75
- private equals: IEqualsComparer<any> = comparer.default
71
+ private equals_: IEqualsComparer<any> = compareDefault
76
72
  ) {
77
73
  super(name_)
78
- this.value_ = enhancer(value, undefined, name_)
74
+ this.value_ = enhancer_(value, undefined, name_)
79
75
  if (__DEV__ && notifySpy && isSpyEnabled()) {
80
76
  // only notify spy if this is a stand-alone observable
81
77
  spyReport({
@@ -99,7 +95,7 @@ export class ObservableValue<T>
99
95
  const oldValue = this.value_
100
96
  newValue = this.prepareNewValue_(newValue) as any
101
97
  if (newValue !== globalState.UNCHANGED) {
102
- const notifySpy = isSpyEnabled()
98
+ const notifySpy = __DEV__ && isSpyEnabled()
103
99
  if (__DEV__ && notifySpy) {
104
100
  spyReportStart({
105
101
  type: UPDATE,
@@ -131,8 +127,8 @@ export class ObservableValue<T>
131
127
  newValue = change.newValue
132
128
  }
133
129
  // apply modifier
134
- newValue = this.enhancer(newValue, this.value_, this.name_)
135
- return this.equals(this.value_, newValue) ? globalState.UNCHANGED : newValue
130
+ newValue = this.enhancer_(newValue, this.value_, this.name_)
131
+ return this.equals_(this.value_, newValue) ? globalState.UNCHANGED : newValue
136
132
  }
137
133
 
138
134
  setNewValue_(newValue: T) {
@@ -154,24 +150,6 @@ export class ObservableValue<T>
154
150
  return this.dehanceValue(this.value_)
155
151
  }
156
152
 
157
- intercept_(handler: IInterceptor<IValueWillChange<T>>): Lambda {
158
- return registerInterceptor(this, handler)
159
- }
160
-
161
- observe_(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda {
162
- if (fireImmediately) {
163
- listener({
164
- observableKind: "value",
165
- debugObjectName: this.name_,
166
- object: this,
167
- type: UPDATE,
168
- newValue: this.value_,
169
- oldValue: undefined
170
- })
171
- }
172
- return registerListener(this, listener)
173
- }
174
-
175
153
  raw() {
176
154
  // used by MST ot get undehanced value
177
155
  return this.value_
@@ -1,23 +1,12 @@
1
- import {
2
- die,
3
- Annotation,
4
- hasProp,
5
- createDecoratorAnnotation,
6
- ObservableObjectAdministration,
7
- MakeResult
8
- } from "../internal"
9
-
10
- import type { ClassMethodDecorator } from "./decorator_fills"
1
+ import { die, Annotation, hasProp, ObservableObjectAdministration, MakeResult } from "../internal"
11
2
 
12
3
  const OVERRIDE = "override"
13
4
 
14
- export const override: Annotation & PropertyDecorator & ClassMethodDecorator =
15
- createDecoratorAnnotation({
16
- annotationType_: OVERRIDE,
17
- make_,
18
- extend_,
19
- decorate_20223_
20
- })
5
+ export const override: Annotation = {
6
+ annotationType_: OVERRIDE,
7
+ make_,
8
+ extend_
9
+ }
21
10
 
22
11
  export function isOverride(annotation: Annotation): boolean {
23
12
  return annotation.annotationType_ === OVERRIDE
@@ -42,9 +31,5 @@ function make_(this: Annotation, adm: ObservableObjectAdministration, key): Make
42
31
  }
43
32
 
44
33
  function extend_(this: Annotation, adm, key, descriptor, proxyTrap): boolean {
45
- die(`'${this.annotationType_}' can only be used with 'makeObservable'`)
46
- }
47
-
48
- function decorate_20223_(this: Annotation, desc, context: DecoratorContext) {
49
- console.warn(`'${this.annotationType_}' cannot be used with decorators - this is a no-op`)
34
+ die(44, this.annotationType_)
50
35
  }
@@ -111,13 +111,15 @@ export function getDebugName(thing: any, property?: string): string {
111
111
  */
112
112
  export function initObservable<T>(cb: () => T): T {
113
113
  const derivation = untrackedStart()
114
- const allowStateChanges = allowStateChangesStart(true)
114
+ const allowStateChanges = __DEV__ ? allowStateChangesStart(true) : true
115
115
  startBatch()
116
116
  try {
117
117
  return cb()
118
118
  } finally {
119
119
  endBatch()
120
- allowStateChangesEnd(allowStateChanges)
120
+ if (__DEV__) {
121
+ allowStateChangesEnd(allowStateChanges)
122
+ }
121
123
  untrackedEnd(derivation)
122
124
  }
123
125
  }
@@ -4,29 +4,16 @@ export interface IEqualsComparer<T> {
4
4
  (a: T, b: T): boolean
5
5
  }
6
6
 
7
- function identityComparer(a: any, b: any): boolean {
7
+ export function compareIdentity(a: any, b: any): boolean {
8
8
  return a === b
9
9
  }
10
10
 
11
- function structuralComparer(a: any, b: any): boolean {
11
+ export function compareStructural(a: any, b: any): boolean {
12
12
  return deepEqual(a, b)
13
13
  }
14
14
 
15
- function shallowComparer(a: any, b: any): boolean {
15
+ export function compareShallow(a: any, b: any): boolean {
16
16
  return deepEqual(a, b, 1)
17
17
  }
18
18
 
19
- function defaultComparer(a: any, b: any): boolean {
20
- if (Object.is) {
21
- return Object.is(a, b)
22
- }
23
-
24
- return a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b
25
- }
26
-
27
- export const comparer = {
28
- identity: identityComparer,
29
- structural: structuralComparer,
30
- default: defaultComparer,
31
- shallow: shallowComparer
32
- }
19
+ export const compareDefault = Object.is
@@ -1,14 +1,13 @@
1
- import { getGlobal } from "../internal"
1
+ import { assign } from "./utils"
2
2
 
3
3
  // safely get iterator prototype if available
4
- const global = getGlobal()
5
- const maybeIteratorPrototype = global.Iterator?.prototype || {}
4
+ const maybeIteratorPrototype = (globalThis as any).Iterator?.prototype || {}
6
5
 
7
6
  export function makeIterable<T, TReturn = unknown>(
8
7
  iterator: Iterator<T>
9
8
  ): IteratorObject<T, TReturn> {
10
9
  iterator[Symbol.iterator] = getSelf
11
- return Object.assign(Object.create(maybeIteratorPrototype), iterator)
10
+ return assign(Object.create(maybeIteratorPrototype), iterator)
12
11
  }
13
12
 
14
13
  function getSelf() {
@@ -1,4 +1,4 @@
1
- import { globalState, die } from "../internal"
1
+ import { globalState } from "../internal"
2
2
 
3
3
  // We shorten anything used > 5 times
4
4
  export const assign = Object.assign
@@ -17,28 +17,8 @@ export interface Lambda {
17
17
  name?: string
18
18
  }
19
19
 
20
- const hasProxy = typeof Proxy !== "undefined"
21
20
  const plainObjectString = Object.toString()
22
21
 
23
- export function assertProxies() {
24
- if (!hasProxy) {
25
- die(
26
- __DEV__
27
- ? "`Proxy` objects are not available in the current environment. Please configure MobX to enable a fallback implementation.`"
28
- : "Proxy not available"
29
- )
30
- }
31
- }
32
-
33
- export function warnAboutProxyRequirement(msg: string) {
34
- if (__DEV__ && globalState.verifyProxies) {
35
- die(
36
- "MobX is currently configured to be able to run in ES5 mode, but in ES5 MobX won't be able to " +
37
- msg
38
- )
39
- }
40
- }
41
-
42
22
  export function getNextId() {
43
23
  return ++globalState.mobxGuid
44
24
  }
@@ -90,7 +70,7 @@ export function isPlainObject(value: any) {
90
70
  if (proto == null) {
91
71
  return true
92
72
  }
93
- const protoConstructor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor
73
+ const protoConstructor = hasProp(proto, "constructor") && proto.constructor
94
74
  return (
95
75
  typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
96
76
  )
@@ -164,17 +144,11 @@ export function isES6Set(thing: unknown): thing is Set<any> {
164
144
  return thing != null && Object.prototype.toString.call(thing) === "[object Set]"
165
145
  }
166
146
 
167
- const hasGetOwnPropertySymbols = typeof Object.getOwnPropertySymbols !== "undefined"
168
-
169
147
  /**
170
148
  * Returns the following: own enumerable keys and symbols.
171
149
  */
172
150
  export function getPlainObjectKeys(object: any) {
173
151
  const keys = Object.keys(object)
174
- // Not supported in IE, so there are not going to be symbol props anyway...
175
- if (!hasGetOwnPropertySymbols) {
176
- return keys
177
- }
178
152
  const symbols = Object.getOwnPropertySymbols(object)
179
153
  if (!symbols.length) {
180
154
  return keys
@@ -184,12 +158,7 @@ export function getPlainObjectKeys(object: any) {
184
158
 
185
159
  // From Immer utils
186
160
  // Returns all own keys, including non-enumerable and symbolic
187
- export const ownKeys: (target: any) => Array<string | symbol> =
188
- typeof Reflect !== "undefined" && Reflect.ownKeys
189
- ? Reflect.ownKeys
190
- : hasGetOwnPropertySymbols
191
- ? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
192
- : /* istanbul ignore next */ Object.getOwnPropertyNames
161
+ export const ownKeys: (target: any) => Array<string | symbol> = Reflect.ownKeys
193
162
 
194
163
  export function stringifyKey(key: any): string {
195
164
  if (typeof key === "string") {
@@ -209,18 +178,7 @@ export function hasProp(target: Object, prop: PropertyKey): boolean {
209
178
  return objectPrototype.hasOwnProperty.call(target, prop)
210
179
  }
211
180
 
212
- // From Immer utils
213
- export const getOwnPropertyDescriptors =
214
- Object.getOwnPropertyDescriptors ||
215
- function getOwnPropertyDescriptors(target: any) {
216
- // Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/274
217
- const res: any = {}
218
- // Note: without polyfill for ownKeys, symbols won't be picked up
219
- ownKeys(target).forEach(key => {
220
- res[key] = getDescriptor(target, key)
221
- })
222
- return res
223
- }
181
+ export const getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors
224
182
 
225
183
  export function getFlag(flags: number, mask: number) {
226
184
  return !!(flags & mask)
@@ -1,3 +0,0 @@
1
- export declare function trace(thing?: any, prop?: string, enterBreakPoint?: boolean): void;
2
- export declare function trace(thing?: any, enterBreakPoint?: boolean): void;
3
- export declare function trace(enterBreakPoint?: boolean): void;
@@ -1,14 +0,0 @@
1
- import { IEnhancer, IObservableArray } from "../internal";
2
- declare class StubArray {
3
- }
4
- export declare class LegacyObservableArray<T> extends StubArray {
5
- constructor(initialValues: T[] | undefined, enhancer: IEnhancer<T>, name?: string, owned?: boolean);
6
- concat(...arrays: T[][]): T[];
7
- get length(): number;
8
- set length(newLength: number);
9
- get [Symbol.toStringTag](): string;
10
- [Symbol.iterator](): IteratorObject<any, unknown, unknown>;
11
- }
12
- export declare function reserveArrayBuffer(max: number): void;
13
- export declare function createLegacyArray<T>(initialValues: T[] | undefined, enhancer: IEnhancer<T>, name?: string): IObservableArray<T>;
14
- export {};
@@ -1 +0,0 @@
1
- export declare function getGlobal(): any;
package/src/api/trace.ts DELETED
@@ -1,35 +0,0 @@
1
- import { TraceMode, die, getAtom, globalState } from "../internal"
2
-
3
- export function trace(thing?: any, prop?: string, enterBreakPoint?: boolean): void
4
- export function trace(thing?: any, enterBreakPoint?: boolean): void
5
- export function trace(enterBreakPoint?: boolean): void
6
- export function trace(...args: any[]): void {
7
- if (!__DEV__) {
8
- return
9
- }
10
- let enterBreakPoint = false
11
- if (typeof args[args.length - 1] === "boolean") {
12
- enterBreakPoint = args.pop()
13
- }
14
- const derivation = getAtomFromArgs(args)
15
- if (!derivation) {
16
- return die(
17
- `'trace(break?)' can only be used inside a tracked computed value or a Reaction. Consider passing in the computed value or reaction explicitly`
18
- )
19
- }
20
- if (derivation.isTracing_ === TraceMode.NONE) {
21
- console.log(`[mobx.trace] '${derivation.name_}' tracing enabled`)
22
- }
23
- derivation.isTracing_ = enterBreakPoint ? TraceMode.BREAK : TraceMode.LOG
24
- }
25
-
26
- function getAtomFromArgs(args): any {
27
- switch (args.length) {
28
- case 0:
29
- return globalState.trackingDerivation
30
- case 1:
31
- return getAtom(args[0])
32
- case 2:
33
- return getAtom(args[0], args[1])
34
- }
35
- }
@@ -1,155 +0,0 @@
1
- import {
2
- getNextId,
3
- addHiddenFinalProp,
4
- makeIterable,
5
- addHiddenProp,
6
- ObservableArrayAdministration,
7
- $mobx,
8
- arrayExtensions,
9
- IEnhancer,
10
- isObservableArray,
11
- IObservableArray,
12
- defineProperty,
13
- initObservable
14
- } from "../internal"
15
-
16
- // Bug in safari 9.* (or iOS 9 safari mobile). See #364
17
- const ENTRY_0 = createArrayEntryDescriptor(0)
18
-
19
- const safariPrototypeSetterInheritanceBug = (() => {
20
- let v = false
21
- const p = {}
22
- Object.defineProperty(p, "0", {
23
- set: () => {
24
- v = true
25
- }
26
- })
27
- Object.create(p)["0"] = 1
28
- return v === false
29
- })()
30
-
31
- /**
32
- * This array buffer contains two lists of properties, so that all arrays
33
- * can recycle their property definitions, which significantly improves performance of creating
34
- * properties on the fly.
35
- */
36
- let OBSERVABLE_ARRAY_BUFFER_SIZE = 0
37
-
38
- // Typescript workaround to make sure ObservableArray extends Array
39
- class StubArray {}
40
- function inherit(ctor, proto) {
41
- if (Object.setPrototypeOf) {
42
- Object.setPrototypeOf(ctor.prototype, proto)
43
- } else if (ctor.prototype.__proto__ !== undefined) {
44
- ctor.prototype.__proto__ = proto
45
- } else {
46
- ctor.prototype = proto
47
- }
48
- }
49
- inherit(StubArray, Array.prototype)
50
-
51
- // Weex proto freeze protection was here,
52
- // but it is unclear why the hack is need as MobX never changed the prototype
53
- // anyway, so removed it in V6
54
-
55
- export class LegacyObservableArray<T> extends StubArray {
56
- constructor(
57
- initialValues: T[] | undefined,
58
- enhancer: IEnhancer<T>,
59
- name = __DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray",
60
- owned = false
61
- ) {
62
- super()
63
- initObservable(() => {
64
- const adm = new ObservableArrayAdministration(name, enhancer, owned, true)
65
- adm.proxy_ = this as any
66
- addHiddenFinalProp(this, $mobx, adm)
67
-
68
- if (initialValues && initialValues.length) {
69
- // @ts-ignore
70
- this.spliceWithArray(0, 0, initialValues)
71
- }
72
-
73
- if (safariPrototypeSetterInheritanceBug) {
74
- // Seems that Safari won't use numeric prototype setter until any * numeric property is
75
- // defined on the instance. After that it works fine, even if this property is deleted.
76
- Object.defineProperty(this, "0", ENTRY_0)
77
- }
78
- })
79
- }
80
-
81
- concat(...arrays: T[][]): T[] {
82
- ;(this[$mobx] as ObservableArrayAdministration).atom_.reportObserved()
83
- return Array.prototype.concat.apply(
84
- (this as any).slice(),
85
- //@ts-ignore
86
- arrays.map(a => (isObservableArray(a) ? a.slice() : a))
87
- )
88
- }
89
-
90
- get length(): number {
91
- return (this[$mobx] as ObservableArrayAdministration).getArrayLength_()
92
- }
93
-
94
- set length(newLength: number) {
95
- ;(this[$mobx] as ObservableArrayAdministration).setArrayLength_(newLength)
96
- }
97
-
98
- get [Symbol.toStringTag]() {
99
- return "Array"
100
- }
101
-
102
- [Symbol.iterator]() {
103
- const self = this
104
- let nextIndex = 0
105
- return makeIterable({
106
- next() {
107
- return nextIndex < self.length
108
- ? { value: self[nextIndex++], done: false }
109
- : { done: true, value: undefined }
110
- }
111
- })
112
- }
113
- }
114
-
115
- Object.entries(arrayExtensions).forEach(([prop, fn]) => {
116
- if (prop !== "concat") {
117
- addHiddenProp(LegacyObservableArray.prototype, prop, fn)
118
- }
119
- })
120
-
121
- function createArrayEntryDescriptor(index: number) {
122
- return {
123
- enumerable: false,
124
- configurable: true,
125
- get: function () {
126
- return this[$mobx].get_(index)
127
- },
128
- set: function (value) {
129
- this[$mobx].set_(index, value)
130
- }
131
- }
132
- }
133
-
134
- function createArrayBufferItem(index: number) {
135
- defineProperty(LegacyObservableArray.prototype, "" + index, createArrayEntryDescriptor(index))
136
- }
137
-
138
- export function reserveArrayBuffer(max: number) {
139
- if (max > OBSERVABLE_ARRAY_BUFFER_SIZE) {
140
- for (let index = OBSERVABLE_ARRAY_BUFFER_SIZE; index < max + 100; index++) {
141
- createArrayBufferItem(index)
142
- }
143
- OBSERVABLE_ARRAY_BUFFER_SIZE = max
144
- }
145
- }
146
-
147
- reserveArrayBuffer(1000)
148
-
149
- export function createLegacyArray<T>(
150
- initialValues: T[] | undefined,
151
- enhancer: IEnhancer<T>,
152
- name?: string
153
- ): IObservableArray<T> {
154
- return new LegacyObservableArray(initialValues, enhancer, name) as any
155
- }
@@ -1,20 +0,0 @@
1
- declare const window: any
2
- declare const self: any
3
-
4
- const mockGlobal = {}
5
-
6
- export function getGlobal() {
7
- if (typeof globalThis !== "undefined") {
8
- return globalThis
9
- }
10
- if (typeof window !== "undefined") {
11
- return window
12
- }
13
- if (typeof global !== "undefined") {
14
- return global
15
- }
16
- if (typeof self !== "undefined") {
17
- return self
18
- }
19
- return mockGlobal
20
- }