mobx 6.16.1 → 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 +72 -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 +1630 -2447
- 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 +1619 -2446
- package/dist/mobx.esm.development.js.map +1 -1
- package/dist/mobx.esm.js +1567 -2397
- 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 +1633 -2450
- 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 +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 +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/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
package/src/mobx.ts
CHANGED
|
@@ -16,13 +16,14 @@
|
|
|
16
16
|
*
|
|
17
17
|
*/
|
|
18
18
|
import { die } from "./errors"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
})
|
|
19
|
+
if (__DEV__) {
|
|
20
|
+
const g = globalThis as any
|
|
21
|
+
;["Symbol", "Map", "Set", "Proxy"].forEach(m => {
|
|
22
|
+
if (typeof g[m] === "undefined") {
|
|
23
|
+
die(`MobX requires global '${m}' to be available or polyfilled`)
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
}
|
|
26
27
|
|
|
27
28
|
import { spy, getDebugName, $mobx } from "./internal"
|
|
28
29
|
|
|
@@ -38,7 +39,10 @@ export {
|
|
|
38
39
|
spy,
|
|
39
40
|
IComputedValue,
|
|
40
41
|
IEqualsComparer,
|
|
41
|
-
|
|
42
|
+
compareDefault,
|
|
43
|
+
compareIdentity,
|
|
44
|
+
compareStructural,
|
|
45
|
+
compareShallow,
|
|
42
46
|
IEnhancer,
|
|
43
47
|
IInterceptable,
|
|
44
48
|
IInterceptor,
|
|
@@ -72,9 +76,14 @@ export {
|
|
|
72
76
|
IObservableSetInitialValues,
|
|
73
77
|
transaction,
|
|
74
78
|
observable,
|
|
79
|
+
observableRef,
|
|
80
|
+
observableShallow,
|
|
81
|
+
observableDeep,
|
|
82
|
+
observableStruct,
|
|
75
83
|
IObservableFactory,
|
|
76
84
|
CreateObservableOptions,
|
|
77
85
|
computed,
|
|
86
|
+
computedStruct,
|
|
78
87
|
IComputedFactory,
|
|
79
88
|
isObservable,
|
|
80
89
|
isObservableProp,
|
|
@@ -90,6 +99,7 @@ export {
|
|
|
90
99
|
when,
|
|
91
100
|
IWhenOptions,
|
|
92
101
|
action,
|
|
102
|
+
actionBound,
|
|
93
103
|
isAction,
|
|
94
104
|
runInAction,
|
|
95
105
|
IActionFactory,
|
|
@@ -106,13 +116,13 @@ export {
|
|
|
106
116
|
onBecomeObserved,
|
|
107
117
|
onBecomeUnobserved,
|
|
108
118
|
flow,
|
|
119
|
+
flowBound,
|
|
109
120
|
isFlow,
|
|
110
121
|
flowResult,
|
|
111
122
|
CancellablePromise,
|
|
112
123
|
FlowCancellationError,
|
|
113
124
|
isFlowCancellationError,
|
|
114
125
|
toJS,
|
|
115
|
-
trace,
|
|
116
126
|
IObserverTree,
|
|
117
127
|
IDependencyTree,
|
|
118
128
|
getDependencyTree,
|
|
@@ -138,6 +148,7 @@ export {
|
|
|
138
148
|
makeObservable,
|
|
139
149
|
makeAutoObservable,
|
|
140
150
|
autoAction as _autoAction,
|
|
151
|
+
autoActionBound as _autoActionBound,
|
|
141
152
|
AnnotationsMap,
|
|
142
153
|
AnnotationMapEntry,
|
|
143
154
|
override
|
|
@@ -145,7 +156,7 @@ export {
|
|
|
145
156
|
|
|
146
157
|
// Devtools support
|
|
147
158
|
declare const __MOBX_DEVTOOLS_GLOBAL_HOOK__: { injectMobx: (any) => void }
|
|
148
|
-
if (typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "object") {
|
|
159
|
+
if (__DEV__ && typeof __MOBX_DEVTOOLS_GLOBAL_HOOK__ === "object") {
|
|
149
160
|
// See: https://github.com/andykog/mobx-devtools/
|
|
150
161
|
__MOBX_DEVTOOLS_GLOBAL_HOOK__.injectMobx({
|
|
151
162
|
spy,
|
|
@@ -16,8 +16,7 @@ export function createActionAnnotation(name: string, options?: object): Annotati
|
|
|
16
16
|
annotationType_: name,
|
|
17
17
|
options_: options,
|
|
18
18
|
make_,
|
|
19
|
-
extend_
|
|
20
|
-
decorate_20223_
|
|
19
|
+
extend_
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
|
|
@@ -62,12 +61,12 @@ function extend_(
|
|
|
62
61
|
return adm.defineProperty_(key, actionDescriptor, proxyTrap)
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
function
|
|
64
|
+
export function decorateAction20223_(annotation: Annotation, mthd, context: DecoratorContext) {
|
|
66
65
|
if (__DEV__) {
|
|
67
66
|
assert20223DecoratorType(context, ["method", "field"])
|
|
68
67
|
}
|
|
69
68
|
const { kind, name, addInitializer } = context
|
|
70
|
-
const ann =
|
|
69
|
+
const ann = annotation
|
|
71
70
|
|
|
72
71
|
const _createAction = m =>
|
|
73
72
|
createAction(ann.options_?.name ?? name!.toString(), m, ann.options_?.autoAction ?? false)
|
|
@@ -91,7 +90,7 @@ function decorate_20223_(this: Annotation, mthd, context: DecoratorContext) {
|
|
|
91
90
|
mthd = _createAction(mthd)
|
|
92
91
|
}
|
|
93
92
|
|
|
94
|
-
if (
|
|
93
|
+
if (ann.options_?.bound) {
|
|
95
94
|
addInitializer(function () {
|
|
96
95
|
const self = this as any
|
|
97
96
|
const bound = self[name].bind(self)
|
|
@@ -103,10 +102,7 @@ function decorate_20223_(this: Annotation, mthd, context: DecoratorContext) {
|
|
|
103
102
|
return mthd
|
|
104
103
|
}
|
|
105
104
|
|
|
106
|
-
die(
|
|
107
|
-
`Cannot apply '${ann.annotationType_}' to '${String(name)}' (kind: ${kind}):` +
|
|
108
|
-
`\n'${ann.annotationType_}' can only be used on properties with a function value.`
|
|
109
|
-
)
|
|
105
|
+
die(43, ann.annotationType_, String(name), kind)
|
|
110
106
|
}
|
|
111
107
|
|
|
112
108
|
function assertActionDescriptor(
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ObservableObjectAdministration,
|
|
3
3
|
observable,
|
|
4
|
+
observableRef,
|
|
4
5
|
Annotation,
|
|
5
6
|
defineProperty,
|
|
6
7
|
createAction,
|
|
7
8
|
globalState,
|
|
8
9
|
flow,
|
|
10
|
+
flowBound,
|
|
9
11
|
computed,
|
|
10
12
|
autoAction,
|
|
13
|
+
autoActionBound,
|
|
11
14
|
isGenerator,
|
|
12
15
|
MakeResult,
|
|
13
|
-
die,
|
|
14
16
|
isAction
|
|
15
17
|
} from "../internal"
|
|
16
18
|
|
|
@@ -23,8 +25,7 @@ export function createAutoAnnotation(options?: object): Annotation {
|
|
|
23
25
|
annotationType_: AUTO,
|
|
24
26
|
options_: options,
|
|
25
27
|
make_,
|
|
26
|
-
extend_
|
|
27
|
-
decorate_20223_
|
|
28
|
+
extend_
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
|
|
@@ -63,16 +64,16 @@ function make_(
|
|
|
63
64
|
// function on proto -> autoAction/flow
|
|
64
65
|
if (source !== adm.target_ && typeof descriptor.value === "function") {
|
|
65
66
|
if (isGenerator(descriptor.value)) {
|
|
66
|
-
const flowAnnotation = this.options_?.autoBind ?
|
|
67
|
+
const flowAnnotation = this.options_?.autoBind ? flowBound : flow
|
|
67
68
|
return flowAnnotation.make_(adm, key, descriptor, source)
|
|
68
69
|
}
|
|
69
|
-
const actionAnnotation = this.options_?.autoBind ?
|
|
70
|
+
const actionAnnotation = this.options_?.autoBind ? autoActionBound : autoAction
|
|
70
71
|
return actionAnnotation.make_(adm, key, descriptor, source)
|
|
71
72
|
}
|
|
72
73
|
// other -> observable
|
|
73
74
|
// Copy props from proto as well, see test:
|
|
74
75
|
// "decorate should work with Object.create"
|
|
75
|
-
let observableAnnotation = this.options_?.deep === false ?
|
|
76
|
+
let observableAnnotation = this.options_?.deep === false ? observableRef : observable
|
|
76
77
|
// if function respect autoBind option
|
|
77
78
|
if (typeof descriptor.value === "function" && this.options_?.autoBind) {
|
|
78
79
|
descriptor.value = descriptor.value.bind(adm.proxy_ ?? adm.target_)
|
|
@@ -107,10 +108,6 @@ function extend_(
|
|
|
107
108
|
if (typeof descriptor.value === "function" && this.options_?.autoBind) {
|
|
108
109
|
descriptor.value = descriptor.value.bind(adm.proxy_ ?? adm.target_)
|
|
109
110
|
}
|
|
110
|
-
let observableAnnotation = this.options_?.deep === false ?
|
|
111
|
+
let observableAnnotation = this.options_?.deep === false ? observableRef : observable
|
|
111
112
|
return observableAnnotation.extend_(adm, key, descriptor, proxyTrap)
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
-
function decorate_20223_(this: Annotation, desc, context: ClassGetterDecoratorContext) {
|
|
115
|
-
die(`'${this.annotationType_}' cannot be used as a decorator`)
|
|
116
|
-
}
|
|
@@ -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)
|
|
@@ -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
|