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.
- package/CHANGELOG.md +86 -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 +1 -4
- package/dist/core/computedvalue.d.ts +5 -33
- package/dist/core/derivation.d.ts +1 -7
- package/dist/core/globalstate.d.ts +7 -2
- package/dist/core/observable.d.ts +9 -1
- 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 +1704 -2484
- 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 +1693 -2483
- package/dist/mobx.esm.development.js.map +1 -1
- package/dist/mobx.esm.js +1643 -2436
- 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 +5308 -0
- package/dist/mobx.mjs.map +1 -0
- package/dist/mobx.umd.development.js +1707 -2487
- 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 +25 -15
- 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 +16 -14
- package/src/core/computedvalue.ts +36 -69
- package/src/core/derivation.ts +16 -15
- package/src/core/globalstate.ts +16 -13
- package/src/core/observable.ts +65 -73
- 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 +13 -11
- 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/modifiers.ts +5 -0
- 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 +9 -52
- 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
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
assert20223DecoratorType,
|
|
7
7
|
$mobx,
|
|
8
8
|
asObservableObject,
|
|
9
|
-
ComputedValue
|
|
9
|
+
ComputedValue,
|
|
10
|
+
assign
|
|
10
11
|
} from "../internal"
|
|
11
12
|
|
|
12
13
|
export function createComputedAnnotation(name: string, options?: object): Annotation {
|
|
@@ -14,8 +15,7 @@ export function createComputedAnnotation(name: string, options?: object): Annota
|
|
|
14
15
|
annotationType_: name,
|
|
15
16
|
options_: options,
|
|
16
17
|
make_,
|
|
17
|
-
extend_
|
|
18
|
-
decorate_20223_
|
|
18
|
+
extend_
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -38,20 +38,23 @@ function extend_(
|
|
|
38
38
|
assertComputedDescriptor(adm, this, key, descriptor)
|
|
39
39
|
return adm.defineComputedProperty_(
|
|
40
40
|
key,
|
|
41
|
-
{
|
|
42
|
-
...this.options_,
|
|
41
|
+
assign({}, this.options_, {
|
|
43
42
|
get: descriptor.get,
|
|
44
43
|
set: descriptor.set
|
|
45
|
-
},
|
|
44
|
+
}),
|
|
46
45
|
proxyTrap
|
|
47
46
|
)
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
function
|
|
49
|
+
export function decorateComputed20223_(
|
|
50
|
+
annotation: Annotation,
|
|
51
|
+
get,
|
|
52
|
+
context: ClassGetterDecoratorContext
|
|
53
|
+
) {
|
|
51
54
|
if (__DEV__) {
|
|
52
55
|
assert20223DecoratorType(context, ["getter"])
|
|
53
56
|
}
|
|
54
|
-
const ann =
|
|
57
|
+
const ann = annotation
|
|
55
58
|
const { name: key, addInitializer } = context
|
|
56
59
|
let computedValues: WeakMap<object, ComputedValue<any>> | undefined
|
|
57
60
|
|
|
@@ -59,11 +62,10 @@ function decorate_20223_(this: Annotation, get, context: ClassGetterDecoratorCon
|
|
|
59
62
|
// ComputedValues for getters that are never read on a given instance.
|
|
60
63
|
// The factory is materialised by ObservableObjectAdministration on demand.
|
|
61
64
|
function createComputedValue(target: object, adm: ObservableObjectAdministration) {
|
|
62
|
-
const options = {
|
|
63
|
-
...ann.options_,
|
|
65
|
+
const options = assign({}, ann.options_, {
|
|
64
66
|
get,
|
|
65
67
|
context: target
|
|
66
|
-
}
|
|
68
|
+
})
|
|
67
69
|
options.name ||= __DEV__
|
|
68
70
|
? `${adm.name_}.${key.toString()}`
|
|
69
71
|
: `ObservableObject.${key.toString()}`
|
|
@@ -25,6 +25,10 @@ export type ClassFieldDecorator<This = any, Value extends (...p: any[]) => any =
|
|
|
25
25
|
context: ClassFieldDecoratorContext<This, Value>
|
|
26
26
|
) => Value | void
|
|
27
27
|
|
|
28
|
+
export type ClassMethodAndFieldDecorator = ClassMethodDecorator & ClassFieldDecorator
|
|
29
|
+
|
|
30
|
+
export type ClassAccessorAndFieldDecorator = ClassAccessorDecorator & ClassFieldDecorator
|
|
31
|
+
|
|
28
32
|
export type Decorator =
|
|
29
33
|
| ClassAccessorDecorator
|
|
30
34
|
| ClassGetterDecorator
|
|
@@ -2,11 +2,8 @@ import {
|
|
|
2
2
|
$mobx,
|
|
3
3
|
IIsObservableObject,
|
|
4
4
|
ObservableObjectAdministration,
|
|
5
|
-
warnAboutProxyRequirement,
|
|
6
|
-
assertProxies,
|
|
7
5
|
die,
|
|
8
6
|
isStringish,
|
|
9
|
-
globalState,
|
|
10
7
|
CreateObservableOptions,
|
|
11
8
|
asObservableObject
|
|
12
9
|
} from "../internal"
|
|
@@ -19,11 +16,6 @@ function getAdm(target): ObservableObjectAdministration {
|
|
|
19
16
|
// and skip either the internal values map, or the base object with its property descriptors!
|
|
20
17
|
const objectProxyTraps: ProxyHandler<any> = {
|
|
21
18
|
has(target: IIsObservableObject, name: PropertyKey): boolean {
|
|
22
|
-
if (__DEV__ && globalState.trackingDerivation) {
|
|
23
|
-
warnAboutProxyRequirement(
|
|
24
|
-
"detect new properties using the 'in' operator. Use 'has' from 'mobx' instead."
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
19
|
return getAdm(target).has_(name)
|
|
28
20
|
},
|
|
29
21
|
get(target: IIsObservableObject, name: PropertyKey): any {
|
|
@@ -33,20 +25,10 @@ const objectProxyTraps: ProxyHandler<any> = {
|
|
|
33
25
|
if (!isStringish(name)) {
|
|
34
26
|
return false
|
|
35
27
|
}
|
|
36
|
-
if (__DEV__ && !getAdm(target).values_.has(name)) {
|
|
37
|
-
warnAboutProxyRequirement(
|
|
38
|
-
"add a new observable property through direct assignment. Use 'set' from 'mobx' instead."
|
|
39
|
-
)
|
|
40
|
-
}
|
|
41
28
|
// null (intercepted) -> true (success)
|
|
42
29
|
return getAdm(target).set_(name, value, true) ?? true
|
|
43
30
|
},
|
|
44
31
|
deleteProperty(target: IIsObservableObject, name: PropertyKey): boolean {
|
|
45
|
-
if (__DEV__) {
|
|
46
|
-
warnAboutProxyRequirement(
|
|
47
|
-
"delete properties from an observable object. Use 'remove' from 'mobx' instead."
|
|
48
|
-
)
|
|
49
|
-
}
|
|
50
32
|
if (!isStringish(name)) {
|
|
51
33
|
return false
|
|
52
34
|
}
|
|
@@ -58,20 +40,10 @@ const objectProxyTraps: ProxyHandler<any> = {
|
|
|
58
40
|
name: PropertyKey,
|
|
59
41
|
descriptor: PropertyDescriptor
|
|
60
42
|
): boolean {
|
|
61
|
-
if (__DEV__) {
|
|
62
|
-
warnAboutProxyRequirement(
|
|
63
|
-
"define property on an observable object. Use 'defineProperty' from 'mobx' instead."
|
|
64
|
-
)
|
|
65
|
-
}
|
|
66
43
|
// null (intercepted) -> true (success)
|
|
67
44
|
return getAdm(target).defineProperty_(name, descriptor) ?? true
|
|
68
45
|
},
|
|
69
46
|
ownKeys(target: IIsObservableObject): ArrayLike<string | symbol> {
|
|
70
|
-
if (__DEV__ && globalState.trackingDerivation) {
|
|
71
|
-
warnAboutProxyRequirement(
|
|
72
|
-
"iterate keys to detect added / removed properties. Use 'keys' from 'mobx' instead."
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
47
|
return getAdm(target).ownKeys_()
|
|
76
48
|
},
|
|
77
49
|
preventExtensions(target) {
|
|
@@ -83,7 +55,6 @@ export function asDynamicObservableObject(
|
|
|
83
55
|
target: any,
|
|
84
56
|
options?: CreateObservableOptions
|
|
85
57
|
): IIsObservableObject {
|
|
86
|
-
assertProxies()
|
|
87
58
|
target = asObservableObject(target, options)
|
|
88
59
|
return (target[$mobx].proxy_ ??= new Proxy(target, objectProxyTraps))
|
|
89
60
|
}
|
|
@@ -17,8 +17,7 @@ export function createFlowAnnotation(name: string, options?: object): Annotation
|
|
|
17
17
|
annotationType_: name,
|
|
18
18
|
options_: options,
|
|
19
19
|
make_,
|
|
20
|
-
extend_
|
|
21
|
-
decorate_20223_
|
|
20
|
+
extend_
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
|
|
@@ -63,7 +62,11 @@ function extend_(
|
|
|
63
62
|
return adm.defineProperty_(key, flowDescriptor, proxyTrap)
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
function
|
|
65
|
+
export function decorateFlow20223_(
|
|
66
|
+
annotation: Annotation,
|
|
67
|
+
mthd,
|
|
68
|
+
context: ClassMethodDecoratorContext
|
|
69
|
+
) {
|
|
67
70
|
if (__DEV__) {
|
|
68
71
|
assert20223DecoratorType(context, ["method"])
|
|
69
72
|
}
|
|
@@ -73,7 +76,7 @@ function decorate_20223_(this: Annotation, mthd, context: ClassMethodDecoratorCo
|
|
|
73
76
|
mthd = flow(mthd)
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
if (
|
|
79
|
+
if (annotation.options_?.bound) {
|
|
77
80
|
addInitializer(function () {
|
|
78
81
|
const self = this as any
|
|
79
82
|
const bound = self[name].bind(self)
|
package/src/types/modifiers.ts
CHANGED
|
@@ -22,6 +22,11 @@ export interface IEnhancer<T> {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export function deepEnhancer(v, _, name) {
|
|
25
|
+
// primitives can never be made observable; skip the type checks below
|
|
26
|
+
if (v === null || (typeof v !== "object" && typeof v !== "function")) {
|
|
27
|
+
return v
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
// it is an observable already, done
|
|
26
31
|
if (isObservable(v)) {
|
|
27
32
|
return v
|
|
@@ -15,8 +15,7 @@ export function createObservableAnnotation(name: string, options?: object): Anno
|
|
|
15
15
|
annotationType_: name,
|
|
16
16
|
options_: options,
|
|
17
17
|
make_,
|
|
18
|
-
extend_
|
|
19
|
-
decorate_20223_
|
|
18
|
+
extend_
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
|
|
@@ -40,13 +39,13 @@ function extend_(
|
|
|
40
39
|
return adm.defineObservableProperty_(
|
|
41
40
|
key,
|
|
42
41
|
descriptor.value,
|
|
43
|
-
this.options_?.
|
|
42
|
+
this.options_?.enhancer_ ?? deepEnhancer,
|
|
44
43
|
proxyTrap
|
|
45
44
|
)
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
function
|
|
49
|
-
|
|
47
|
+
export function decorateObservable20223_(
|
|
48
|
+
annotation: Annotation,
|
|
50
49
|
desc,
|
|
51
50
|
context: ClassAccessorDecoratorContext | ClassFieldDecoratorContext
|
|
52
51
|
) {
|
|
@@ -61,7 +60,7 @@ function decorate_20223_(
|
|
|
61
60
|
assert20223DecoratorType(context, ["accessor"])
|
|
62
61
|
}
|
|
63
62
|
|
|
64
|
-
const ann =
|
|
63
|
+
const ann = annotation
|
|
65
64
|
const { kind, name } = context
|
|
66
65
|
|
|
67
66
|
if (kind !== "accessor") {
|
|
@@ -78,7 +77,7 @@ function decorate_20223_(
|
|
|
78
77
|
() =>
|
|
79
78
|
new ObservableValue(
|
|
80
79
|
value,
|
|
81
|
-
ann.options_?.
|
|
80
|
+
ann.options_?.enhancer_ ?? deepEnhancer,
|
|
82
81
|
__DEV__
|
|
83
82
|
? `${adm.name_}.${name.toString()}`
|
|
84
83
|
: `ObservableObject.${name.toString()}`,
|
|
@@ -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
|