mobx-state-tree 5.0.0 → 5.0.4
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/README.md +69 -4
- package/dist/mobx-state-tree.js +100 -84
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +101 -85
- package/dist/mobx-state-tree.umd.js +108 -92
- package/dist/mobx-state-tree.umd.min.js +2 -2
- package/dist/types/complex-types/array.d.ts +1 -1
- package/dist/types/complex-types/map.d.ts +1 -12
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -2,14 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
# mobx-state-tree
|
|
4
4
|
|
|
5
|
-
_Opinionated, transactional, MobX powered state container combining the best features of the immutable and mutable world for an optimal DX_
|
|
6
|
-
|
|
7
5
|
[](https://badge.fury.io/js/mobx-state-tree)
|
|
8
6
|
[](https://circleci.com/gh/mobxjs/mobx-state-tree)
|
|
9
7
|
[](https://coveralls.io/github/mobxjs/mobx-state-tree?branch=master)
|
|
10
|
-
[](https://github.com/mobxjs/mobx-state-tree/discussions)
|
|
9
|
+
|
|
10
|
+
## What is mobx-state-tree?
|
|
11
|
+
|
|
12
|
+
Technically speaking, mobx-state-tree (also known as MST) is a state container system built on [MobX](https://github.com/mobxjs/mobx), a functional reactive state library.
|
|
13
|
+
|
|
14
|
+
This may not mean much to you, and that’s okay. I’ll explain it like this: **MobX is a state management "engine", and MobX-State-Tree gives it structure and common tools you need for your app.** MST is valuable in a large team but also useful in smaller applications when you expect your code to scale rapidly. And if we compare it to Redux, MST offers better performance and much less boilerplate code than Redux!
|
|
11
15
|
|
|
12
|
-
|
|
16
|
+
MobX is [one of the most popular Redux alternatives](https://2019.stateofjs.com/data-layer/mobx/) and is used (along with MobX-State-Tree) by companies worldwide. MST plays very well with TypeScript, React, and React Native, especially when paired with [mobx-react-lite](https://github.com/mobxjs/mobx/tree/main/packages/mobx-react-lite). It supports multiple stores, async actions and side effects, enables extremely targeted re-renders for React apps, and much more -- all in a package with _zero dependencies_ other than MobX itself.
|
|
17
|
+
|
|
18
|
+
_Note: you don't need to know how to use MobX in order to use MST._
|
|
13
19
|
|
|
14
20
|
# Getting started
|
|
15
21
|
|
|
@@ -17,8 +23,67 @@ See the [Getting started](https://mobx-state-tree.js.org/intro/getting-started)
|
|
|
17
23
|
|
|
18
24
|
👉 Official docs can be found at [http://mobx-state-tree.js.org/](http://mobx-state-tree.js.org/)
|
|
19
25
|
|
|
26
|
+
## Quick Code Example
|
|
27
|
+
|
|
28
|
+
There's nothing quite like looking at some code to get a feel for a library. Check out this small example of an author and list of tweets by that author.
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { types } from "mobx-state-tree"
|
|
32
|
+
|
|
33
|
+
// Define a couple models
|
|
34
|
+
const Author = types.model({
|
|
35
|
+
id: types.identifier,
|
|
36
|
+
firstName: types.string,
|
|
37
|
+
lastName: types.string
|
|
38
|
+
})
|
|
39
|
+
const Tweet = types.model({
|
|
40
|
+
id: types.identifier,
|
|
41
|
+
author: types.reference(Author), // stores just the `id` reference!
|
|
42
|
+
body: types.string,
|
|
43
|
+
timestamp: types.number
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// Define a store just like a model
|
|
47
|
+
const RootStore = types.model({
|
|
48
|
+
authors: types.array(Author),
|
|
49
|
+
tweets: types.array(Tweet)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
// Instantiate a couple model instances
|
|
53
|
+
const jamon = Author.create({
|
|
54
|
+
id: "jamon",
|
|
55
|
+
firstName: "Jamon",
|
|
56
|
+
lastName: "Holmgren"
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const tweet = Tweet.create({
|
|
60
|
+
id: "1",
|
|
61
|
+
author: jamon.id, // just the ID needed here
|
|
62
|
+
body: "Hello world!",
|
|
63
|
+
timestamp: Date.now()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
// Now instantiate the store!
|
|
67
|
+
const rootStore = RootStore.create({
|
|
68
|
+
authors: [jamon],
|
|
69
|
+
tweets: [tweet]
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// Ready to use in a React component, if that's your target.
|
|
73
|
+
import { observer } from "mobx-react-lite"
|
|
74
|
+
const MyComponent = observer((props) => {
|
|
75
|
+
return <div>Hello, {rootStore.authors[0].firstName}!</div>
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
// Note: since this component is "observed", any changes to rootStore.authors[0].firstName
|
|
79
|
+
// will result in a re-render! If you're not using React, you can also "listen" to changes
|
|
80
|
+
// using `onSnapshot`: https://mobx-state-tree.js.org/concepts/snapshots
|
|
81
|
+
```
|
|
82
|
+
|
|
20
83
|
## Thanks!
|
|
21
84
|
|
|
85
|
+
- [Michel Weststrate](https://twitter.com/mweststrate) for creating MobX, MobX-State-Tree, and MobX-React.
|
|
86
|
+
- [Infinite Red](https://infinite.red) for supporting ongoing maintenance on MST.
|
|
22
87
|
- [Mendix](https://mendix.com) for sponsoring and providing the opportunity to work on exploratory projects like MST.
|
|
23
88
|
- [Dan Abramov](https://twitter.com/dan_abramov)'s work on [Redux](http://redux.js.org) has strongly influenced the idea of snapshots and transactional actions in MST.
|
|
24
89
|
- [Giulio Canti](https://twitter.com/GiulioCanti)'s work on [tcomb](http://github.com/gcanti/tcomb) and type systems in general has strongly influenced the type system of MST.
|
package/dist/mobx-state-tree.js
CHANGED
|
@@ -68,11 +68,13 @@ PERFORMANCE OF THIS SOFTWARE.
|
|
|
68
68
|
var extendStatics = function(d, b) {
|
|
69
69
|
extendStatics = Object.setPrototypeOf ||
|
|
70
70
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
71
|
-
function (d, b) { for (var p in b) if (
|
|
71
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
72
72
|
return extendStatics(d, b);
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
function __extends(d, b) {
|
|
76
|
+
if (typeof b !== "function" && b !== null)
|
|
77
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
76
78
|
extendStatics(d, b);
|
|
77
79
|
function __() { this.constructor = d; }
|
|
78
80
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
@@ -101,13 +103,6 @@ function __rest(s, e) {
|
|
|
101
103
|
return t;
|
|
102
104
|
}
|
|
103
105
|
|
|
104
|
-
function __decorate(decorators, target, key, desc) {
|
|
105
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
106
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
107
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
108
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
106
|
function __generator(thisArg, body) {
|
|
112
107
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
113
108
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
@@ -165,6 +160,7 @@ function __read(o, n) {
|
|
|
165
160
|
return ar;
|
|
166
161
|
}
|
|
167
162
|
|
|
163
|
+
/** @deprecated */
|
|
168
164
|
function __spread() {
|
|
169
165
|
for (var ar = [], i = 0; i < arguments.length; i++)
|
|
170
166
|
ar = ar.concat(__read(arguments[i]));
|
|
@@ -1134,6 +1130,14 @@ var BaseNode = /** @class */ (function () {
|
|
|
1134
1130
|
enumerable: false,
|
|
1135
1131
|
configurable: true
|
|
1136
1132
|
});
|
|
1133
|
+
Object.defineProperty(BaseNode.prototype, "getReconciliationType", {
|
|
1134
|
+
enumerable: false,
|
|
1135
|
+
configurable: true,
|
|
1136
|
+
writable: true,
|
|
1137
|
+
value: function () {
|
|
1138
|
+
return this.type;
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1137
1141
|
Object.defineProperty(BaseNode.prototype, "baseSetParent", {
|
|
1138
1142
|
enumerable: false,
|
|
1139
1143
|
configurable: true,
|
|
@@ -1269,7 +1273,6 @@ var ScalarNode = /** @class */ (function (_super) {
|
|
|
1269
1273
|
__extends(ScalarNode, _super);
|
|
1270
1274
|
function ScalarNode(simpleType, parent, subpath, environment, initialSnapshot) {
|
|
1271
1275
|
var _this = _super.call(this, simpleType, parent, subpath, environment) || this;
|
|
1272
|
-
mobx.makeObservable(_this);
|
|
1273
1276
|
try {
|
|
1274
1277
|
_this.storedValue = simpleType.createNewInstance(initialSnapshot);
|
|
1275
1278
|
}
|
|
@@ -1390,11 +1393,9 @@ var ScalarNode = /** @class */ (function (_super) {
|
|
|
1390
1393
|
this.fireInternalHook(name);
|
|
1391
1394
|
}
|
|
1392
1395
|
});
|
|
1393
|
-
__decorate([
|
|
1394
|
-
mobx.action
|
|
1395
|
-
], ScalarNode.prototype, "die", null);
|
|
1396
1396
|
return ScalarNode;
|
|
1397
1397
|
}(BaseNode));
|
|
1398
|
+
ScalarNode.prototype.die = mobx.action(ScalarNode.prototype.die);
|
|
1398
1399
|
|
|
1399
1400
|
var nextNodeId = 1;
|
|
1400
1401
|
var snapshotReactionOptions = {
|
|
@@ -1512,6 +1513,12 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1512
1513
|
writable: true,
|
|
1513
1514
|
value: false
|
|
1514
1515
|
});
|
|
1516
|
+
Object.defineProperty(_this, "_snapshotComputed", {
|
|
1517
|
+
enumerable: true,
|
|
1518
|
+
configurable: true,
|
|
1519
|
+
writable: true,
|
|
1520
|
+
value: void 0
|
|
1521
|
+
});
|
|
1515
1522
|
Object.defineProperty(_this, "_snapshotUponDeath", {
|
|
1516
1523
|
enumerable: true,
|
|
1517
1524
|
configurable: true,
|
|
@@ -1525,7 +1532,7 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1525
1532
|
writable: true,
|
|
1526
1533
|
value: void 0
|
|
1527
1534
|
});
|
|
1528
|
-
mobx.
|
|
1535
|
+
_this._snapshotComputed = mobx.computed(function () { return freeze(_this.getSnapshot()); });
|
|
1529
1536
|
_this.unbox = _this.unbox.bind(_this);
|
|
1530
1537
|
_this._initialSnapshot = freeze(initialValue);
|
|
1531
1538
|
_this.identifierAttribute = complexType.identifierAttribute;
|
|
@@ -1645,9 +1652,7 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1645
1652
|
this._isRunningAction = false;
|
|
1646
1653
|
}
|
|
1647
1654
|
this._observableInstanceState = 2 /* CREATED */;
|
|
1648
|
-
|
|
1649
|
-
// "_observableInstanceState" field was touched
|
|
1650
|
-
invalidateComputed(this, "snapshot");
|
|
1655
|
+
this._snapshotComputed.trackAndCompute();
|
|
1651
1656
|
if (this.isRoot)
|
|
1652
1657
|
this._addSnapshotReaction();
|
|
1653
1658
|
this._childNodes = EMPTY_OBJECT;
|
|
@@ -1759,7 +1764,7 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1759
1764
|
Object.defineProperty(ObjectNode.prototype, "snapshot", {
|
|
1760
1765
|
// advantage of using computed for a snapshot is that nicely respects transactions etc.
|
|
1761
1766
|
get: function () {
|
|
1762
|
-
return
|
|
1767
|
+
return this._snapshotComputed.get();
|
|
1763
1768
|
},
|
|
1764
1769
|
enumerable: false,
|
|
1765
1770
|
configurable: true
|
|
@@ -2255,21 +2260,13 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
2255
2260
|
}
|
|
2256
2261
|
}
|
|
2257
2262
|
});
|
|
2258
|
-
__decorate([
|
|
2259
|
-
mobx.action
|
|
2260
|
-
], ObjectNode.prototype, "createObservableInstance", null);
|
|
2261
|
-
__decorate([
|
|
2262
|
-
mobx.computed
|
|
2263
|
-
], ObjectNode.prototype, "snapshot", null);
|
|
2264
|
-
__decorate([
|
|
2265
|
-
mobx.action
|
|
2266
|
-
], ObjectNode.prototype, "detach", null);
|
|
2267
|
-
__decorate([
|
|
2268
|
-
mobx.action
|
|
2269
|
-
], ObjectNode.prototype, "die", null);
|
|
2270
2263
|
return ObjectNode;
|
|
2271
2264
|
}(BaseNode));
|
|
2265
|
+
ObjectNode.prototype.createObservableInstance = mobx.action(ObjectNode.prototype.createObservableInstance);
|
|
2266
|
+
ObjectNode.prototype.detach = mobx.action(ObjectNode.prototype.detach);
|
|
2267
|
+
ObjectNode.prototype.die = mobx.action(ObjectNode.prototype.die);
|
|
2272
2268
|
|
|
2269
|
+
var _a;
|
|
2273
2270
|
/**
|
|
2274
2271
|
* @internal
|
|
2275
2272
|
* @hidden
|
|
@@ -2355,7 +2352,6 @@ var BaseType = /** @class */ (function () {
|
|
|
2355
2352
|
writable: true,
|
|
2356
2353
|
value: void 0
|
|
2357
2354
|
});
|
|
2358
|
-
mobx.makeObservable(this);
|
|
2359
2355
|
this.name = name;
|
|
2360
2356
|
}
|
|
2361
2357
|
Object.defineProperty(BaseType.prototype, "create", {
|
|
@@ -2440,13 +2436,10 @@ var BaseType = /** @class */ (function () {
|
|
|
2440
2436
|
enumerable: false,
|
|
2441
2437
|
configurable: true
|
|
2442
2438
|
});
|
|
2443
|
-
var _a;
|
|
2444
|
-
_a = $type;
|
|
2445
|
-
__decorate([
|
|
2446
|
-
mobx.action
|
|
2447
|
-
], BaseType.prototype, "create", null);
|
|
2448
2439
|
return BaseType;
|
|
2449
2440
|
}());
|
|
2441
|
+
_a = $type;
|
|
2442
|
+
BaseType.prototype.create = mobx.action(BaseType.prototype.create);
|
|
2450
2443
|
/**
|
|
2451
2444
|
* A complex type produces a MST node (Node in the state tree)
|
|
2452
2445
|
*
|
|
@@ -2483,6 +2476,16 @@ var ComplexType = /** @class */ (function (_super) {
|
|
|
2483
2476
|
return node.storedValue;
|
|
2484
2477
|
}
|
|
2485
2478
|
});
|
|
2479
|
+
Object.defineProperty(ComplexType.prototype, "isMatchingSnapshotId", {
|
|
2480
|
+
enumerable: false,
|
|
2481
|
+
configurable: true,
|
|
2482
|
+
writable: true,
|
|
2483
|
+
value: function (current, snapshot) {
|
|
2484
|
+
return (!current.identifierAttribute ||
|
|
2485
|
+
current.identifier ===
|
|
2486
|
+
normalizeIdentifier(snapshot[current.identifierAttribute]));
|
|
2487
|
+
}
|
|
2488
|
+
});
|
|
2486
2489
|
Object.defineProperty(ComplexType.prototype, "tryToReconcileNode", {
|
|
2487
2490
|
enumerable: false,
|
|
2488
2491
|
configurable: true,
|
|
@@ -2501,9 +2504,7 @@ var ComplexType = /** @class */ (function (_super) {
|
|
|
2501
2504
|
if (current.type === this &&
|
|
2502
2505
|
isMutable(newValue) &&
|
|
2503
2506
|
!isStateTreeNode(newValue) &&
|
|
2504
|
-
(
|
|
2505
|
-
current.identifier ===
|
|
2506
|
-
normalizeIdentifier(newValue[current.identifierAttribute]))) {
|
|
2507
|
+
this.isMatchingSnapshotId(current, newValue)) {
|
|
2507
2508
|
// the newValue has no node, so can be treated like a snapshot
|
|
2508
2509
|
// we can reconcile
|
|
2509
2510
|
current.applySnapshot(newValue);
|
|
@@ -2543,11 +2544,9 @@ var ComplexType = /** @class */ (function (_super) {
|
|
|
2543
2544
|
return null;
|
|
2544
2545
|
}
|
|
2545
2546
|
});
|
|
2546
|
-
__decorate([
|
|
2547
|
-
mobx.action
|
|
2548
|
-
], ComplexType.prototype, "create", null);
|
|
2549
2547
|
return ComplexType;
|
|
2550
2548
|
}(BaseType));
|
|
2549
|
+
ComplexType.prototype.create = mobx.action(ComplexType.prototype.create);
|
|
2551
2550
|
/**
|
|
2552
2551
|
* @internal
|
|
2553
2552
|
* @hidden
|
|
@@ -3976,12 +3975,21 @@ function deepFreeze(value) {
|
|
|
3976
3975
|
function isSerializable(value) {
|
|
3977
3976
|
return typeof value !== "function";
|
|
3978
3977
|
}
|
|
3978
|
+
/**
|
|
3979
|
+
* @internal
|
|
3980
|
+
* @hidden
|
|
3981
|
+
*/
|
|
3982
|
+
function defineProperty(object, key, descriptor) {
|
|
3983
|
+
mobx.isObservableObject(object)
|
|
3984
|
+
? mobx.defineProperty(object, key, descriptor)
|
|
3985
|
+
: Object.defineProperty(object, key, descriptor);
|
|
3986
|
+
}
|
|
3979
3987
|
/**
|
|
3980
3988
|
* @internal
|
|
3981
3989
|
* @hidden
|
|
3982
3990
|
*/
|
|
3983
3991
|
function addHiddenFinalProp(object, propName, value) {
|
|
3984
|
-
|
|
3992
|
+
defineProperty(object, propName, {
|
|
3985
3993
|
enumerable: false,
|
|
3986
3994
|
writable: false,
|
|
3987
3995
|
configurable: true,
|
|
@@ -3993,7 +4001,7 @@ function addHiddenFinalProp(object, propName, value) {
|
|
|
3993
4001
|
* @hidden
|
|
3994
4002
|
*/
|
|
3995
4003
|
function addHiddenWritableProp(object, propName, value) {
|
|
3996
|
-
|
|
4004
|
+
defineProperty(object, propName, {
|
|
3997
4005
|
enumerable: false,
|
|
3998
4006
|
writable: true,
|
|
3999
4007
|
configurable: true,
|
|
@@ -4185,14 +4193,6 @@ function argsToArray(args) {
|
|
|
4185
4193
|
res[i] = args[i];
|
|
4186
4194
|
return res;
|
|
4187
4195
|
}
|
|
4188
|
-
/**
|
|
4189
|
-
* @internal
|
|
4190
|
-
* @hidden
|
|
4191
|
-
*/
|
|
4192
|
-
function invalidateComputed(target, propName) {
|
|
4193
|
-
var atom = mobx.getAtom(target, propName);
|
|
4194
|
-
atom.trackAndCompute();
|
|
4195
|
-
}
|
|
4196
4196
|
/**
|
|
4197
4197
|
* @internal
|
|
4198
4198
|
* @hidden
|
|
@@ -4594,6 +4594,8 @@ function splitJsonPath(path) {
|
|
|
4594
4594
|
return parts;
|
|
4595
4595
|
}
|
|
4596
4596
|
|
|
4597
|
+
/** @hidden */
|
|
4598
|
+
var $preProcessorFailed = Symbol("$preProcessorFailed");
|
|
4597
4599
|
var SnapshotProcessor = /** @class */ (function (_super) {
|
|
4598
4600
|
__extends(SnapshotProcessor, _super);
|
|
4599
4601
|
function SnapshotProcessor(_subtype, _processors, name) {
|
|
@@ -4638,6 +4640,19 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4638
4640
|
return sn;
|
|
4639
4641
|
}
|
|
4640
4642
|
});
|
|
4643
|
+
Object.defineProperty(SnapshotProcessor.prototype, "preProcessSnapshotSafe", {
|
|
4644
|
+
enumerable: false,
|
|
4645
|
+
configurable: true,
|
|
4646
|
+
writable: true,
|
|
4647
|
+
value: function (sn) {
|
|
4648
|
+
try {
|
|
4649
|
+
return this.preProcessSnapshot(sn);
|
|
4650
|
+
}
|
|
4651
|
+
catch (e) {
|
|
4652
|
+
return $preProcessorFailed;
|
|
4653
|
+
}
|
|
4654
|
+
}
|
|
4655
|
+
});
|
|
4641
4656
|
Object.defineProperty(SnapshotProcessor.prototype, "postProcessSnapshot", {
|
|
4642
4657
|
enumerable: false,
|
|
4643
4658
|
configurable: true,
|
|
@@ -4656,11 +4671,14 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4656
4671
|
value: function (node) {
|
|
4657
4672
|
var _this = this;
|
|
4658
4673
|
// the node has to use these methods rather than the original type ones
|
|
4659
|
-
proxyNodeTypeMethods(node.type, this, "create");
|
|
4674
|
+
proxyNodeTypeMethods(node.type, this, "create", "is", "isMatchingSnapshotId");
|
|
4660
4675
|
var oldGetSnapshot = node.getSnapshot;
|
|
4661
4676
|
node.getSnapshot = function () {
|
|
4662
4677
|
return _this.postProcessSnapshot(oldGetSnapshot.call(node));
|
|
4663
4678
|
};
|
|
4679
|
+
node.getReconciliationType = function () {
|
|
4680
|
+
return _this;
|
|
4681
|
+
};
|
|
4664
4682
|
}
|
|
4665
4683
|
});
|
|
4666
4684
|
Object.defineProperty(SnapshotProcessor.prototype, "instantiate", {
|
|
@@ -4703,7 +4721,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4703
4721
|
configurable: true,
|
|
4704
4722
|
writable: true,
|
|
4705
4723
|
value: function (value, context) {
|
|
4706
|
-
var processedSn = this.
|
|
4724
|
+
var processedSn = this.preProcessSnapshotSafe(value);
|
|
4725
|
+
if (processedSn === $preProcessorFailed) {
|
|
4726
|
+
return typeCheckFailure(context, value, "Failed to preprocess value");
|
|
4727
|
+
}
|
|
4707
4728
|
return this._subtype.validate(processedSn, context);
|
|
4708
4729
|
}
|
|
4709
4730
|
});
|
|
@@ -4724,7 +4745,10 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4724
4745
|
? this._subtype
|
|
4725
4746
|
: isStateTreeNode(thing)
|
|
4726
4747
|
? getSnapshot(thing, false)
|
|
4727
|
-
: this.
|
|
4748
|
+
: this.preProcessSnapshotSafe(thing);
|
|
4749
|
+
if (value === $preProcessorFailed) {
|
|
4750
|
+
return false;
|
|
4751
|
+
}
|
|
4728
4752
|
return this._subtype.validate(value, [{ path: "", type: this._subtype }]).length === 0;
|
|
4729
4753
|
}
|
|
4730
4754
|
});
|
|
@@ -4736,6 +4760,18 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4736
4760
|
return this._subtype.isAssignableFrom(type);
|
|
4737
4761
|
}
|
|
4738
4762
|
});
|
|
4763
|
+
Object.defineProperty(SnapshotProcessor.prototype, "isMatchingSnapshotId", {
|
|
4764
|
+
enumerable: false,
|
|
4765
|
+
configurable: true,
|
|
4766
|
+
writable: true,
|
|
4767
|
+
value: function (current, snapshot) {
|
|
4768
|
+
if (!(this._subtype instanceof ComplexType)) {
|
|
4769
|
+
return false;
|
|
4770
|
+
}
|
|
4771
|
+
var processedSn = this.preProcessSnapshot(snapshot);
|
|
4772
|
+
return ComplexType.prototype.isMatchingSnapshotId.call(this._subtype, current, processedSn);
|
|
4773
|
+
}
|
|
4774
|
+
});
|
|
4739
4775
|
return SnapshotProcessor;
|
|
4740
4776
|
}(BaseType));
|
|
4741
4777
|
function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
|
|
@@ -4967,7 +5003,6 @@ var MapType = /** @class */ (function (_super) {
|
|
|
4967
5003
|
});
|
|
4968
5004
|
_this._determineIdentifierMode();
|
|
4969
5005
|
_this.hookInitializers = hookInitializers;
|
|
4970
|
-
mobx.makeObservable(_this);
|
|
4971
5006
|
return _this;
|
|
4972
5007
|
}
|
|
4973
5008
|
Object.defineProperty(MapType.prototype, "hooks", {
|
|
@@ -5268,16 +5303,14 @@ var MapType = /** @class */ (function (_super) {
|
|
|
5268
5303
|
node.storedValue.delete(subpath);
|
|
5269
5304
|
}
|
|
5270
5305
|
});
|
|
5271
|
-
__decorate([
|
|
5272
|
-
mobx.action
|
|
5273
|
-
], MapType.prototype, "applySnapshot", null);
|
|
5274
5306
|
return MapType;
|
|
5275
5307
|
}(ComplexType));
|
|
5308
|
+
MapType.prototype.applySnapshot = mobx.action(MapType.prototype.applySnapshot);
|
|
5276
5309
|
/**
|
|
5277
5310
|
* `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
|
|
5278
5311
|
* If the type stored in a map has an identifier, it is mandatory to store the child under that identifier in the map.
|
|
5279
5312
|
*
|
|
5280
|
-
* This type will always produce [observable maps](https://mobx.js.org/
|
|
5313
|
+
* This type will always produce [observable maps](https://mobx.js.org/api.html#observablemap)
|
|
5281
5314
|
*
|
|
5282
5315
|
* Example:
|
|
5283
5316
|
* ```ts
|
|
@@ -5340,7 +5373,6 @@ var ArrayType = /** @class */ (function (_super) {
|
|
|
5340
5373
|
writable: true,
|
|
5341
5374
|
value: []
|
|
5342
5375
|
});
|
|
5343
|
-
mobx.makeObservable(_this);
|
|
5344
5376
|
_this.hookInitializers = hookInitializers;
|
|
5345
5377
|
return _this;
|
|
5346
5378
|
}
|
|
@@ -5589,15 +5621,13 @@ var ArrayType = /** @class */ (function (_super) {
|
|
|
5589
5621
|
node.storedValue.splice(Number(subpath), 1);
|
|
5590
5622
|
}
|
|
5591
5623
|
});
|
|
5592
|
-
__decorate([
|
|
5593
|
-
mobx.action
|
|
5594
|
-
], ArrayType.prototype, "applySnapshot", null);
|
|
5595
5624
|
return ArrayType;
|
|
5596
5625
|
}(ComplexType));
|
|
5626
|
+
ArrayType.prototype.applySnapshot = mobx.action(ArrayType.prototype.applySnapshot);
|
|
5597
5627
|
/**
|
|
5598
5628
|
* `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
|
|
5599
5629
|
*
|
|
5600
|
-
* This type will always produce [observable arrays](https://mobx.js.org/
|
|
5630
|
+
* This type will always produce [observable arrays](https://mobx.js.org/api.html#observablearray)
|
|
5601
5631
|
*
|
|
5602
5632
|
* Example:
|
|
5603
5633
|
* ```ts
|
|
@@ -5738,8 +5768,8 @@ function areSame(oldNode, newValue) {
|
|
|
5738
5768
|
oldNode.identifier !== null &&
|
|
5739
5769
|
oldNode.identifierAttribute &&
|
|
5740
5770
|
isPlainObject(newValue) &&
|
|
5741
|
-
oldNode.
|
|
5742
|
-
oldNode.type.
|
|
5771
|
+
oldNode.type.is(newValue) &&
|
|
5772
|
+
oldNode.type.isMatchingSnapshotId(oldNode, newValue));
|
|
5743
5773
|
}
|
|
5744
5774
|
/**
|
|
5745
5775
|
* Returns if a given value represents an array type.
|
|
@@ -5902,7 +5932,6 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
5902
5932
|
});
|
|
5903
5933
|
}
|
|
5904
5934
|
});
|
|
5905
|
-
mobx.makeObservable(_this);
|
|
5906
5935
|
Object.assign(_this, defaultObjectOptions, opts);
|
|
5907
5936
|
// ensures that any default value gets converted to its related type
|
|
5908
5937
|
_this.properties = toPropertiesObject(_this.properties);
|
|
@@ -6065,19 +6094,8 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6065
6094
|
// is this a computed property?
|
|
6066
6095
|
var descriptor = Object.getOwnPropertyDescriptor(views, key);
|
|
6067
6096
|
if ("get" in descriptor) {
|
|
6068
|
-
|
|
6069
|
-
|
|
6070
|
-
// TODO: mobx currently does not allow redefining computes yet, pending #1121
|
|
6071
|
-
// FIXME: this binds to the internals of mobx!
|
|
6072
|
-
computedValue.derivation = descriptor.get;
|
|
6073
|
-
computedValue.scope = self;
|
|
6074
|
-
if (descriptor.set)
|
|
6075
|
-
computedValue.setter = mobx.action(computedValue.name + "-setter", descriptor.set);
|
|
6076
|
-
}
|
|
6077
|
-
else {
|
|
6078
|
-
Object.defineProperty(self, key, descriptor);
|
|
6079
|
-
mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
|
|
6080
|
-
}
|
|
6097
|
+
mobx.defineProperty(self, key, descriptor);
|
|
6098
|
+
mobx.makeObservable(self, (_a = {}, _a[key] = mobx.computed, _a));
|
|
6081
6099
|
}
|
|
6082
6100
|
else if (typeof descriptor.value === "function") {
|
|
6083
6101
|
(!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, key, descriptor.value);
|
|
@@ -6341,11 +6359,9 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6341
6359
|
node.storedValue[subpath] = undefined;
|
|
6342
6360
|
}
|
|
6343
6361
|
});
|
|
6344
|
-
__decorate([
|
|
6345
|
-
mobx.action
|
|
6346
|
-
], ModelType.prototype, "applySnapshot", null);
|
|
6347
6362
|
return ModelType;
|
|
6348
6363
|
}(ComplexType));
|
|
6364
|
+
ModelType.prototype.applySnapshot = mobx.action(ModelType.prototype.applySnapshot);
|
|
6349
6365
|
/**
|
|
6350
6366
|
* `types.model` - Creates a new model type by providing a name, properties, volatile state and actions.
|
|
6351
6367
|
*
|
|
@@ -6907,7 +6923,7 @@ var Union = /** @class */ (function (_super) {
|
|
|
6907
6923
|
configurable: true,
|
|
6908
6924
|
writable: true,
|
|
6909
6925
|
value: function (current, newValue, parent, subpath) {
|
|
6910
|
-
var type = this.determineType(newValue, current.
|
|
6926
|
+
var type = this.determineType(newValue, current.getReconciliationType());
|
|
6911
6927
|
if (!type)
|
|
6912
6928
|
throw fail$1("No matching type for union " + this.describe()); // can happen in prod builds
|
|
6913
6929
|
return type.reconcile(current, newValue, parent, subpath);
|