mobx 6.16.0 → 7.0.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.
- package/CHANGELOG.md +80 -0
- package/README.md +9 -13
- package/dist/api/action.d.ts +6 -4
- package/dist/api/annotation.d.ts +0 -1
- package/dist/api/computed.d.ts +4 -7
- package/dist/api/configure.d.ts +0 -1
- package/dist/api/decoratorannotation.d.ts +3 -0
- package/dist/api/decorators.d.ts +0 -19
- package/dist/api/flow.d.ts +4 -5
- package/dist/api/makeObservable.d.ts +2 -3
- package/dist/api/observable.d.ts +9 -15
- package/dist/core/atom.d.ts +0 -3
- package/dist/core/computedvalue.d.ts +4 -32
- package/dist/core/derivation.d.ts +1 -7
- package/dist/core/globalstate.d.ts +0 -2
- package/dist/core/reaction.d.ts +1 -9
- package/dist/errors.d.ts +6 -4
- package/dist/internal.d.ts +1 -4
- package/dist/mobx.cjs.development.js +1647 -2444
- package/dist/mobx.cjs.development.js.map +1 -1
- package/dist/mobx.cjs.production.min.js +1 -1
- package/dist/mobx.cjs.production.min.js.map +1 -1
- package/dist/mobx.d.ts +2 -2
- package/dist/mobx.esm.development.js +1636 -2443
- package/dist/mobx.esm.development.js.map +1 -1
- package/dist/mobx.esm.js +1584 -2394
- package/dist/mobx.esm.js.map +1 -1
- package/dist/mobx.esm.production.min.js +1 -1
- package/dist/mobx.esm.production.min.js.map +1 -1
- package/dist/mobx.mjs +5271 -0
- package/dist/mobx.mjs.map +1 -0
- package/dist/mobx.umd.development.js +1650 -2447
- package/dist/mobx.umd.development.js.map +1 -1
- package/dist/mobx.umd.production.min.js +1 -1
- package/dist/mobx.umd.production.min.js.map +1 -1
- package/dist/types/actionannotation.d.ts +1 -0
- package/dist/types/computedannotation.d.ts +1 -0
- package/dist/types/decorator_fills.d.ts +2 -0
- package/dist/types/flowannotation.d.ts +1 -0
- package/dist/types/observableannotation.d.ts +5 -0
- package/dist/types/observablearray.d.ts +2 -5
- package/dist/types/observablemap.d.ts +1 -8
- package/dist/types/observableobject.d.ts +2 -13
- package/dist/types/observableset.d.ts +1 -3
- package/dist/types/observablevalue.d.ts +4 -6
- package/dist/types/overrideannotation.d.ts +1 -2
- package/dist/utils/comparer.d.ts +4 -11
- package/dist/utils/utils.d.ts +0 -2
- package/package.json +26 -18
- package/src/api/action.ts +27 -35
- package/src/api/annotation.ts +0 -1
- package/src/api/autorun.ts +2 -4
- package/src/api/computed.ts +21 -28
- package/src/api/configure.ts +1 -15
- package/src/api/decoratorannotation.ts +20 -0
- package/src/api/decorators.ts +1 -85
- package/src/api/extras.ts +1 -1
- package/src/api/flow.ts +26 -29
- package/src/api/intercept.ts +4 -3
- package/src/api/makeObservable.ts +45 -17
- package/src/api/object-api.ts +1 -1
- package/src/api/observable.ts +34 -67
- package/src/api/observe.ts +82 -3
- package/src/api/when.ts +4 -3
- package/src/core/action.ts +12 -5
- package/src/core/atom.ts +14 -13
- package/src/core/computedvalue.ts +27 -67
- package/src/core/derivation.ts +15 -14
- package/src/core/globalstate.ts +8 -13
- package/src/core/observable.ts +0 -48
- package/src/core/reaction.ts +19 -25
- package/src/core/spy.ts +3 -3
- package/src/errors.ts +23 -12
- package/src/internal.ts +1 -4
- package/src/mobx.ts +21 -10
- package/src/types/actionannotation.ts +5 -9
- package/src/types/autoannotation.ts +8 -11
- package/src/types/computedannotation.ts +40 -21
- package/src/types/decorator_fills.ts +4 -0
- package/src/types/dynamicobject.ts +0 -29
- package/src/types/flowannotation.ts +7 -4
- package/src/types/observableannotation.ts +6 -7
- package/src/types/observablearray.ts +8 -61
- package/src/types/observablemap.ts +4 -29
- package/src/types/observableobject.ts +5 -70
- package/src/types/observableset.ts +5 -28
- package/src/types/observablevalue.ts +7 -29
- package/src/types/overrideannotation.ts +7 -22
- package/src/types/type-utils.ts +4 -2
- package/src/utils/comparer.ts +4 -17
- package/src/utils/iterable.ts +3 -4
- package/src/utils/utils.ts +4 -46
- package/dist/api/trace.d.ts +0 -3
- package/dist/types/legacyobservablearray.d.ts +0 -14
- package/dist/utils/global.d.ts +0 -1
- package/src/api/trace.ts +0 -35
- package/src/types/legacyobservablearray.ts +0 -155
- package/src/utils/global.ts +0 -20
|
@@ -5,9 +5,7 @@ import {
|
|
|
5
5
|
IAtom,
|
|
6
6
|
IEnhancer,
|
|
7
7
|
IInterceptable,
|
|
8
|
-
IInterceptor,
|
|
9
8
|
IListenable,
|
|
10
|
-
Lambda,
|
|
11
9
|
addHiddenFinalProp,
|
|
12
10
|
checkIfStateModificationsAreAllowed,
|
|
13
11
|
createInstanceofPredicate,
|
|
@@ -18,12 +16,8 @@ import {
|
|
|
18
16
|
isObject,
|
|
19
17
|
isSpyEnabled,
|
|
20
18
|
notifyListeners,
|
|
21
|
-
registerInterceptor,
|
|
22
|
-
registerListener,
|
|
23
19
|
spyReportEnd,
|
|
24
20
|
spyReportStart,
|
|
25
|
-
assertProxies,
|
|
26
|
-
reserveArrayBuffer,
|
|
27
21
|
hasProp,
|
|
28
22
|
die,
|
|
29
23
|
globalState,
|
|
@@ -130,8 +124,7 @@ export class ObservableArrayAdministration
|
|
|
130
124
|
constructor(
|
|
131
125
|
name = __DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray",
|
|
132
126
|
enhancer: IEnhancer<any>,
|
|
133
|
-
public owned_: boolean
|
|
134
|
-
public legacyMode_: boolean
|
|
127
|
+
public owned_: boolean
|
|
135
128
|
) {
|
|
136
129
|
this.atom_ = new Atom(name)
|
|
137
130
|
this.enhancer_ = (newV, oldV) =>
|
|
@@ -152,30 +145,6 @@ export class ObservableArrayAdministration
|
|
|
152
145
|
return values
|
|
153
146
|
}
|
|
154
147
|
|
|
155
|
-
intercept_(handler: IInterceptor<IArrayWillChange<any> | IArrayWillSplice<any>>): Lambda {
|
|
156
|
-
return registerInterceptor<IArrayWillChange<any> | IArrayWillSplice<any>>(this, handler)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
observe_(
|
|
160
|
-
listener: (changeData: IArrayDidChange<any>) => void,
|
|
161
|
-
fireImmediately = false
|
|
162
|
-
): Lambda {
|
|
163
|
-
if (fireImmediately) {
|
|
164
|
-
listener(<IArraySplice<any>>{
|
|
165
|
-
observableKind: "array",
|
|
166
|
-
object: this.proxy_ as any,
|
|
167
|
-
debugObjectName: this.atom_.name_,
|
|
168
|
-
type: "splice",
|
|
169
|
-
index: 0,
|
|
170
|
-
added: this.values_.slice(),
|
|
171
|
-
addedCount: this.values_.length,
|
|
172
|
-
removed: [],
|
|
173
|
-
removedCount: 0
|
|
174
|
-
})
|
|
175
|
-
}
|
|
176
|
-
return registerListener(this, listener)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
148
|
getArrayLength_(): number {
|
|
180
149
|
this.atom_.reportObserved()
|
|
181
150
|
return this.values_.length
|
|
@@ -183,16 +152,13 @@ export class ObservableArrayAdministration
|
|
|
183
152
|
|
|
184
153
|
setArrayLength_(newLength: number) {
|
|
185
154
|
if (typeof newLength !== "number" || isNaN(newLength) || newLength < 0) {
|
|
186
|
-
die(
|
|
155
|
+
die(40, newLength)
|
|
187
156
|
}
|
|
188
157
|
let currentLength = this.values_.length
|
|
189
158
|
if (newLength === currentLength) {
|
|
190
159
|
return
|
|
191
160
|
} else if (newLength > currentLength) {
|
|
192
|
-
const newItems =
|
|
193
|
-
for (let i = 0; i < newLength - currentLength; i++) {
|
|
194
|
-
newItems[i] = undefined
|
|
195
|
-
} // No Array.fill everywhere...
|
|
161
|
+
const newItems = Array.from({ length: newLength - currentLength })
|
|
196
162
|
this.spliceWithArray_(currentLength, 0, newItems)
|
|
197
163
|
} else {
|
|
198
164
|
this.spliceWithArray_(newLength, currentLength - newLength)
|
|
@@ -204,9 +170,6 @@ export class ObservableArrayAdministration
|
|
|
204
170
|
die(16)
|
|
205
171
|
}
|
|
206
172
|
this.lastKnownLength_ += delta
|
|
207
|
-
if (this.legacyMode_ && delta > 0) {
|
|
208
|
-
reserveArrayBuffer(oldLength + delta + 1)
|
|
209
|
-
}
|
|
210
173
|
}
|
|
211
174
|
|
|
212
175
|
spliceWithArray_(index: number, deleteCount?: number, newItems?: any[]): any[] {
|
|
@@ -250,7 +213,7 @@ export class ObservableArrayAdministration
|
|
|
250
213
|
|
|
251
214
|
newItems =
|
|
252
215
|
newItems.length === 0 ? newItems : newItems.map(v => this.enhancer_(v, undefined))
|
|
253
|
-
if (
|
|
216
|
+
if (__DEV__) {
|
|
254
217
|
const lengthDelta = newItems.length - deleteCount
|
|
255
218
|
this.updateArrayLength_(length, lengthDelta) // checks if internal array wasn't modified
|
|
256
219
|
}
|
|
@@ -283,7 +246,7 @@ export class ObservableArrayAdministration
|
|
|
283
246
|
}
|
|
284
247
|
|
|
285
248
|
notifyArrayChildUpdate_(index: number, newValue: any, oldValue: any) {
|
|
286
|
-
const notifySpy = !this.owned_ && isSpyEnabled()
|
|
249
|
+
const notifySpy = __DEV__ && !this.owned_ && isSpyEnabled()
|
|
287
250
|
const notify = hasListeners(this)
|
|
288
251
|
const change: IArrayDidChange | null =
|
|
289
252
|
notify || notifySpy
|
|
@@ -313,7 +276,7 @@ export class ObservableArrayAdministration
|
|
|
313
276
|
}
|
|
314
277
|
|
|
315
278
|
notifyArraySplice_(index: number, added: any[], removed: any[]) {
|
|
316
|
-
const notifySpy = !this.owned_ && isSpyEnabled()
|
|
279
|
+
const notifySpy = __DEV__ && !this.owned_ && isSpyEnabled()
|
|
317
280
|
const notify = hasListeners(this)
|
|
318
281
|
const change: IArraySplice | null =
|
|
319
282
|
notify || notifySpy
|
|
@@ -344,24 +307,12 @@ export class ObservableArrayAdministration
|
|
|
344
307
|
}
|
|
345
308
|
|
|
346
309
|
get_(index: number): any | undefined {
|
|
347
|
-
if (this.legacyMode_ && index >= this.values_.length) {
|
|
348
|
-
console.warn(
|
|
349
|
-
__DEV__
|
|
350
|
-
? `[mobx.array] Attempt to read an array index (${index}) that is out of bounds (${this.values_.length}). Please check length first. Out of bound indices will not be tracked by MobX`
|
|
351
|
-
: `[mobx] Out of bounds read: ${index}`
|
|
352
|
-
)
|
|
353
|
-
return undefined
|
|
354
|
-
}
|
|
355
310
|
this.atom_.reportObserved()
|
|
356
311
|
return this.dehanceValue_(this.values_[index])
|
|
357
312
|
}
|
|
358
313
|
|
|
359
314
|
set_(index: number, newValue: any) {
|
|
360
315
|
const values = this.values_
|
|
361
|
-
if (this.legacyMode_ && index > values.length) {
|
|
362
|
-
// out of bounds
|
|
363
|
-
die(17, index, values.length)
|
|
364
|
-
}
|
|
365
316
|
if (index < values.length) {
|
|
366
317
|
// update at index in range
|
|
367
318
|
checkIfStateModificationsAreAllowed(this.atom_)
|
|
@@ -388,10 +339,7 @@ export class ObservableArrayAdministration
|
|
|
388
339
|
// For out of bound index, we don't create an actual sparse array,
|
|
389
340
|
// but rather fill the holes with undefined (same as setArrayLength_).
|
|
390
341
|
// This could be considered a bug.
|
|
391
|
-
const newItems =
|
|
392
|
-
for (let i = 0; i < newItems.length - 1; i++) {
|
|
393
|
-
newItems[i] = undefined
|
|
394
|
-
} // No Array.fill everywhere...
|
|
342
|
+
const newItems = Array.from({ length: index + 1 - values.length })
|
|
395
343
|
newItems[newItems.length - 1] = newValue
|
|
396
344
|
this.spliceWithArray_(values.length, 0, newItems)
|
|
397
345
|
}
|
|
@@ -404,9 +352,8 @@ export function createObservableArray<T>(
|
|
|
404
352
|
name = __DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray",
|
|
405
353
|
owned = false
|
|
406
354
|
): IObservableArray<T> {
|
|
407
|
-
assertProxies()
|
|
408
355
|
return initObservable(() => {
|
|
409
|
-
const adm = new ObservableArrayAdministration(name, enhancer, owned
|
|
356
|
+
const adm = new ObservableArrayAdministration(name, enhancer, owned)
|
|
410
357
|
addHiddenFinalProp(adm.values_, $mobx, adm)
|
|
411
358
|
const proxy = new Proxy(adm.values_, arrayTraps) as any
|
|
412
359
|
adm.proxy_ = proxy
|
|
@@ -2,9 +2,7 @@ import {
|
|
|
2
2
|
$mobx,
|
|
3
3
|
IEnhancer,
|
|
4
4
|
IInterceptable,
|
|
5
|
-
IInterceptor,
|
|
6
5
|
IListenable,
|
|
7
|
-
Lambda,
|
|
8
6
|
ObservableValue,
|
|
9
7
|
checkIfStateModificationsAreAllowed,
|
|
10
8
|
createAtom,
|
|
@@ -22,17 +20,13 @@ import {
|
|
|
22
20
|
isSpyEnabled,
|
|
23
21
|
notifyListeners,
|
|
24
22
|
referenceEnhancer,
|
|
25
|
-
registerInterceptor,
|
|
26
|
-
registerListener,
|
|
27
23
|
spyReportEnd,
|
|
28
24
|
spyReportStart,
|
|
29
25
|
stringifyKey,
|
|
30
26
|
transaction,
|
|
31
27
|
untracked,
|
|
32
|
-
onBecomeUnobserved,
|
|
33
28
|
globalState,
|
|
34
29
|
die,
|
|
35
|
-
isFunction,
|
|
36
30
|
UPDATE,
|
|
37
31
|
IAtom,
|
|
38
32
|
PureSpyEvent,
|
|
@@ -106,9 +100,6 @@ export class ObservableMap<K = any, V = any>
|
|
|
106
100
|
public enhancer_: IEnhancer<V> = deepEnhancer,
|
|
107
101
|
public name_ = __DEV__ ? "ObservableMap@" + getNextId() : "ObservableMap"
|
|
108
102
|
) {
|
|
109
|
-
if (!isFunction(Map)) {
|
|
110
|
-
die(18)
|
|
111
|
-
}
|
|
112
103
|
initObservable(() => {
|
|
113
104
|
this.keysAtom_ = createAtom(__DEV__ ? `${this.name_}.keys()` : "ObservableMap.keys()")
|
|
114
105
|
this.data_ = new Map()
|
|
@@ -137,7 +128,7 @@ export class ObservableMap<K = any, V = any>
|
|
|
137
128
|
false
|
|
138
129
|
))
|
|
139
130
|
this.hasMap_.set(key, newEntry)
|
|
140
|
-
|
|
131
|
+
newEntry.onBUOL = new Set([() => this.hasMap_.delete(key)])
|
|
141
132
|
}
|
|
142
133
|
|
|
143
134
|
return entry.get()
|
|
@@ -178,7 +169,7 @@ export class ObservableMap<K = any, V = any>
|
|
|
178
169
|
}
|
|
179
170
|
}
|
|
180
171
|
if (this.has_(key)) {
|
|
181
|
-
const notifySpy = isSpyEnabled()
|
|
172
|
+
const notifySpy = __DEV__ && isSpyEnabled()
|
|
182
173
|
const notify = hasListeners(this)
|
|
183
174
|
const change: IMapDidChange<K, V> | null =
|
|
184
175
|
notify || notifySpy
|
|
@@ -217,7 +208,7 @@ export class ObservableMap<K = any, V = any>
|
|
|
217
208
|
const observable = this.data_.get(key)!
|
|
218
209
|
newValue = (observable as any).prepareNewValue_(newValue) as V
|
|
219
210
|
if (newValue !== globalState.UNCHANGED) {
|
|
220
|
-
const notifySpy = isSpyEnabled()
|
|
211
|
+
const notifySpy = __DEV__ && isSpyEnabled()
|
|
221
212
|
const notify = hasListeners(this)
|
|
222
213
|
const change: IMapDidChange<K, V> | null =
|
|
223
214
|
notify || notifySpy
|
|
@@ -258,7 +249,7 @@ export class ObservableMap<K = any, V = any>
|
|
|
258
249
|
this.hasMap_.get(key)?.setNewValue_(true)
|
|
259
250
|
this.keysAtom_.reportChanged()
|
|
260
251
|
})
|
|
261
|
-
const notifySpy = isSpyEnabled()
|
|
252
|
+
const notifySpy = __DEV__ && isSpyEnabled()
|
|
262
253
|
const notify = hasListeners(this)
|
|
263
254
|
const change: IMapDidChange<K, V> | null =
|
|
264
255
|
notify || notifySpy
|
|
@@ -481,22 +472,6 @@ export class ObservableMap<K = any, V = any>
|
|
|
481
472
|
get [Symbol.toStringTag]() {
|
|
482
473
|
return "Map"
|
|
483
474
|
}
|
|
484
|
-
|
|
485
|
-
/**
|
|
486
|
-
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
|
|
487
|
-
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
|
|
488
|
-
* for callback details
|
|
489
|
-
*/
|
|
490
|
-
observe_(listener: (changes: IMapDidChange<K, V>) => void, fireImmediately?: boolean): Lambda {
|
|
491
|
-
if (__DEV__ && fireImmediately === true) {
|
|
492
|
-
die("`observe` doesn't support fireImmediately=true in combination with maps.")
|
|
493
|
-
}
|
|
494
|
-
return registerListener(this, listener)
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
intercept_(handler: IInterceptor<IMapWillChange<K, V>>): Lambda {
|
|
498
|
-
return registerInterceptor(this, handler)
|
|
499
|
-
}
|
|
500
475
|
}
|
|
501
476
|
|
|
502
477
|
// eslint-disable-next-line
|
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
IEnhancer,
|
|
13
13
|
IInterceptable,
|
|
14
14
|
IListenable,
|
|
15
|
-
Lambda,
|
|
16
15
|
ObservableValue,
|
|
17
16
|
addHiddenProp,
|
|
18
17
|
createInstanceofPredicate,
|
|
@@ -26,8 +25,6 @@ import {
|
|
|
26
25
|
isSpyEnabled,
|
|
27
26
|
notifyListeners,
|
|
28
27
|
referenceEnhancer,
|
|
29
|
-
registerInterceptor,
|
|
30
|
-
registerListener,
|
|
31
28
|
spyReportEnd,
|
|
32
29
|
spyReportStart,
|
|
33
30
|
startBatch,
|
|
@@ -38,16 +35,14 @@ import {
|
|
|
38
35
|
die,
|
|
39
36
|
hasProp,
|
|
40
37
|
getDescriptor,
|
|
41
|
-
storedAnnotationsSymbol,
|
|
42
38
|
ownKeys,
|
|
43
39
|
isOverride,
|
|
44
40
|
defineProperty,
|
|
45
41
|
autoAnnotation,
|
|
46
42
|
getAdministration,
|
|
47
43
|
getDebugName,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
checkIfStateModificationsAreAllowed
|
|
44
|
+
checkIfStateModificationsAreAllowed,
|
|
45
|
+
assign
|
|
51
46
|
} from "../internal"
|
|
52
47
|
|
|
53
48
|
const descriptorCache = Object.create(null)
|
|
@@ -274,47 +269,6 @@ export class ObservableObjectAdministration
|
|
|
274
269
|
return entry.get()
|
|
275
270
|
}
|
|
276
271
|
|
|
277
|
-
/**
|
|
278
|
-
* @param {PropertyKey} key
|
|
279
|
-
* @param {Annotation|boolean} annotation true - use default annotation, false - ignore prop
|
|
280
|
-
*/
|
|
281
|
-
make_(key: PropertyKey, annotation: Annotation | boolean): void {
|
|
282
|
-
if (annotation === true) {
|
|
283
|
-
annotation = this.defaultAnnotation_
|
|
284
|
-
}
|
|
285
|
-
if (annotation === false) {
|
|
286
|
-
return
|
|
287
|
-
}
|
|
288
|
-
assertAnnotable(this, annotation, key)
|
|
289
|
-
if (!(key in this.target_)) {
|
|
290
|
-
// Throw on missing key, except for decorators:
|
|
291
|
-
// Decorator annotations are collected from whole prototype chain.
|
|
292
|
-
// When called from super() some props may not exist yet.
|
|
293
|
-
// However we don't have to worry about missing prop,
|
|
294
|
-
// because the decorator must have been applied to something.
|
|
295
|
-
if (this.target_[storedAnnotationsSymbol]?.[key]) {
|
|
296
|
-
return // will be annotated by subclass constructor
|
|
297
|
-
} else {
|
|
298
|
-
die(1, annotation.annotationType_, `${this.name_}.${key.toString()}`)
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
let source = this.target_
|
|
302
|
-
while (source && source !== objectPrototype) {
|
|
303
|
-
const descriptor = getDescriptor(source, key)
|
|
304
|
-
if (descriptor) {
|
|
305
|
-
const outcome = annotation.make_(this, key, descriptor, source)
|
|
306
|
-
if (outcome === MakeResult.Cancel) {
|
|
307
|
-
return
|
|
308
|
-
}
|
|
309
|
-
if (outcome === MakeResult.Break) {
|
|
310
|
-
break
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
source = Object.getPrototypeOf(source)
|
|
314
|
-
}
|
|
315
|
-
recordAnnotationApplied(this, annotation, key)
|
|
316
|
-
}
|
|
317
|
-
|
|
318
272
|
/**
|
|
319
273
|
* @param {PropertyKey} key
|
|
320
274
|
* @param {PropertyDescriptor} descriptor
|
|
@@ -377,10 +331,9 @@ export class ObservableObjectAdministration
|
|
|
377
331
|
}
|
|
378
332
|
const { newValue } = change as any
|
|
379
333
|
if (descriptor.value !== newValue) {
|
|
380
|
-
descriptor = {
|
|
381
|
-
...descriptor,
|
|
334
|
+
descriptor = assign({}, descriptor, {
|
|
382
335
|
value: newValue
|
|
383
|
-
}
|
|
336
|
+
})
|
|
384
337
|
}
|
|
385
338
|
}
|
|
386
339
|
|
|
@@ -618,22 +571,6 @@ export class ObservableObjectAdministration
|
|
|
618
571
|
return true
|
|
619
572
|
}
|
|
620
573
|
|
|
621
|
-
/**
|
|
622
|
-
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
|
|
623
|
-
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
|
|
624
|
-
* for callback details
|
|
625
|
-
*/
|
|
626
|
-
observe_(callback: (changes: IObjectDidChange) => void, fireImmediately?: boolean): Lambda {
|
|
627
|
-
if (__DEV__ && fireImmediately === true) {
|
|
628
|
-
die("`observe` doesn't support the fire immediately property for observable objects.")
|
|
629
|
-
}
|
|
630
|
-
return registerListener(this, callback)
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
intercept_(handler): Lambda {
|
|
634
|
-
return registerInterceptor(this, handler)
|
|
635
|
-
}
|
|
636
|
-
|
|
637
574
|
notifyPropertyAddition_(key: PropertyKey, value: any) {
|
|
638
575
|
const notify = hasListeners(this)
|
|
639
576
|
const notifySpy = __DEV__ && isSpyEnabled()
|
|
@@ -765,11 +702,9 @@ export function recordAnnotationApplied(
|
|
|
765
702
|
if (__DEV__) {
|
|
766
703
|
adm.appliedAnnotations_![key] = annotation
|
|
767
704
|
}
|
|
768
|
-
// Remove applied decorator annotation so we don't try to apply it again in subclass constructor
|
|
769
|
-
delete adm.target_[storedAnnotationsSymbol]?.[key]
|
|
770
705
|
}
|
|
771
706
|
|
|
772
|
-
function assertAnnotable(
|
|
707
|
+
export function assertAnnotable(
|
|
773
708
|
adm: ObservableObjectAdministration,
|
|
774
709
|
annotation: Annotation,
|
|
775
710
|
key: PropertyKey
|
|
@@ -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(() => {
|
|
@@ -304,24 +293,12 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
|
|
|
304
293
|
this.clear()
|
|
305
294
|
other.forEach(value => this.add(value))
|
|
306
295
|
} else if (other !== null && other !== undefined) {
|
|
307
|
-
die(
|
|
296
|
+
die(41, other)
|
|
308
297
|
}
|
|
309
298
|
})
|
|
310
299
|
|
|
311
300
|
return this
|
|
312
301
|
}
|
|
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
302
|
toJSON(): T[] {
|
|
326
303
|
return Array.from(this)
|
|
327
304
|
}
|
|
@@ -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
|
-
|
|
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
|
|
68
|
+
public enhancer_: IEnhancer<T>,
|
|
73
69
|
public name_ = __DEV__ ? "ObservableValue@" + getNextId() : "ObservableValue",
|
|
74
70
|
notifySpy = true,
|
|
75
|
-
private
|
|
71
|
+
private equals_: IEqualsComparer<any> = compareDefault
|
|
76
72
|
) {
|
|
77
73
|
super(name_)
|
|
78
|
-
this.value_ =
|
|
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.
|
|
135
|
-
return this.
|
|
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
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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(
|
|
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
|
}
|
package/src/types/type-utils.ts
CHANGED
|
@@ -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
|
-
|
|
120
|
+
if (__DEV__) {
|
|
121
|
+
allowStateChangesEnd(allowStateChanges)
|
|
122
|
+
}
|
|
121
123
|
untrackedEnd(derivation)
|
|
122
124
|
}
|
|
123
125
|
}
|
package/src/utils/comparer.ts
CHANGED
|
@@ -4,29 +4,16 @@ export interface IEqualsComparer<T> {
|
|
|
4
4
|
(a: T, b: T): boolean
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
function
|
|
7
|
+
export function compareIdentity(a: any, b: any): boolean {
|
|
8
8
|
return a === b
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
function
|
|
11
|
+
export function compareStructural(a: any, b: any): boolean {
|
|
12
12
|
return deepEqual(a, b)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function
|
|
15
|
+
export function compareShallow(a: any, b: any): boolean {
|
|
16
16
|
return deepEqual(a, b, 1)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
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
|
package/src/utils/iterable.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assign } from "./utils"
|
|
2
2
|
|
|
3
3
|
// safely get iterator prototype if available
|
|
4
|
-
const
|
|
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
|
|
10
|
+
return assign(Object.create(maybeIteratorPrototype), iterator)
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
function getSelf() {
|