mobx-state-tree 5.3.1-alpha.1 → 5.4.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/README.md +26 -26
- package/dist/index.d.ts +2 -1
- package/dist/mobx-state-tree.js +28 -8
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +28 -9
- package/dist/mobx-state-tree.umd.js +28 -8
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/dist/types/utility-types/snapshotProcessor.d.ts +9 -7
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -28,51 +28,51 @@ See the [Getting started](https://mobx-state-tree.js.org/intro/getting-started)
|
|
|
28
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
29
|
|
|
30
30
|
```js
|
|
31
|
-
import { types } from "mobx-state-tree"
|
|
31
|
+
import { types } from "mobx-state-tree" // alternatively: import { t } from "mobx-state-tree"
|
|
32
32
|
|
|
33
33
|
// Define a couple models
|
|
34
34
|
const Author = types.model({
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
id: types.identifier,
|
|
36
|
+
firstName: types.string,
|
|
37
|
+
lastName: types.string
|
|
38
38
|
})
|
|
39
39
|
const Tweet = types.model({
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
id: types.identifier,
|
|
41
|
+
author: types.reference(Author), // stores just the `id` reference!
|
|
42
|
+
body: types.string,
|
|
43
|
+
timestamp: types.number
|
|
44
44
|
})
|
|
45
45
|
|
|
46
46
|
// Define a store just like a model
|
|
47
47
|
const RootStore = types.model({
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
authors: types.array(Author),
|
|
49
|
+
tweets: types.array(Tweet)
|
|
50
50
|
})
|
|
51
51
|
|
|
52
52
|
// Instantiate a couple model instances
|
|
53
53
|
const jamon = Author.create({
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
id: "jamon",
|
|
55
|
+
firstName: "Jamon",
|
|
56
|
+
lastName: "Holmgren"
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
const tweet = Tweet.create({
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
id: "1",
|
|
61
|
+
author: jamon.id, // just the ID needed here
|
|
62
|
+
body: "Hello world!",
|
|
63
|
+
timestamp: Date.now()
|
|
64
64
|
})
|
|
65
65
|
|
|
66
66
|
// Now instantiate the store!
|
|
67
67
|
const rootStore = RootStore.create({
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
authors: [jamon],
|
|
69
|
+
tweets: [tweet]
|
|
70
70
|
})
|
|
71
71
|
|
|
72
72
|
// Ready to use in a React component, if that's your target.
|
|
73
73
|
import { observer } from "mobx-react-lite"
|
|
74
74
|
const MyComponent = observer((props) => {
|
|
75
|
-
|
|
75
|
+
return <div>Hello, {rootStore.authors[0].firstName}!</div>
|
|
76
76
|
})
|
|
77
77
|
|
|
78
78
|
// Note: since this component is "observed", any changes to rootStore.authors[0].firstName
|
|
@@ -82,9 +82,9 @@ const MyComponent = observer((props) => {
|
|
|
82
82
|
|
|
83
83
|
## Thanks!
|
|
84
84
|
|
|
85
|
-
-
|
|
86
|
-
-
|
|
87
|
-
-
|
|
88
|
-
-
|
|
89
|
-
-
|
|
90
|
-
-
|
|
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.
|
|
87
|
+
- [Mendix](https://mendix.com) for sponsoring and providing the opportunity to work on exploratory projects like MST.
|
|
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.
|
|
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.
|
|
90
|
+
- All the early adopters encouraging to pursue this whole idea and proving it is something feasible.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { IModelType, IAnyModelType, IDisposer, IMSTMap, IMapType, IMSTArray, IArrayType, IType, IAnyType, ModelPrimitive, ISimpleType, IComplexType, IAnyComplexType, IReferenceType, _CustomCSProcessor, _CustomOrOther, _CustomJoin, _NotCustomized, typecheck, escapeJsonPath, unescapeJsonPath, joinJsonPath, splitJsonPath, IJsonPatch, IReversibleJsonPatch, decorate, addMiddleware, IMiddlewareEvent, IActionTrackingMiddleware2Call, IMiddlewareHandler, IMiddlewareEventType, IActionTrackingMiddlewareHooks, IActionTrackingMiddleware2Hooks, process, isStateTreeNode, IStateTreeNode, IAnyStateTreeNode, flow, castFlowReturn, applyAction, onAction, IActionRecorder, ISerializedActionCall, recordActions, createActionTrackingMiddleware, createActionTrackingMiddleware2, setLivelinessChecking, getLivelinessChecking, LivelinessMode, setLivelynessChecking, // to be deprecated
|
|
2
2
|
LivelynessMode, // to be deprecated
|
|
3
|
-
ModelSnapshotType, ModelCreationType, ModelSnapshotType2, ModelCreationType2, ModelInstanceType, ModelInstanceTypeProps, ModelPropertiesDeclarationToProperties, ModelProperties, ModelPropertiesDeclaration, ModelActions, ITypeUnion, CustomTypeOptions, UnionOptions, Instance, SnapshotIn, SnapshotOut, SnapshotOrInstance, TypeOrStateTreeNodeToStateTreeNode, UnionStringArray, getType, getChildType, onPatch, onSnapshot, applyPatch, IPatchRecorder, recordPatches, protect, unprotect, isProtected, applySnapshot, getSnapshot, hasParent, getParent, hasParentOfType, getParentOfType, getRoot, getPath, getPathParts, isRoot, resolvePath, resolveIdentifier, getIdentifier, tryResolve, getRelativePath, clone, detach, destroy, isAlive, addDisposer, getEnv, walk, IModelReflectionData, IModelReflectionPropertiesData, IMaybeIType, IMaybe, IMaybeNull, IOptionalIType, OptionalDefaultValueOrFunction, ValidOptionalValue, ValidOptionalValues, getMembers, getPropertyMembers, TypeOfValue, cast, castToSnapshot, castToReferenceSnapshot, isType, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isReferenceType, isRefinementType, isUnionType, tryReference, isValidReference, OnReferenceInvalidated, OnReferenceInvalidatedEvent, ReferenceOptions, ReferenceOptionsGetSet, ReferenceOptionsOnInvalidated, ReferenceIdentifier, ISnapshotProcessor, ISnapshotProcessors, getNodeId, IActionContext, getRunningActionContext, isActionContextChildOf, isActionContextThisOrChildOf, types,
|
|
3
|
+
ModelSnapshotType, ModelCreationType, ModelSnapshotType2, ModelCreationType2, ModelInstanceType, ModelInstanceTypeProps, ModelPropertiesDeclarationToProperties, ModelProperties, ModelPropertiesDeclaration, ModelActions, ITypeUnion, CustomTypeOptions, UnionOptions, Instance, SnapshotIn, SnapshotOut, SnapshotOrInstance, TypeOrStateTreeNodeToStateTreeNode, UnionStringArray, getType, getChildType, onPatch, onSnapshot, applyPatch, IPatchRecorder, recordPatches, protect, unprotect, isProtected, applySnapshot, getSnapshot, hasParent, getParent, hasParentOfType, getParentOfType, getRoot, getPath, getPathParts, isRoot, resolvePath, resolveIdentifier, getIdentifier, tryResolve, getRelativePath, clone, detach, destroy, isAlive, addDisposer, getEnv, walk, IModelReflectionData, IModelReflectionPropertiesData, IMaybeIType, IMaybe, IMaybeNull, IOptionalIType, OptionalDefaultValueOrFunction, ValidOptionalValue, ValidOptionalValues, getMembers, getPropertyMembers, TypeOfValue, cast, castToSnapshot, castToReferenceSnapshot, isType, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isReferenceType, isRefinementType, isUnionType, tryReference, isValidReference, OnReferenceInvalidated, OnReferenceInvalidatedEvent, ReferenceOptions, ReferenceOptionsGetSet, ReferenceOptionsOnInvalidated, ReferenceIdentifier, ISnapshotProcessor, ISnapshotProcessors, getNodeId, IActionContext, getRunningActionContext, isActionContextChildOf, isActionContextThisOrChildOf, types, types as t, // We do this as a less ambiguous term for the traditional types module, which sometimes gets confused when discussing "types" in general
|
|
4
|
+
toGeneratorFunction, toGenerator } from "./internal";
|
package/dist/mobx-state-tree.js
CHANGED
|
@@ -1462,6 +1462,12 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1462
1462
|
writable: true,
|
|
1463
1463
|
value: void 0
|
|
1464
1464
|
});
|
|
1465
|
+
Object.defineProperty(_this, "hasSnapshotPostProcessor", {
|
|
1466
|
+
enumerable: true,
|
|
1467
|
+
configurable: true,
|
|
1468
|
+
writable: true,
|
|
1469
|
+
value: false
|
|
1470
|
+
});
|
|
1465
1471
|
Object.defineProperty(_this, "_applyPatches", {
|
|
1466
1472
|
enumerable: true,
|
|
1467
1473
|
configurable: true,
|
|
@@ -1792,6 +1798,9 @@ var ObjectNode = /** @class */ (function (_super) {
|
|
|
1792
1798
|
Object.defineProperty(ObjectNode.prototype, "snapshot", {
|
|
1793
1799
|
// advantage of using computed for a snapshot is that nicely respects transactions etc.
|
|
1794
1800
|
get: function () {
|
|
1801
|
+
if (this.hasSnapshotPostProcessor) {
|
|
1802
|
+
this.createObservableInstanceIfNeeded();
|
|
1803
|
+
}
|
|
1795
1804
|
return this._snapshotComputed.get();
|
|
1796
1805
|
},
|
|
1797
1806
|
enumerable: false,
|
|
@@ -4704,9 +4713,9 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4704
4713
|
enumerable: false,
|
|
4705
4714
|
configurable: true,
|
|
4706
4715
|
writable: true,
|
|
4707
|
-
value: function (sn) {
|
|
4716
|
+
value: function (sn, node) {
|
|
4708
4717
|
if (this._processors.postProcessor) {
|
|
4709
|
-
return this._processors.postProcessor.call(null, sn);
|
|
4718
|
+
return this._processors.postProcessor.call(null, sn, node.storedValue);
|
|
4710
4719
|
}
|
|
4711
4720
|
return sn;
|
|
4712
4721
|
}
|
|
@@ -4719,10 +4728,11 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4719
4728
|
var _this = this;
|
|
4720
4729
|
// the node has to use these methods rather than the original type ones
|
|
4721
4730
|
proxyNodeTypeMethods(node.type, this, "create");
|
|
4731
|
+
if (node instanceof ObjectNode) {
|
|
4732
|
+
node.hasSnapshotPostProcessor = !!this._processors.postProcessor;
|
|
4733
|
+
}
|
|
4722
4734
|
var oldGetSnapshot = node.getSnapshot;
|
|
4723
|
-
node.getSnapshot = function () {
|
|
4724
|
-
return _this.postProcessSnapshot(oldGetSnapshot.call(node));
|
|
4725
|
-
};
|
|
4735
|
+
node.getSnapshot = function () { return _this.postProcessSnapshot(oldGetSnapshot.call(node), node); };
|
|
4726
4736
|
if (!isUnionType(this._subtype)) {
|
|
4727
4737
|
node.getReconciliationType = function () {
|
|
4728
4738
|
return _this;
|
|
@@ -4762,7 +4772,7 @@ var SnapshotProcessor = /** @class */ (function (_super) {
|
|
|
4762
4772
|
value: function (node, applyPostProcess) {
|
|
4763
4773
|
if (applyPostProcess === void 0) { applyPostProcess = true; }
|
|
4764
4774
|
var sn = this._subtype.getSnapshot(node);
|
|
4765
|
-
return applyPostProcess ? this.postProcessSnapshot(sn) : sn;
|
|
4775
|
+
return applyPostProcess ? this.postProcessSnapshot(sn, node) : sn;
|
|
4766
4776
|
}
|
|
4767
4777
|
});
|
|
4768
4778
|
Object.defineProperty(SnapshotProcessor.prototype, "isValidSnapshot", {
|
|
@@ -4853,15 +4863,17 @@ function proxyNodeTypeMethods(nodeType, snapshotProcessorType) {
|
|
|
4853
4863
|
* interface BackendTodo {
|
|
4854
4864
|
* text: string | null
|
|
4855
4865
|
* }
|
|
4866
|
+
*
|
|
4856
4867
|
* const Todo2 = types.snapshotProcessor(Todo1, {
|
|
4857
4868
|
* // from snapshot to instance
|
|
4858
|
-
* preProcessor(
|
|
4869
|
+
* preProcessor(snapshot: BackendTodo) {
|
|
4859
4870
|
* return {
|
|
4860
4871
|
* text: sn.text || "";
|
|
4861
4872
|
* }
|
|
4862
4873
|
* },
|
|
4874
|
+
*
|
|
4863
4875
|
* // from instance to snapshot
|
|
4864
|
-
* postProcessor(
|
|
4876
|
+
* postProcessor(snapshot, node): BackendTodo {
|
|
4865
4877
|
* return {
|
|
4866
4878
|
* text: !sn.text ? null : sn.text
|
|
4867
4879
|
* }
|
|
@@ -5859,27 +5871,34 @@ function toPropertiesObject(declaredProps) {
|
|
|
5859
5871
|
if (key in Hook) {
|
|
5860
5872
|
throw fail$1("Hook '" + key + "' was defined as property. Hooks should be defined as part of the actions");
|
|
5861
5873
|
}
|
|
5874
|
+
// the user intended to use a view
|
|
5862
5875
|
var descriptor = Object.getOwnPropertyDescriptor(declaredProps, key);
|
|
5863
5876
|
if ("get" in descriptor) {
|
|
5864
5877
|
throw fail$1("Getters are not supported as properties. Please use views instead");
|
|
5865
5878
|
}
|
|
5879
|
+
// undefined and null are not valid
|
|
5866
5880
|
var value = descriptor.value;
|
|
5867
5881
|
if (value === null || value === undefined) {
|
|
5868
5882
|
throw fail$1("The default value of an attribute cannot be null or undefined as the type cannot be inferred. Did you mean `types.maybe(someType)`?");
|
|
5869
5883
|
}
|
|
5884
|
+
// its a primitive, convert to its type
|
|
5870
5885
|
else if (isPrimitive(value)) {
|
|
5871
5886
|
props[key] = optional(getPrimitiveFactoryFromValue(value), value);
|
|
5872
5887
|
}
|
|
5888
|
+
// map defaults to empty object automatically for models
|
|
5873
5889
|
else if (value instanceof MapType) {
|
|
5874
5890
|
props[key] = optional(value, {});
|
|
5875
5891
|
}
|
|
5876
5892
|
else if (value instanceof ArrayType) {
|
|
5877
5893
|
props[key] = optional(value, []);
|
|
5878
5894
|
}
|
|
5895
|
+
// its already a type
|
|
5879
5896
|
else if (isType(value)) ;
|
|
5897
|
+
// its a function, maybe the user wanted a view?
|
|
5880
5898
|
else if (devMode() && typeof value === "function") {
|
|
5881
5899
|
throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed a function. Did you forget to invoke it, or did you intend to declare a view / action?");
|
|
5882
5900
|
}
|
|
5901
|
+
// no other complex values
|
|
5883
5902
|
else if (devMode() && typeof value === "object") {
|
|
5884
5903
|
throw fail$1("Invalid type definition for property '" + key + "', it looks like you passed an object. Try passing another model type or a types.frozen.");
|
|
5885
5904
|
}
|
|
@@ -8596,6 +8615,7 @@ exports.resolvePath = resolvePath;
|
|
|
8596
8615
|
exports.setLivelinessChecking = setLivelinessChecking;
|
|
8597
8616
|
exports.setLivelynessChecking = setLivelynessChecking;
|
|
8598
8617
|
exports.splitJsonPath = splitJsonPath;
|
|
8618
|
+
exports.t = types;
|
|
8599
8619
|
exports.toGenerator = toGenerator;
|
|
8600
8620
|
exports.toGeneratorFunction = toGeneratorFunction;
|
|
8601
8621
|
exports.tryReference = tryReference;
|