mobx-state-tree 5.2.0-alpha.1 → 5.2.0-alpha.2
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/dist/core/mst-operations.d.ts +10 -10
- package/dist/mobx-state-tree.js +42 -24
- package/dist/mobx-state-tree.min.js +1 -1
- package/dist/mobx-state-tree.module.js +42 -24
- package/dist/mobx-state-tree.umd.js +42 -24
- package/dist/mobx-state-tree.umd.min.js +1 -1
- package/dist/types/utility-types/enumeration.d.ts +2 -2
- package/package.json +1 -1
|
@@ -28,7 +28,7 @@ export declare function getChildType(object: IAnyStateTreeNode, propertyName?: s
|
|
|
28
28
|
/**
|
|
29
29
|
* Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
|
|
30
30
|
* See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
|
|
31
|
-
* Patches can be used to
|
|
31
|
+
* Patches can be used to deeply observe a model tree.
|
|
32
32
|
*
|
|
33
33
|
* @param target the model instance from which to receive patches
|
|
34
34
|
* @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
|
|
@@ -68,7 +68,7 @@ export interface IPatchRecorder {
|
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
70
|
* Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
|
|
71
|
-
* Returns
|
|
71
|
+
* Returns a recorder object with the following signature:
|
|
72
72
|
*
|
|
73
73
|
* Example:
|
|
74
74
|
* ```ts
|
|
@@ -247,7 +247,7 @@ export declare function resolveIdentifier<IT extends IAnyModelType>(type: IT, ta
|
|
|
247
247
|
*/
|
|
248
248
|
export declare function getIdentifier(target: IAnyStateTreeNode): string | null;
|
|
249
249
|
/**
|
|
250
|
-
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if
|
|
250
|
+
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
|
|
251
251
|
* else it returns undefined.
|
|
252
252
|
*
|
|
253
253
|
* @param getter Function to access the reference.
|
|
@@ -282,9 +282,9 @@ export declare function tryResolve(target: IAnyStateTreeNode, path: string): any
|
|
|
282
282
|
export declare function getRelativePath(base: IAnyStateTreeNode, target: IAnyStateTreeNode): string;
|
|
283
283
|
/**
|
|
284
284
|
* Returns a deep copy of the given state tree node as new tree.
|
|
285
|
-
*
|
|
285
|
+
* Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
|
|
286
286
|
*
|
|
287
|
-
* _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
|
|
287
|
+
* _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc. during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
|
|
288
288
|
*
|
|
289
289
|
* @param source
|
|
290
290
|
* @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
|
|
@@ -391,7 +391,7 @@ export declare function getMembers(target: IAnyStateTreeNode): IModelReflectionD
|
|
|
391
391
|
export declare function cast<O extends string | number | boolean | null | undefined = never>(snapshotOrInstance: O): O;
|
|
392
392
|
export declare function cast<O = never>(snapshotOrInstance: TypeOfValue<O>["CreationType"] | TypeOfValue<O>["SnapshotType"] | TypeOfValue<O>["Type"]): O;
|
|
393
393
|
/**
|
|
394
|
-
* Casts a node instance type to
|
|
394
|
+
* Casts a node instance type to a snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
|
|
395
395
|
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
|
|
396
396
|
* but just fool typescript into thinking so.
|
|
397
397
|
*
|
|
@@ -415,12 +415,12 @@ export declare function cast<O = never>(snapshotOrInstance: TypeOfValue<O>["Crea
|
|
|
415
415
|
* ```
|
|
416
416
|
*
|
|
417
417
|
* @param snapshotOrInstance Snapshot or instance
|
|
418
|
-
* @returns The same object
|
|
418
|
+
* @returns The same object cast as an input (creation) snapshot
|
|
419
419
|
*/
|
|
420
420
|
export declare function castToSnapshot<I>(snapshotOrInstance: I): Extract<I, IAnyStateTreeNode> extends never ? I : TypeOfValue<I>["CreationType"];
|
|
421
421
|
/**
|
|
422
|
-
* Casts a node instance type to a reference snapshot type so it can be assigned to a
|
|
423
|
-
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a
|
|
422
|
+
* Casts a node instance type to a reference snapshot type so it can be assigned to a reference snapshot (e.g. to be used inside a create call).
|
|
423
|
+
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
|
|
424
424
|
* but just fool typescript into thinking so.
|
|
425
425
|
*
|
|
426
426
|
* Example:
|
|
@@ -444,7 +444,7 @@ export declare function castToSnapshot<I>(snapshotOrInstance: I): Extract<I, IAn
|
|
|
444
444
|
* ```
|
|
445
445
|
*
|
|
446
446
|
* @param instance Instance
|
|
447
|
-
* @returns The same object
|
|
447
|
+
* @returns The same object cast as a reference snapshot (string or number)
|
|
448
448
|
*/
|
|
449
449
|
export declare function castToReferenceSnapshot<I>(instance: I): Extract<I, IAnyStateTreeNode> extends never ? I : ReferenceIdentifier;
|
|
450
450
|
/**
|
package/dist/mobx-state-tree.js
CHANGED
|
@@ -200,7 +200,7 @@ function getChildType(object, propertyName) {
|
|
|
200
200
|
/**
|
|
201
201
|
* Registers a function that will be invoked for each mutation that is applied to the provided model instance, or to any of its children.
|
|
202
202
|
* See [patches](https://github.com/mobxjs/mobx-state-tree#patches) for more details. onPatch events are emitted immediately and will not await the end of a transaction.
|
|
203
|
-
* Patches can be used to
|
|
203
|
+
* Patches can be used to deeply observe a model tree.
|
|
204
204
|
*
|
|
205
205
|
* @param target the model instance from which to receive patches
|
|
206
206
|
* @param callback the callback that is invoked for each patch. The reversePatch is a patch that would actually undo the emitted patch
|
|
@@ -245,7 +245,7 @@ function applyPatch(target, patch) {
|
|
|
245
245
|
}
|
|
246
246
|
/**
|
|
247
247
|
* Small abstraction around `onPatch` and `applyPatch`, attaches a patch listener to a tree and records all the patches.
|
|
248
|
-
* Returns
|
|
248
|
+
* Returns a recorder object with the following signature:
|
|
249
249
|
*
|
|
250
250
|
* Example:
|
|
251
251
|
* ```ts
|
|
@@ -595,7 +595,7 @@ function getIdentifier(target) {
|
|
|
595
595
|
return getStateTreeNode(target).identifier;
|
|
596
596
|
}
|
|
597
597
|
/**
|
|
598
|
-
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if
|
|
598
|
+
* Tests if a reference is valid (pointing to an existing node and optionally if alive) and returns such reference if the check passes,
|
|
599
599
|
* else it returns undefined.
|
|
600
600
|
*
|
|
601
601
|
* @param getter Function to access the reference.
|
|
@@ -695,9 +695,9 @@ function getRelativePath(base, target) {
|
|
|
695
695
|
}
|
|
696
696
|
/**
|
|
697
697
|
* Returns a deep copy of the given state tree node as new tree.
|
|
698
|
-
*
|
|
698
|
+
* Shorthand for `snapshot(x) = getType(x).create(getSnapshot(x))`
|
|
699
699
|
*
|
|
700
|
-
* _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
|
|
700
|
+
* _Tip: clone will create a literal copy, including the same identifiers. To modify identifiers etc. during cloning, don't use clone but take a snapshot of the tree, modify it, and create new instance_
|
|
701
701
|
*
|
|
702
702
|
* @param source
|
|
703
703
|
* @param keepEnvironment indicates whether the clone should inherit the same environment (`true`, the default), or not have an environment (`false`). If an object is passed in as second argument, that will act as the environment for the cloned tree.
|
|
@@ -907,13 +907,13 @@ function getMembers(target) {
|
|
|
907
907
|
* ```
|
|
908
908
|
*
|
|
909
909
|
* @param snapshotOrInstance Snapshot or instance
|
|
910
|
-
* @returns The same object
|
|
910
|
+
* @returns The same object cast as an instance
|
|
911
911
|
*/
|
|
912
912
|
function cast(snapshotOrInstance) {
|
|
913
913
|
return snapshotOrInstance;
|
|
914
914
|
}
|
|
915
915
|
/**
|
|
916
|
-
* Casts a node instance type to
|
|
916
|
+
* Casts a node instance type to a snapshot type so it can be assigned to a type snapshot (e.g. to be used inside a create call).
|
|
917
917
|
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a snapshot,
|
|
918
918
|
* but just fool typescript into thinking so.
|
|
919
919
|
*
|
|
@@ -937,14 +937,14 @@ function cast(snapshotOrInstance) {
|
|
|
937
937
|
* ```
|
|
938
938
|
*
|
|
939
939
|
* @param snapshotOrInstance Snapshot or instance
|
|
940
|
-
* @returns The same object
|
|
940
|
+
* @returns The same object cast as an input (creation) snapshot
|
|
941
941
|
*/
|
|
942
942
|
function castToSnapshot(snapshotOrInstance) {
|
|
943
943
|
return snapshotOrInstance;
|
|
944
944
|
}
|
|
945
945
|
/**
|
|
946
|
-
* Casts a node instance type to a reference snapshot type so it can be assigned to a
|
|
947
|
-
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a
|
|
946
|
+
* Casts a node instance type to a reference snapshot type so it can be assigned to a reference snapshot (e.g. to be used inside a create call).
|
|
947
|
+
* Note that this is just a cast for the type system, this is, it won't actually convert an instance to a reference snapshot,
|
|
948
948
|
* but just fool typescript into thinking so.
|
|
949
949
|
*
|
|
950
950
|
* Example:
|
|
@@ -968,7 +968,7 @@ function castToSnapshot(snapshotOrInstance) {
|
|
|
968
968
|
* ```
|
|
969
969
|
*
|
|
970
970
|
* @param instance Instance
|
|
971
|
-
* @returns The same object
|
|
971
|
+
* @returns The same object cast as a reference snapshot (string or number)
|
|
972
972
|
*/
|
|
973
973
|
function castToReferenceSnapshot(instance) {
|
|
974
974
|
return instance;
|
|
@@ -5127,7 +5127,7 @@ var MapType = /** @class */ (function (_super) {
|
|
|
5127
5127
|
configurable: true,
|
|
5128
5128
|
writable: true,
|
|
5129
5129
|
value: function (childNodes) {
|
|
5130
|
-
return new MSTMap(childNodes, this.
|
|
5130
|
+
return new MSTMap(childNodes, this.name);
|
|
5131
5131
|
}
|
|
5132
5132
|
});
|
|
5133
5133
|
Object.defineProperty(MapType.prototype, "finalizeNewInstance", {
|
|
@@ -5154,7 +5154,7 @@ var MapType = /** @class */ (function (_super) {
|
|
|
5154
5154
|
configurable: true,
|
|
5155
5155
|
writable: true,
|
|
5156
5156
|
value: function () {
|
|
5157
|
-
return
|
|
5157
|
+
return this.name;
|
|
5158
5158
|
}
|
|
5159
5159
|
});
|
|
5160
5160
|
Object.defineProperty(MapType.prototype, "getChildren", {
|
|
@@ -5390,7 +5390,7 @@ MapType.prototype.applySnapshot = mobx.action(MapType.prototype.applySnapshot);
|
|
|
5390
5390
|
* @returns
|
|
5391
5391
|
*/
|
|
5392
5392
|
function map(subtype) {
|
|
5393
|
-
return new MapType("
|
|
5393
|
+
return new MapType("Map<string, " + subtype.name + ">", subtype);
|
|
5394
5394
|
}
|
|
5395
5395
|
/**
|
|
5396
5396
|
* Returns if a given value represents a map type.
|
|
@@ -5469,7 +5469,7 @@ var ArrayType = /** @class */ (function (_super) {
|
|
|
5469
5469
|
configurable: true,
|
|
5470
5470
|
writable: true,
|
|
5471
5471
|
value: function (childNodes) {
|
|
5472
|
-
var options = __assign(__assign({}, mobxShallow), { name: this.
|
|
5472
|
+
var options = __assign(__assign({}, mobxShallow), { name: this.name });
|
|
5473
5473
|
return mobx.observable.array(convertChildNodesToArray(childNodes), options);
|
|
5474
5474
|
}
|
|
5475
5475
|
});
|
|
@@ -5497,7 +5497,7 @@ var ArrayType = /** @class */ (function (_super) {
|
|
|
5497
5497
|
configurable: true,
|
|
5498
5498
|
writable: true,
|
|
5499
5499
|
value: function () {
|
|
5500
|
-
return this.
|
|
5500
|
+
return this.name;
|
|
5501
5501
|
}
|
|
5502
5502
|
});
|
|
5503
5503
|
Object.defineProperty(ArrayType.prototype, "getChildren", {
|
|
@@ -5853,8 +5853,18 @@ var defaultObjectOptions = {
|
|
|
5853
5853
|
initializers: EMPTY_ARRAY
|
|
5854
5854
|
};
|
|
5855
5855
|
function toPropertiesObject(declaredProps) {
|
|
5856
|
+
var keysList = Object.keys(declaredProps);
|
|
5857
|
+
var alreadySeenKeys = {};
|
|
5858
|
+
keysList.forEach(function (key) {
|
|
5859
|
+
if (alreadySeenKeys[key]) {
|
|
5860
|
+
throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
|
|
5861
|
+
}
|
|
5862
|
+
else {
|
|
5863
|
+
alreadySeenKeys[key] = true;
|
|
5864
|
+
}
|
|
5865
|
+
});
|
|
5856
5866
|
// loop through properties and ensures that all items are types
|
|
5857
|
-
return
|
|
5867
|
+
return keysList.reduce(function (props, key) {
|
|
5858
5868
|
var _a, _b, _c;
|
|
5859
5869
|
// warn if user intended a HOOK
|
|
5860
5870
|
if (key in Hook)
|
|
@@ -6200,7 +6210,7 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6200
6210
|
configurable: true,
|
|
6201
6211
|
writable: true,
|
|
6202
6212
|
value: function (childNodes) {
|
|
6203
|
-
var options = __assign(__assign({}, mobxShallow), { name: this.
|
|
6213
|
+
var options = __assign(__assign({}, mobxShallow), { name: this.name });
|
|
6204
6214
|
return mobx.observable.object(childNodes, EMPTY_OBJECT, options);
|
|
6205
6215
|
}
|
|
6206
6216
|
});
|
|
@@ -6277,10 +6287,11 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6277
6287
|
configurable: true,
|
|
6278
6288
|
writable: true,
|
|
6279
6289
|
value: function (node, key) {
|
|
6290
|
+
var _a;
|
|
6280
6291
|
if (!(key in this.properties))
|
|
6281
6292
|
throw fail$1("Not a value property: " + key);
|
|
6282
6293
|
var adm = mobx._getAdministration(node.storedValue, key);
|
|
6283
|
-
var childNode = adm.raw();
|
|
6294
|
+
var childNode = (_a = adm.raw) === null || _a === void 0 ? void 0 : _a.call(adm);
|
|
6284
6295
|
if (!childNode)
|
|
6285
6296
|
throw fail$1("Node not available for property " + key);
|
|
6286
6297
|
return childNode;
|
|
@@ -6295,7 +6306,14 @@ var ModelType = /** @class */ (function (_super) {
|
|
|
6295
6306
|
if (applyPostProcess === void 0) { applyPostProcess = true; }
|
|
6296
6307
|
var res = {};
|
|
6297
6308
|
this.forAllProps(function (name, type) {
|
|
6298
|
-
|
|
6309
|
+
try {
|
|
6310
|
+
// TODO: FIXME, make sure the observable ref is used!
|
|
6311
|
+
var atom = mobx.getAtom(node.storedValue, name);
|
|
6312
|
+
atom.reportObserved();
|
|
6313
|
+
}
|
|
6314
|
+
catch (e) {
|
|
6315
|
+
throw fail$1(name + " property is declared twice");
|
|
6316
|
+
}
|
|
6299
6317
|
res[name] = _this.getChildNode(node, name).snapshot;
|
|
6300
6318
|
});
|
|
6301
6319
|
if (applyPostProcess) {
|
|
@@ -7167,7 +7185,7 @@ var OptionalValue = /** @class */ (function (_super) {
|
|
|
7167
7185
|
writable: true,
|
|
7168
7186
|
value: function (parent, subpath, environment, initialValue) {
|
|
7169
7187
|
if (this.optionalValues.indexOf(initialValue) >= 0) {
|
|
7170
|
-
var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot(
|
|
7188
|
+
var defaultInstanceOrSnapshot = this.getDefaultInstanceOrSnapshot();
|
|
7171
7189
|
return this._subtype.instantiate(parent, subpath, environment, defaultInstanceOrSnapshot);
|
|
7172
7190
|
}
|
|
7173
7191
|
return this._subtype.instantiate(parent, subpath, environment, initialValue);
|
|
@@ -7180,16 +7198,16 @@ var OptionalValue = /** @class */ (function (_super) {
|
|
|
7180
7198
|
value: function (current, newValue, parent, subpath) {
|
|
7181
7199
|
return this._subtype.reconcile(current, this.optionalValues.indexOf(newValue) < 0 && this._subtype.is(newValue)
|
|
7182
7200
|
? newValue
|
|
7183
|
-
: this.getDefaultInstanceOrSnapshot(
|
|
7201
|
+
: this.getDefaultInstanceOrSnapshot(), parent, subpath);
|
|
7184
7202
|
}
|
|
7185
7203
|
});
|
|
7186
7204
|
Object.defineProperty(OptionalValue.prototype, "getDefaultInstanceOrSnapshot", {
|
|
7187
7205
|
enumerable: false,
|
|
7188
7206
|
configurable: true,
|
|
7189
7207
|
writable: true,
|
|
7190
|
-
value: function (
|
|
7208
|
+
value: function () {
|
|
7191
7209
|
var defaultInstanceOrSnapshot = typeof this._defaultValue === "function"
|
|
7192
|
-
? this._defaultValue(
|
|
7210
|
+
? this._defaultValue()
|
|
7193
7211
|
: this._defaultValue;
|
|
7194
7212
|
// while static values are already snapshots and checked on types.optional
|
|
7195
7213
|
// generator functions must always be rechecked just in case
|