mobx 6.3.6 → 6.3.10

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.
@@ -27,7 +27,7 @@ export declare type IObjectWillChange<T = any> = {
27
27
  };
28
28
  export declare class ObservableObjectAdministration implements IInterceptable<IObjectWillChange>, IListenable {
29
29
  target_: any;
30
- values_: Map<string | number | symbol, ObservableValue<any> | ComputedValue<any>>;
30
+ values_: Map<PropertyKey, ObservableValue<any> | ComputedValue<any>>;
31
31
  name_: string;
32
32
  defaultAnnotation_: Annotation;
33
33
  keysAtom_: IAtom;
@@ -37,7 +37,7 @@ export declare class ObservableObjectAdministration implements IInterceptable<IO
37
37
  isPlainObject_: boolean;
38
38
  appliedAnnotations_?: object;
39
39
  private pendingKeys_;
40
- constructor(target_: any, values_: Map<string | number | symbol, ObservableValue<any> | ComputedValue<any>>, name_: string, defaultAnnotation_?: Annotation);
40
+ constructor(target_: any, values_: Map<PropertyKey, ObservableValue<any> | ComputedValue<any>>, name_: string, defaultAnnotation_?: Annotation);
41
41
  getObservablePropValue_(key: PropertyKey): any;
42
42
  setObservablePropValue_(key: PropertyKey, newValue: any): boolean | null;
43
43
  get_(key: PropertyKey): any;
@@ -87,7 +87,7 @@ export declare class ObservableObjectAdministration implements IInterceptable<IO
87
87
  observe_(callback: (changes: IObjectDidChange) => void, fireImmediately?: boolean): Lambda;
88
88
  intercept_(handler: any): Lambda;
89
89
  notifyPropertyAddition_(key: PropertyKey, value: any): void;
90
- ownKeys_(): PropertyKey[];
90
+ ownKeys_(): ArrayLike<string | symbol>;
91
91
  keys_(): PropertyKey[];
92
92
  }
93
93
  export interface IIsObservableObject {
@@ -4,8 +4,8 @@ export declare const assign: {
4
4
  <T_2, U_2, V_1, W>(target: T_2, source1: U_2, source2: V_1, source3: W): T_2 & U_2 & V_1 & W;
5
5
  (target: object, ...sources: any[]): any;
6
6
  };
7
- export declare const getDescriptor: (o: any, p: string | number | symbol) => PropertyDescriptor | undefined;
8
- export declare const defineProperty: (o: any, p: string | number | symbol, attributes: PropertyDescriptor & ThisType<any>) => any;
7
+ export declare const getDescriptor: (o: any, p: PropertyKey) => PropertyDescriptor | undefined;
8
+ export declare const defineProperty: <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T;
9
9
  export declare const objectPrototype: Object;
10
10
  export declare const EMPTY_ARRAY: never[];
11
11
  export declare const EMPTY_OBJECT: {};
@@ -36,7 +36,7 @@ export declare function isES6Set(thing: any): thing is Set<any>;
36
36
  * Returns the following: own enumerable keys and symbols.
37
37
  */
38
38
  export declare function getPlainObjectKeys(object: any): (string | symbol)[];
39
- export declare const ownKeys: (target: any) => PropertyKey[];
39
+ export declare const ownKeys: (target: any) => Array<string | symbol>;
40
40
  export declare function stringifyKey(key: any): string;
41
41
  export declare function toPrimitive(value: any): any;
42
42
  export declare function hasProp(target: Object, prop: PropertyKey): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobx",
3
- "version": "6.3.6",
3
+ "version": "6.3.10",
4
4
  "description": "Simple, scalable state management.",
5
5
  "source": "src/mobx.ts",
6
6
  "main": "dist/index.js",
@@ -24,15 +24,15 @@ export function intercept<T>(
24
24
  handler: IInterceptor<IArrayWillChange<T> | IArrayWillSplice<T>>
25
25
  ): Lambda
26
26
  export function intercept<K, V>(
27
- observableMap: ObservableMap<K, V>,
27
+ observableMap: ObservableMap<K, V> | Map<K, V>,
28
28
  handler: IInterceptor<IMapWillChange<K, V>>
29
29
  ): Lambda
30
30
  export function intercept<V>(
31
- observableMap: ObservableSet<V>,
31
+ observableSet: ObservableSet<V> | Set<V>,
32
32
  handler: IInterceptor<ISetWillChange<V>>
33
33
  ): Lambda
34
34
  export function intercept<K, V>(
35
- observableMap: ObservableMap<K, V>,
35
+ observableMap: ObservableMap<K, V> | Map<K, V>,
36
36
  property: K,
37
37
  handler: IInterceptor<IValueWillChange<V>>
38
38
  ): Lambda
@@ -40,7 +40,7 @@ export function intercept(object: object, handler: IInterceptor<IObjectWillChang
40
40
  export function intercept<T extends object, K extends keyof T>(
41
41
  object: T,
42
42
  property: K,
43
- handler: IInterceptor<IValueWillChange<any>>
43
+ handler: IInterceptor<IValueWillChange<T[K]>>
44
44
  ): Lambda
45
45
  export function intercept(thing, propOrHandler?, handler?): Lambda {
46
46
  if (isFunction(handler)) return interceptProperty(thing, propOrHandler, handler)
@@ -25,17 +25,18 @@ export function observe<T>(
25
25
  fireImmediately?: boolean
26
26
  ): Lambda
27
27
  export function observe<V>(
28
- observableMap: ObservableSet<V>,
28
+ // ObservableSet/ObservableMap are required despite they implement Set/Map: https://github.com/mobxjs/mobx/pull/3180#discussion_r746542929
29
+ observableSet: ObservableSet<V> | Set<V>,
29
30
  listener: (change: ISetDidChange<V>) => void,
30
31
  fireImmediately?: boolean
31
32
  ): Lambda
32
33
  export function observe<K, V>(
33
- observableMap: ObservableMap<K, V>,
34
+ observableMap: ObservableMap<K, V> | Map<K, V>,
34
35
  listener: (change: IMapDidChange<K, V>) => void,
35
36
  fireImmediately?: boolean
36
37
  ): Lambda
37
38
  export function observe<K, V>(
38
- observableMap: ObservableMap<K, V>,
39
+ observableMap: ObservableMap<K, V> | Map<K, V>,
39
40
  property: K,
40
41
  listener: (change: IValueDidChange<V>) => void,
41
42
  fireImmediately?: boolean
package/src/api/when.ts CHANGED
@@ -69,7 +69,7 @@ function whenPromise(
69
69
  return die(`the options 'onError' and 'promise' cannot be combined`)
70
70
  let cancel
71
71
  const res = new Promise((resolve, reject) => {
72
- let disposer = _when(predicate, resolve, { ...opts, onError: reject })
72
+ let disposer = _when(predicate, resolve as Lambda, { ...opts, onError: reject })
73
73
  cancel = () => {
74
74
  disposer()
75
75
  reject(new Error("WHEN_CANCELLED"))
package/src/core/atom.ts CHANGED
@@ -17,7 +17,7 @@ import {
17
17
  export const $mobx = Symbol("mobx administration")
18
18
 
19
19
  export interface IAtom extends IObservable {
20
- reportObserved()
20
+ reportObserved(): boolean
21
21
  reportChanged()
22
22
  }
23
23
 
@@ -98,7 +98,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
98
98
  isTracing_: TraceMode = TraceMode.NONE
99
99
  scope_: Object | undefined
100
100
  private equals_: IEqualsComparer<any>
101
- private requiresReaction_: boolean
101
+ private requiresReaction_: boolean | undefined
102
102
  keepAlive_: boolean
103
103
 
104
104
  /**
@@ -129,7 +129,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
129
129
  ? comparer.structural
130
130
  : comparer.default)
131
131
  this.scope_ = options.context
132
- this.requiresReaction_ = !!options.requiresReaction
132
+ this.requiresReaction_ = options.requiresReaction
133
133
  this.keepAlive_ = !!options.keepAlive
134
134
  }
135
135
 
@@ -293,7 +293,7 @@ export class ComputedValue<T> implements IObservable, IComputedValue<T>, IDeriva
293
293
  `[mobx.trace] Computed value '${this.name_}' is being read outside a reactive context. Doing a full recompute.`
294
294
  )
295
295
  }
296
- if (globalState.computedRequiresReaction || this.requiresReaction_) {
296
+ if (typeof this.requiresReaction_ === "boolean" ? this.requiresReaction_ : globalState.computedRequiresReaction) {
297
297
  console.warn(
298
298
  `[mobx] Computed value '${this.name_}' is being read outside a reactive context. Doing a full recompute.`
299
299
  )
@@ -61,7 +61,7 @@ const objectProxyTraps: ProxyHandler<any> = {
61
61
  // null (intercepted) -> true (success)
62
62
  return getAdm(target).defineProperty_(name, descriptor) ?? true
63
63
  },
64
- ownKeys(target: IIsObservableObject): PropertyKey[] {
64
+ ownKeys(target: IIsObservableObject): ArrayLike<string | symbol> {
65
65
  if (__DEV__ && globalState.trackingDerivation)
66
66
  warnAboutProxyRequirement(
67
67
  "iterate keys to detect added / removed properties. Use 'keys' from 'mobx' instead."
@@ -175,7 +175,8 @@ export class ObservableArrayAdministration
175
175
  }
176
176
 
177
177
  setArrayLength_(newLength: number) {
178
- if (typeof newLength !== "number" || isNaN(newLength) || newLength < 0) die("Out of range: " + newLength)
178
+ if (typeof newLength !== "number" || isNaN(newLength) || newLength < 0)
179
+ die("Out of range: " + newLength)
179
180
  let currentLength = this.values_.length
180
181
  if (newLength === currentLength) return
181
182
  else if (newLength > currentLength) {
@@ -235,9 +236,12 @@ export class ObservableArrayAdministration
235
236
  if (newItems.length < MAX_SPLICE_SIZE) {
236
237
  return this.values_.splice(index, deleteCount, ...newItems)
237
238
  } else {
239
+ // The items removed by the splice
238
240
  const res = this.values_.slice(index, index + deleteCount)
241
+ // The items that that should remain at the end of the array
239
242
  let oldItems = this.values_.slice(index + deleteCount)
240
- this.values_.length = index + newItems.length - deleteCount
243
+ // New length is the previous length + addition count - deletion count
244
+ this.values_.length += newItems.length - deleteCount
241
245
  for (let i = 0; i < newItems.length; i++) this.values_[index + i] = newItems[i]
242
246
  for (let i = 0; i < oldItems.length; i++)
243
247
  this.values_[index + newItems.length + i] = oldItems[i]
@@ -33,7 +33,8 @@ import {
33
33
  die,
34
34
  isFunction,
35
35
  UPDATE,
36
- IAtom
36
+ IAtom,
37
+ PureSpyEvent
37
38
  } from "../internal"
38
39
 
39
40
  export interface IKeyValueMap<V = any> {
@@ -85,7 +86,8 @@ export type IObservableMapInitialValues<K = any, V = any> =
85
86
  // just extend Map? See also https://gist.github.com/nestharus/13b4d74f2ef4a2f4357dbd3fc23c1e54
86
87
  // But: https://github.com/mobxjs/mobx/issues/1556
87
88
  export class ObservableMap<K = any, V = any>
88
- implements Map<K, V>, IInterceptable<IMapWillChange<K, V>>, IListenable {
89
+ implements Map<K, V>, IInterceptable<IMapWillChange<K, V>>, IListenable
90
+ {
89
91
  [$mobx] = ObservableMapMarker
90
92
  data_: Map<K, ObservableValue<V>>
91
93
  hasMap_: Map<K, ObservableValue<boolean>> // hasMap, not hashMap >-).
@@ -175,7 +177,7 @@ export class ObservableMap<K = any, V = any>
175
177
  }
176
178
  : null
177
179
 
178
- if (__DEV__ && notifySpy) spyReportStart(change!)
180
+ if (__DEV__ && notifySpy) spyReportStart(change! as PureSpyEvent) // TODO fix type
179
181
  transaction(() => {
180
182
  this.keysAtom_.reportChanged()
181
183
  this.hasMap_.get(key)?.setNewValue_(false)
@@ -208,7 +210,7 @@ export class ObservableMap<K = any, V = any>
208
210
  newValue
209
211
  }
210
212
  : null
211
- if (__DEV__ && notifySpy) spyReportStart(change!)
213
+ if (__DEV__ && notifySpy) spyReportStart(change! as PureSpyEvent) // TODO fix type
212
214
  observable.setNewValue_(newValue as V)
213
215
  if (notify) notifyListeners(this, change)
214
216
  if (__DEV__ && notifySpy) spyReportEnd()
@@ -242,7 +244,7 @@ export class ObservableMap<K = any, V = any>
242
244
  newValue
243
245
  }
244
246
  : null
245
- if (__DEV__ && notifySpy) spyReportStart(change!)
247
+ if (__DEV__ && notifySpy) spyReportStart(change! as PureSpyEvent) // TODO fix type
246
248
  if (notify) notifyListeners(this, change)
247
249
  if (__DEV__ && notifySpy) spyReportEnd()
248
250
  }
@@ -308,7 +310,7 @@ export class ObservableMap<K = any, V = any>
308
310
  transaction(() => {
309
311
  if (isPlainObject(other))
310
312
  getPlainObjectKeys(other).forEach((key: any) =>
311
- this.set((key as any) as K, other[key])
313
+ this.set(key as any as K, other[key])
312
314
  )
313
315
  else if (Array.isArray(other)) other.forEach(([key, value]) => this.set(key, value))
314
316
  else if (isES6Map(other)) {
@@ -88,7 +88,8 @@ export type IObjectWillChange<T = any> =
88
88
  const REMOVE = "remove"
89
89
 
90
90
  export class ObservableObjectAdministration
91
- implements IInterceptable<IObjectWillChange>, IListenable {
91
+ implements IInterceptable<IObjectWillChange>, IListenable
92
+ {
92
93
  keysAtom_: IAtom
93
94
  changeListeners_
94
95
  interceptors_
@@ -590,7 +591,7 @@ export class ObservableObjectAdministration
590
591
  this.keysAtom_.reportChanged()
591
592
  }
592
593
 
593
- ownKeys_(): PropertyKey[] {
594
+ ownKeys_(): ArrayLike<string | symbol> {
594
595
  this.keysAtom_.reportObserved()
595
596
  return ownKeys(this.target_)
596
597
  }
package/src/utils/eq.ts CHANGED
@@ -28,7 +28,7 @@ function eq(a: any, b: any, depth: number, aStack?: any[], bStack?: any[]) {
28
28
  if (a !== a) return b !== b
29
29
  // Exhaust primitive checks
30
30
  const type = typeof a
31
- if (!isFunction(type) && type !== "object" && typeof b != "object") return false
31
+ if (type !== "function" && type !== "object" && typeof b != "object") return false
32
32
 
33
33
  // Compare `[[Class]]` names.
34
34
  const className = toString.call(a)
@@ -34,7 +34,7 @@ export function warnAboutProxyRequirement(msg: string) {
34
34
  if (__DEV__ && globalState.verifyProxies) {
35
35
  die(
36
36
  "MobX is currently configured to be able to run in ES5 mode, but in ES5 MobX won't be able to " +
37
- msg
37
+ msg
38
38
  )
39
39
  }
40
40
  }
@@ -55,7 +55,7 @@ export function once(func: Lambda): Lambda {
55
55
  }
56
56
  }
57
57
 
58
- export const noop = () => {}
58
+ export const noop = () => { }
59
59
 
60
60
  export function isFunction(fn: any): fn is Function {
61
61
  return typeof fn === "function"
@@ -84,7 +84,8 @@ export function isPlainObject(value) {
84
84
  if (!isObject(value)) return false
85
85
  const proto = Object.getPrototypeOf(value)
86
86
  if (proto == null) return true
87
- return proto.constructor?.toString() === plainObjectString
87
+ const protoConstructor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
88
+ return typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
88
89
  }
89
90
 
90
91
  // https://stackoverflow.com/a/37865170
@@ -149,12 +150,12 @@ export function getPlainObjectKeys(object) {
149
150
 
150
151
  // From Immer utils
151
152
  // Returns all own keys, including non-enumerable and symbolic
152
- export const ownKeys: (target: any) => PropertyKey[] =
153
+ export const ownKeys: (target: any) => Array<string | symbol> =
153
154
  typeof Reflect !== "undefined" && Reflect.ownKeys
154
155
  ? Reflect.ownKeys
155
156
  : hasGetOwnPropertySymbols
156
- ? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
157
- : /* istanbul ignore next */ Object.getOwnPropertyNames
157
+ ? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
158
+ : /* istanbul ignore next */ Object.getOwnPropertyNames
158
159
 
159
160
  export function stringifyKey(key: any): string {
160
161
  if (typeof key === "string") return key