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
package/src/utils/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { globalState
|
|
1
|
+
import { globalState } from "../internal"
|
|
2
2
|
|
|
3
3
|
// We shorten anything used > 5 times
|
|
4
4
|
export const assign = Object.assign
|
|
@@ -17,28 +17,8 @@ export interface Lambda {
|
|
|
17
17
|
name?: string
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const hasProxy = typeof Proxy !== "undefined"
|
|
21
20
|
const plainObjectString = Object.toString()
|
|
22
21
|
|
|
23
|
-
export function assertProxies() {
|
|
24
|
-
if (!hasProxy) {
|
|
25
|
-
die(
|
|
26
|
-
__DEV__
|
|
27
|
-
? "`Proxy` objects are not available in the current environment. Please configure MobX to enable a fallback implementation.`"
|
|
28
|
-
: "Proxy not available"
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function warnAboutProxyRequirement(msg: string) {
|
|
34
|
-
if (__DEV__ && globalState.verifyProxies) {
|
|
35
|
-
die(
|
|
36
|
-
"MobX is currently configured to be able to run in ES5 mode, but in ES5 MobX won't be able to " +
|
|
37
|
-
msg
|
|
38
|
-
)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
22
|
export function getNextId() {
|
|
43
23
|
return ++globalState.mobxGuid
|
|
44
24
|
}
|
|
@@ -90,7 +70,7 @@ export function isPlainObject(value: any) {
|
|
|
90
70
|
if (proto == null) {
|
|
91
71
|
return true
|
|
92
72
|
}
|
|
93
|
-
const protoConstructor =
|
|
73
|
+
const protoConstructor = hasProp(proto, "constructor") && proto.constructor
|
|
94
74
|
return (
|
|
95
75
|
typeof protoConstructor === "function" && protoConstructor.toString() === plainObjectString
|
|
96
76
|
)
|
|
@@ -164,17 +144,11 @@ export function isES6Set(thing: unknown): thing is Set<any> {
|
|
|
164
144
|
return thing != null && Object.prototype.toString.call(thing) === "[object Set]"
|
|
165
145
|
}
|
|
166
146
|
|
|
167
|
-
const hasGetOwnPropertySymbols = typeof Object.getOwnPropertySymbols !== "undefined"
|
|
168
|
-
|
|
169
147
|
/**
|
|
170
148
|
* Returns the following: own enumerable keys and symbols.
|
|
171
149
|
*/
|
|
172
150
|
export function getPlainObjectKeys(object: any) {
|
|
173
151
|
const keys = Object.keys(object)
|
|
174
|
-
// Not supported in IE, so there are not going to be symbol props anyway...
|
|
175
|
-
if (!hasGetOwnPropertySymbols) {
|
|
176
|
-
return keys
|
|
177
|
-
}
|
|
178
152
|
const symbols = Object.getOwnPropertySymbols(object)
|
|
179
153
|
if (!symbols.length) {
|
|
180
154
|
return keys
|
|
@@ -184,12 +158,7 @@ export function getPlainObjectKeys(object: any) {
|
|
|
184
158
|
|
|
185
159
|
// From Immer utils
|
|
186
160
|
// Returns all own keys, including non-enumerable and symbolic
|
|
187
|
-
export const ownKeys: (target: any) => Array<string | symbol> =
|
|
188
|
-
typeof Reflect !== "undefined" && Reflect.ownKeys
|
|
189
|
-
? Reflect.ownKeys
|
|
190
|
-
: hasGetOwnPropertySymbols
|
|
191
|
-
? obj => Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj) as any)
|
|
192
|
-
: /* istanbul ignore next */ Object.getOwnPropertyNames
|
|
161
|
+
export const ownKeys: (target: any) => Array<string | symbol> = Reflect.ownKeys
|
|
193
162
|
|
|
194
163
|
export function stringifyKey(key: any): string {
|
|
195
164
|
if (typeof key === "string") {
|
|
@@ -209,18 +178,7 @@ export function hasProp(target: Object, prop: PropertyKey): boolean {
|
|
|
209
178
|
return objectPrototype.hasOwnProperty.call(target, prop)
|
|
210
179
|
}
|
|
211
180
|
|
|
212
|
-
|
|
213
|
-
export const getOwnPropertyDescriptors =
|
|
214
|
-
Object.getOwnPropertyDescriptors ||
|
|
215
|
-
function getOwnPropertyDescriptors(target: any) {
|
|
216
|
-
// Polyfill needed for Hermes and IE, see https://github.com/facebook/hermes/issues/274
|
|
217
|
-
const res: any = {}
|
|
218
|
-
// Note: without polyfill for ownKeys, symbols won't be picked up
|
|
219
|
-
ownKeys(target).forEach(key => {
|
|
220
|
-
res[key] = getDescriptor(target, key)
|
|
221
|
-
})
|
|
222
|
-
return res
|
|
223
|
-
}
|
|
181
|
+
export const getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors
|
|
224
182
|
|
|
225
183
|
export function getFlag(flags: number, mask: number) {
|
|
226
184
|
return !!(flags & mask)
|
package/dist/api/trace.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { IEnhancer, IObservableArray } from "../internal";
|
|
2
|
-
declare class StubArray {
|
|
3
|
-
}
|
|
4
|
-
export declare class LegacyObservableArray<T> extends StubArray {
|
|
5
|
-
constructor(initialValues: T[] | undefined, enhancer: IEnhancer<T>, name?: string, owned?: boolean);
|
|
6
|
-
concat(...arrays: T[][]): T[];
|
|
7
|
-
get length(): number;
|
|
8
|
-
set length(newLength: number);
|
|
9
|
-
get [Symbol.toStringTag](): string;
|
|
10
|
-
[Symbol.iterator](): IteratorObject<any, unknown, unknown>;
|
|
11
|
-
}
|
|
12
|
-
export declare function reserveArrayBuffer(max: number): void;
|
|
13
|
-
export declare function createLegacyArray<T>(initialValues: T[] | undefined, enhancer: IEnhancer<T>, name?: string): IObservableArray<T>;
|
|
14
|
-
export {};
|
package/dist/utils/global.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function getGlobal(): any;
|
package/src/api/trace.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { TraceMode, die, getAtom, globalState } from "../internal"
|
|
2
|
-
|
|
3
|
-
export function trace(thing?: any, prop?: string, enterBreakPoint?: boolean): void
|
|
4
|
-
export function trace(thing?: any, enterBreakPoint?: boolean): void
|
|
5
|
-
export function trace(enterBreakPoint?: boolean): void
|
|
6
|
-
export function trace(...args: any[]): void {
|
|
7
|
-
if (!__DEV__) {
|
|
8
|
-
return
|
|
9
|
-
}
|
|
10
|
-
let enterBreakPoint = false
|
|
11
|
-
if (typeof args[args.length - 1] === "boolean") {
|
|
12
|
-
enterBreakPoint = args.pop()
|
|
13
|
-
}
|
|
14
|
-
const derivation = getAtomFromArgs(args)
|
|
15
|
-
if (!derivation) {
|
|
16
|
-
return die(
|
|
17
|
-
`'trace(break?)' can only be used inside a tracked computed value or a Reaction. Consider passing in the computed value or reaction explicitly`
|
|
18
|
-
)
|
|
19
|
-
}
|
|
20
|
-
if (derivation.isTracing_ === TraceMode.NONE) {
|
|
21
|
-
console.log(`[mobx.trace] '${derivation.name_}' tracing enabled`)
|
|
22
|
-
}
|
|
23
|
-
derivation.isTracing_ = enterBreakPoint ? TraceMode.BREAK : TraceMode.LOG
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function getAtomFromArgs(args): any {
|
|
27
|
-
switch (args.length) {
|
|
28
|
-
case 0:
|
|
29
|
-
return globalState.trackingDerivation
|
|
30
|
-
case 1:
|
|
31
|
-
return getAtom(args[0])
|
|
32
|
-
case 2:
|
|
33
|
-
return getAtom(args[0], args[1])
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getNextId,
|
|
3
|
-
addHiddenFinalProp,
|
|
4
|
-
makeIterable,
|
|
5
|
-
addHiddenProp,
|
|
6
|
-
ObservableArrayAdministration,
|
|
7
|
-
$mobx,
|
|
8
|
-
arrayExtensions,
|
|
9
|
-
IEnhancer,
|
|
10
|
-
isObservableArray,
|
|
11
|
-
IObservableArray,
|
|
12
|
-
defineProperty,
|
|
13
|
-
initObservable
|
|
14
|
-
} from "../internal"
|
|
15
|
-
|
|
16
|
-
// Bug in safari 9.* (or iOS 9 safari mobile). See #364
|
|
17
|
-
const ENTRY_0 = createArrayEntryDescriptor(0)
|
|
18
|
-
|
|
19
|
-
const safariPrototypeSetterInheritanceBug = (() => {
|
|
20
|
-
let v = false
|
|
21
|
-
const p = {}
|
|
22
|
-
Object.defineProperty(p, "0", {
|
|
23
|
-
set: () => {
|
|
24
|
-
v = true
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
Object.create(p)["0"] = 1
|
|
28
|
-
return v === false
|
|
29
|
-
})()
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* This array buffer contains two lists of properties, so that all arrays
|
|
33
|
-
* can recycle their property definitions, which significantly improves performance of creating
|
|
34
|
-
* properties on the fly.
|
|
35
|
-
*/
|
|
36
|
-
let OBSERVABLE_ARRAY_BUFFER_SIZE = 0
|
|
37
|
-
|
|
38
|
-
// Typescript workaround to make sure ObservableArray extends Array
|
|
39
|
-
class StubArray {}
|
|
40
|
-
function inherit(ctor, proto) {
|
|
41
|
-
if (Object.setPrototypeOf) {
|
|
42
|
-
Object.setPrototypeOf(ctor.prototype, proto)
|
|
43
|
-
} else if (ctor.prototype.__proto__ !== undefined) {
|
|
44
|
-
ctor.prototype.__proto__ = proto
|
|
45
|
-
} else {
|
|
46
|
-
ctor.prototype = proto
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
inherit(StubArray, Array.prototype)
|
|
50
|
-
|
|
51
|
-
// Weex proto freeze protection was here,
|
|
52
|
-
// but it is unclear why the hack is need as MobX never changed the prototype
|
|
53
|
-
// anyway, so removed it in V6
|
|
54
|
-
|
|
55
|
-
export class LegacyObservableArray<T> extends StubArray {
|
|
56
|
-
constructor(
|
|
57
|
-
initialValues: T[] | undefined,
|
|
58
|
-
enhancer: IEnhancer<T>,
|
|
59
|
-
name = __DEV__ ? "ObservableArray@" + getNextId() : "ObservableArray",
|
|
60
|
-
owned = false
|
|
61
|
-
) {
|
|
62
|
-
super()
|
|
63
|
-
initObservable(() => {
|
|
64
|
-
const adm = new ObservableArrayAdministration(name, enhancer, owned, true)
|
|
65
|
-
adm.proxy_ = this as any
|
|
66
|
-
addHiddenFinalProp(this, $mobx, adm)
|
|
67
|
-
|
|
68
|
-
if (initialValues && initialValues.length) {
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
this.spliceWithArray(0, 0, initialValues)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (safariPrototypeSetterInheritanceBug) {
|
|
74
|
-
// Seems that Safari won't use numeric prototype setter until any * numeric property is
|
|
75
|
-
// defined on the instance. After that it works fine, even if this property is deleted.
|
|
76
|
-
Object.defineProperty(this, "0", ENTRY_0)
|
|
77
|
-
}
|
|
78
|
-
})
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
concat(...arrays: T[][]): T[] {
|
|
82
|
-
;(this[$mobx] as ObservableArrayAdministration).atom_.reportObserved()
|
|
83
|
-
return Array.prototype.concat.apply(
|
|
84
|
-
(this as any).slice(),
|
|
85
|
-
//@ts-ignore
|
|
86
|
-
arrays.map(a => (isObservableArray(a) ? a.slice() : a))
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
get length(): number {
|
|
91
|
-
return (this[$mobx] as ObservableArrayAdministration).getArrayLength_()
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
set length(newLength: number) {
|
|
95
|
-
;(this[$mobx] as ObservableArrayAdministration).setArrayLength_(newLength)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
get [Symbol.toStringTag]() {
|
|
99
|
-
return "Array"
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
[Symbol.iterator]() {
|
|
103
|
-
const self = this
|
|
104
|
-
let nextIndex = 0
|
|
105
|
-
return makeIterable({
|
|
106
|
-
next() {
|
|
107
|
-
return nextIndex < self.length
|
|
108
|
-
? { value: self[nextIndex++], done: false }
|
|
109
|
-
: { done: true, value: undefined }
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
Object.entries(arrayExtensions).forEach(([prop, fn]) => {
|
|
116
|
-
if (prop !== "concat") {
|
|
117
|
-
addHiddenProp(LegacyObservableArray.prototype, prop, fn)
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
function createArrayEntryDescriptor(index: number) {
|
|
122
|
-
return {
|
|
123
|
-
enumerable: false,
|
|
124
|
-
configurable: true,
|
|
125
|
-
get: function () {
|
|
126
|
-
return this[$mobx].get_(index)
|
|
127
|
-
},
|
|
128
|
-
set: function (value) {
|
|
129
|
-
this[$mobx].set_(index, value)
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function createArrayBufferItem(index: number) {
|
|
135
|
-
defineProperty(LegacyObservableArray.prototype, "" + index, createArrayEntryDescriptor(index))
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function reserveArrayBuffer(max: number) {
|
|
139
|
-
if (max > OBSERVABLE_ARRAY_BUFFER_SIZE) {
|
|
140
|
-
for (let index = OBSERVABLE_ARRAY_BUFFER_SIZE; index < max + 100; index++) {
|
|
141
|
-
createArrayBufferItem(index)
|
|
142
|
-
}
|
|
143
|
-
OBSERVABLE_ARRAY_BUFFER_SIZE = max
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
reserveArrayBuffer(1000)
|
|
148
|
-
|
|
149
|
-
export function createLegacyArray<T>(
|
|
150
|
-
initialValues: T[] | undefined,
|
|
151
|
-
enhancer: IEnhancer<T>,
|
|
152
|
-
name?: string
|
|
153
|
-
): IObservableArray<T> {
|
|
154
|
-
return new LegacyObservableArray(initialValues, enhancer, name) as any
|
|
155
|
-
}
|
package/src/utils/global.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
declare const window: any
|
|
2
|
-
declare const self: any
|
|
3
|
-
|
|
4
|
-
const mockGlobal = {}
|
|
5
|
-
|
|
6
|
-
export function getGlobal() {
|
|
7
|
-
if (typeof globalThis !== "undefined") {
|
|
8
|
-
return globalThis
|
|
9
|
-
}
|
|
10
|
-
if (typeof window !== "undefined") {
|
|
11
|
-
return window
|
|
12
|
-
}
|
|
13
|
-
if (typeof global !== "undefined") {
|
|
14
|
-
return global
|
|
15
|
-
}
|
|
16
|
-
if (typeof self !== "undefined") {
|
|
17
|
-
return self
|
|
18
|
-
}
|
|
19
|
-
return mockGlobal
|
|
20
|
-
}
|