@jbrowse/mobx-state-tree 5.12.0 → 5.13.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/dist/index.d.ts +38 -1
- package/dist/mobx-state-tree.cjs +182 -15
- package/dist/mobx-state-tree.cjs.map +1 -1
- package/dist/mobx-state-tree.mjs +182 -16
- package/dist/mobx-state-tree.mjs.map +1 -1
- package/package.json +1 -1
package/dist/mobx-state-tree.mjs
CHANGED
|
@@ -1137,6 +1137,10 @@ const snapshotReactionOptions = {
|
|
|
1137
1137
|
throw e;
|
|
1138
1138
|
}
|
|
1139
1139
|
};
|
|
1140
|
+
// keepAlive lets the snapshot computed memoize its value even with no observer,
|
|
1141
|
+
// so getSnapshot() stays referentially stable without an eager snapshot
|
|
1142
|
+
// reaction serializing the whole tree at create time. Shared: mobx only reads it.
|
|
1143
|
+
const snapshotComputedOptions = { keepAlive: true };
|
|
1140
1144
|
/**
|
|
1141
1145
|
* @internal
|
|
1142
1146
|
* @hidden
|
|
@@ -1169,9 +1173,18 @@ class ObjectNode extends BaseNode {
|
|
|
1169
1173
|
_cachedInitialSnapshot;
|
|
1170
1174
|
_cachedInitialSnapshotCreated = false;
|
|
1171
1175
|
_snapshotComputed;
|
|
1176
|
+
// whether _snapshotComputed has ever run — see createObservableInstance
|
|
1177
|
+
_snapshotComputedEvaluated = false;
|
|
1172
1178
|
constructor(complexType, parent, subpath, environment, initialValue) {
|
|
1173
1179
|
super(complexType, parent, subpath, environment);
|
|
1174
|
-
|
|
1180
|
+
// keepAlive: memoize the snapshot after the first read even with no
|
|
1181
|
+
// observer, so getSnapshot() is referentially stable (and reconcile-by-
|
|
1182
|
+
// snapshot works) without eagerly serializing the whole tree at create
|
|
1183
|
+
// time — the previous cost, driven by the eager root reaction below.
|
|
1184
|
+
this._snapshotComputed = computed(() => {
|
|
1185
|
+
this._snapshotComputedEvaluated = true;
|
|
1186
|
+
return freeze(this.getSnapshot());
|
|
1187
|
+
}, snapshotComputedOptions);
|
|
1175
1188
|
this.unbox = this.unbox.bind(this);
|
|
1176
1189
|
this._initialSnapshot = freeze(initialValue);
|
|
1177
1190
|
this.identifierAttribute = complexType.identifierAttribute;
|
|
@@ -1255,10 +1268,17 @@ class ObjectNode extends BaseNode {
|
|
|
1255
1268
|
this._isRunningAction = false;
|
|
1256
1269
|
}
|
|
1257
1270
|
this._observableInstanceState = ObservableInstanceLifecycle.CREATED;
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1271
|
+
// The snapshot computed branches on the non-observable
|
|
1272
|
+
// "_observableInstanceState" flag we just flipped, so a value already
|
|
1273
|
+
// memoized from the initial snapshot is now stale (mobx can't see the
|
|
1274
|
+
// change) — refresh it. When it was never read there is nothing to refresh,
|
|
1275
|
+
// and forcing it here would serialize the whole subtree (the dominant cost
|
|
1276
|
+
// of creating a root) for a value nobody has asked for yet, so we skip it.
|
|
1277
|
+
this.refreshMemoizedSnapshot();
|
|
1278
|
+
// The root's snapshot reaction only drives onSnapshot; set it up lazily
|
|
1279
|
+
// (onSnapshot already does) rather than eagerly here, so a root nobody
|
|
1280
|
+
// subscribes to never pays for a full-tree snapshot at creation. keepAlive
|
|
1281
|
+
// on _snapshotComputed preserves snapshot referential stability without it.
|
|
1262
1282
|
this._childNodes = EMPTY_OBJECT;
|
|
1263
1283
|
this.state = NodeLifeCycle.CREATED;
|
|
1264
1284
|
if (fireHooks) {
|
|
@@ -1363,6 +1383,21 @@ class ObjectNode extends BaseNode {
|
|
|
1363
1383
|
}
|
|
1364
1384
|
return this._snapshotComputed.get();
|
|
1365
1385
|
}
|
|
1386
|
+
/**
|
|
1387
|
+
* @internal
|
|
1388
|
+
* @hidden
|
|
1389
|
+
* Recompute+retrack the keepAlive snapshot computed if it has already run.
|
|
1390
|
+
* Needed whenever something changes what the snapshot would produce without
|
|
1391
|
+
* an observable mobx would track: flipping the (non-observable) observable-
|
|
1392
|
+
* instance state, or SnapshotProcessor rewriting getSnapshot after
|
|
1393
|
+
* construction (see snapshotProcessor._fixNode). If it never ran, there is
|
|
1394
|
+
* nothing cached to go stale, so this is a no-op.
|
|
1395
|
+
*/
|
|
1396
|
+
refreshMemoizedSnapshot() {
|
|
1397
|
+
if (this._snapshotComputedEvaluated) {
|
|
1398
|
+
this._snapshotComputed.trackAndCompute();
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1366
1401
|
// NOTE: we use this method to get snapshot without creating @computed overhead
|
|
1367
1402
|
getSnapshot() {
|
|
1368
1403
|
if (!this.isAlive) {
|
|
@@ -3310,6 +3345,11 @@ let _typeChecking;
|
|
|
3310
3345
|
function setTypeChecking(enabled) {
|
|
3311
3346
|
_typeChecking = enabled;
|
|
3312
3347
|
}
|
|
3348
|
+
// the ENABLE_TYPE_CHECK env var is fixed at process start, so read it once:
|
|
3349
|
+
// process.env access is a comparatively expensive lookup and isTypeCheckingEnabled
|
|
3350
|
+
// runs on every create() / typed write. The setTypeChecking() override and
|
|
3351
|
+
// dev-mode default stay live below.
|
|
3352
|
+
const _envTypeCheck = typeof process !== "undefined" && process.env?.ENABLE_TYPE_CHECK === "true";
|
|
3313
3353
|
/**
|
|
3314
3354
|
* @internal
|
|
3315
3355
|
* @hidden
|
|
@@ -3317,10 +3357,7 @@ function setTypeChecking(enabled) {
|
|
|
3317
3357
|
function isTypeCheckingEnabled() {
|
|
3318
3358
|
// an explicit setTypeChecking() override (incl. `false`) wins over the
|
|
3319
3359
|
// dev-mode / env-var default, hence ?? rather than ||
|
|
3320
|
-
return
|
|
3321
|
-
(devMode() ||
|
|
3322
|
-
(typeof process !== "undefined" &&
|
|
3323
|
-
process.env?.ENABLE_TYPE_CHECK === "true")));
|
|
3360
|
+
return _typeChecking ?? (devMode() || _envTypeCheck);
|
|
3324
3361
|
}
|
|
3325
3362
|
let _devMode = process.env.NODE_ENV !== "production";
|
|
3326
3363
|
/**
|
|
@@ -3723,6 +3760,11 @@ class SnapshotProcessor extends BaseType {
|
|
|
3723
3760
|
}
|
|
3724
3761
|
const oldGetSnapshot = node.getSnapshot;
|
|
3725
3762
|
node.getSnapshot = () => this.postProcessSnapshot(oldGetSnapshot.call(node), node);
|
|
3763
|
+
// getSnapshot was just rewritten; refresh any snapshot already memoized for
|
|
3764
|
+
// this node (e.g. it was serialized before being moved under this processor)
|
|
3765
|
+
if (node instanceof ObjectNode) {
|
|
3766
|
+
node.refreshMemoizedSnapshot();
|
|
3767
|
+
}
|
|
3726
3768
|
if (!isUnionType(this._subtype)) {
|
|
3727
3769
|
node.getReconciliationType = () => {
|
|
3728
3770
|
return this;
|
|
@@ -4645,11 +4687,27 @@ class ModelType extends ComplexType {
|
|
|
4645
4687
|
constructor(opts) {
|
|
4646
4688
|
super(opts.name || defaultObjectOptions.name);
|
|
4647
4689
|
Object.assign(this, defaultObjectOptions, opts);
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4690
|
+
if (opts.propertiesArePreProcessed) {
|
|
4691
|
+
// `properties` is a parent type's already-converted + frozen output
|
|
4692
|
+
// (chain step with no new props), so its derived propertyNames and
|
|
4693
|
+
// identifierAttribute are identical to the parent's — reuse them verbatim
|
|
4694
|
+
// (cloneAndEnhance passed them in) instead of re-running Object.keys and
|
|
4695
|
+
// the per-prop identifier scan, both O(props), on every step.
|
|
4696
|
+
this.propertyNames = opts.propertyNames;
|
|
4697
|
+
this.identifierAttribute = opts.identifierAttribute;
|
|
4698
|
+
}
|
|
4699
|
+
else {
|
|
4700
|
+
// `propertiesAreConverted` (set by cloneAndEnhance when props are added)
|
|
4701
|
+
// means every value is already a type — the parent's converted bag merged
|
|
4702
|
+
// with a freshly converted delta — so skip re-converting. Only raw entry
|
|
4703
|
+
// points (`model()`) still need the full toPropertiesObject pass.
|
|
4704
|
+
if (!opts.propertiesAreConverted) {
|
|
4705
|
+
this.properties = toPropertiesObject(this.properties);
|
|
4706
|
+
}
|
|
4707
|
+
freeze(this.properties); // make sure nobody messes with it
|
|
4708
|
+
this.propertyNames = Object.keys(this.properties);
|
|
4709
|
+
this.identifierAttribute = this._getIdentifierAttribute();
|
|
4710
|
+
}
|
|
4653
4711
|
}
|
|
4654
4712
|
_getIdentifierAttribute() {
|
|
4655
4713
|
let identifierAttribute = undefined;
|
|
@@ -4664,9 +4722,35 @@ class ModelType extends ComplexType {
|
|
|
4664
4722
|
return identifierAttribute;
|
|
4665
4723
|
}
|
|
4666
4724
|
cloneAndEnhance(opts) {
|
|
4725
|
+
// Fast path: a chain step that adds no new properties (.actions/.views/
|
|
4726
|
+
// .volatile/.named/pre-postProcessor) reuses this type's already-converted +
|
|
4727
|
+
// frozen properties (and their derived names/identifier) verbatim.
|
|
4728
|
+
const hasNewProps = opts.properties !== undefined && Object.keys(opts.properties).length > 0;
|
|
4729
|
+
if (!hasNewProps) {
|
|
4730
|
+
return new ModelType({
|
|
4731
|
+
name: opts.name || this.name,
|
|
4732
|
+
properties: this.properties,
|
|
4733
|
+
propertiesArePreProcessed: true,
|
|
4734
|
+
// safe to share: propertyNames is never mutated after construction
|
|
4735
|
+
propertyNames: this.propertyNames,
|
|
4736
|
+
identifierAttribute: this.identifierAttribute,
|
|
4737
|
+
initializers: this.initializers.concat(opts.initializers || []),
|
|
4738
|
+
preProcessor: opts.preProcessor || this.preProcessor,
|
|
4739
|
+
postProcessor: opts.postProcessor || this.postProcessor
|
|
4740
|
+
});
|
|
4741
|
+
}
|
|
4742
|
+
// Adding props: this type's `properties` is already converted+frozen, so only
|
|
4743
|
+
// the delta needs conversion. compose passes already-converted props
|
|
4744
|
+
// (cur.properties) and flags them; `.props()` passes a raw declaration. Either
|
|
4745
|
+
// way the merged bag is fully converted, so the constructor skips re-converting
|
|
4746
|
+
// the (potentially large) inherited set — the previous hot spot in compose.
|
|
4747
|
+
const convertedNewProps = opts.propertiesAreConverted
|
|
4748
|
+
? opts.properties
|
|
4749
|
+
: toPropertiesObject(opts.properties);
|
|
4667
4750
|
return new ModelType({
|
|
4668
4751
|
name: opts.name || this.name,
|
|
4669
|
-
properties: Object.assign({}, this.properties,
|
|
4752
|
+
properties: Object.assign({}, this.properties, convertedNewProps),
|
|
4753
|
+
propertiesAreConverted: true,
|
|
4670
4754
|
initializers: this.initializers.concat(opts.initializers || []),
|
|
4671
4755
|
preProcessor: opts.preProcessor || this.preProcessor,
|
|
4672
4756
|
postProcessor: opts.postProcessor || this.postProcessor
|
|
@@ -4763,6 +4847,29 @@ class ModelType extends ComplexType {
|
|
|
4763
4847
|
};
|
|
4764
4848
|
return this.cloneAndEnhance({ initializers: [viewInitializer] });
|
|
4765
4849
|
}
|
|
4850
|
+
/**
|
|
4851
|
+
* @internal
|
|
4852
|
+
* @hidden
|
|
4853
|
+
* Materializes an additional actions/views/state extension onto an
|
|
4854
|
+
* already-created instance, using the same machinery as `.extend()` runs at
|
|
4855
|
+
* creation time. Powers {@link extendInstance}; callers must ensure the writes
|
|
4856
|
+
* are permitted (see extendInstance, which runs this in an action context).
|
|
4857
|
+
*/
|
|
4858
|
+
applyExtensionToInstance(self, extension) {
|
|
4859
|
+
const { actions, views, state, ...rest } = extension;
|
|
4860
|
+
for (const key in rest) {
|
|
4861
|
+
throw fail(`extendInstance should return an object with a subset of the fields 'actions', 'views' and 'state'. Found invalid key '${key}'`);
|
|
4862
|
+
}
|
|
4863
|
+
if (state) {
|
|
4864
|
+
this.instantiateVolatileState(self, state);
|
|
4865
|
+
}
|
|
4866
|
+
if (views) {
|
|
4867
|
+
this.instantiateViews(self, views);
|
|
4868
|
+
}
|
|
4869
|
+
if (actions) {
|
|
4870
|
+
this.instantiateActions(self, actions);
|
|
4871
|
+
}
|
|
4872
|
+
}
|
|
4766
4873
|
instantiateViews(self, views) {
|
|
4767
4874
|
// check views return
|
|
4768
4875
|
if (!isPlainObject(views)) {
|
|
@@ -5023,6 +5130,8 @@ function compose(...args) {
|
|
|
5023
5130
|
.reduce((prev, cur) => prev.cloneAndEnhance({
|
|
5024
5131
|
name: `${prev.name}_${cur.name}`,
|
|
5025
5132
|
properties: cur.properties,
|
|
5133
|
+
// cur.properties is another ModelType's already-converted+frozen bag
|
|
5134
|
+
propertiesAreConverted: true,
|
|
5026
5135
|
initializers: cur.initializers,
|
|
5027
5136
|
preProcessor: (snapshot) => cur.applySnapshotPreProcessor(prev.applySnapshotPreProcessor(snapshot)),
|
|
5028
5137
|
postProcessor: (snapshot) => cur.applySnapshotPostProcessor(prev.applySnapshotPostProcessor(snapshot))
|
|
@@ -5038,6 +5147,46 @@ function compose(...args) {
|
|
|
5038
5147
|
function isModelType(type) {
|
|
5039
5148
|
return isType(type) && (type.flags & TypeFlags.Object) > 0;
|
|
5040
5149
|
}
|
|
5150
|
+
/**
|
|
5151
|
+
* `extendInstance` - attaches additional actions, views and volatile state to an
|
|
5152
|
+
* already-created model instance, using the same instantiation machinery as
|
|
5153
|
+
* `.extend()`. This enables lazily loading a model's views/actions chain: create
|
|
5154
|
+
* the model with only its base props (plus any critical-path members), then attach
|
|
5155
|
+
* the rest at runtime — e.g. from a dynamically imported module — without rebuilding
|
|
5156
|
+
* the type or re-hydrating the tree.
|
|
5157
|
+
*
|
|
5158
|
+
* The extension is applied inside an action context, so it works on protected trees.
|
|
5159
|
+
* Attached views are computeds (reactive, memoized) and attached actions get a full
|
|
5160
|
+
* MST action context, exactly like members declared in the original chain.
|
|
5161
|
+
*
|
|
5162
|
+
* Note: the attached members are NOT part of the instance's *static* type. If you
|
|
5163
|
+
* need them typed, declare the augmented shape separately (an `import type` of the
|
|
5164
|
+
* chain's return type is erased at build time, so it costs nothing in the bundle).
|
|
5165
|
+
*
|
|
5166
|
+
* @param instance a live model instance
|
|
5167
|
+
* @param fn receives the instance and returns `{ actions?, views?, state? }`
|
|
5168
|
+
* @returns the same instance
|
|
5169
|
+
*/
|
|
5170
|
+
function extendInstance(instance, fn) {
|
|
5171
|
+
if (!isStateTreeNode(instance)) {
|
|
5172
|
+
throw fail("extendInstance expects a mobx-state-tree node");
|
|
5173
|
+
}
|
|
5174
|
+
const type = getStateTreeNode(instance).type;
|
|
5175
|
+
if (!isModelType(type)) {
|
|
5176
|
+
throw fail("extendInstance can only be used on model instances");
|
|
5177
|
+
}
|
|
5178
|
+
// Views/actions/volatile are installed via defineProperty + makeObservable — the
|
|
5179
|
+
// same "add" operations the initializers perform at creation, before the
|
|
5180
|
+
// write-protection interceptor is attached (see finalizeNewInstance). On a live
|
|
5181
|
+
// instance that interceptor is already active, so run the attach inside an action
|
|
5182
|
+
// context: isRunningAction() then short-circuits assertWritable.
|
|
5183
|
+
const modelType = type;
|
|
5184
|
+
const attach = createActionInvoker(instance, "@@extendInstance", (() => {
|
|
5185
|
+
modelType.applyExtensionToInstance(instance, fn(instance));
|
|
5186
|
+
}));
|
|
5187
|
+
attach();
|
|
5188
|
+
return instance;
|
|
5189
|
+
}
|
|
5041
5190
|
|
|
5042
5191
|
// TODO: implement CoreType using types.custom ?
|
|
5043
5192
|
/**
|
|
@@ -5466,7 +5615,24 @@ class Union extends BaseType {
|
|
|
5466
5615
|
}
|
|
5467
5616
|
return `${baseWithDiscriminator}:\n ${formatValidationErrorLines(errors).join("\n ")}`;
|
|
5468
5617
|
}
|
|
5618
|
+
// Memoizes the discriminator -> member scan below. Union membership is fixed
|
|
5619
|
+
// at construction, so the result for a given `type` string never changes.
|
|
5620
|
+
// Without this, validating a config with many elements drawn from a wide
|
|
5621
|
+
// pluggable union (e.g. jbrowse's 30+ track/adapter types) re-scans every
|
|
5622
|
+
// member — and calls resolveModelType + literal.is() on each — once per
|
|
5623
|
+
// element. With it, each distinct discriminator scans once; the rest are
|
|
5624
|
+
// O(1) map hits. `undefined` (no match OR ambiguous) is cached too.
|
|
5625
|
+
_discriminatorCache;
|
|
5469
5626
|
_findCandidateByTypeDiscriminator(discriminator) {
|
|
5627
|
+
const cache = (this._discriminatorCache ??= new Map());
|
|
5628
|
+
if (cache.has(discriminator)) {
|
|
5629
|
+
return cache.get(discriminator);
|
|
5630
|
+
}
|
|
5631
|
+
const found = this._scanForTypeDiscriminator(discriminator);
|
|
5632
|
+
cache.set(discriminator, found);
|
|
5633
|
+
return found;
|
|
5634
|
+
}
|
|
5635
|
+
_scanForTypeDiscriminator(discriminator) {
|
|
5470
5636
|
let found;
|
|
5471
5637
|
for (const t of this._types) {
|
|
5472
5638
|
const model = resolveModelType(t);
|
|
@@ -6987,5 +7153,5 @@ const types = {
|
|
|
6987
7153
|
resilient
|
|
6988
7154
|
};
|
|
6989
7155
|
|
|
6990
|
-
export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, flow, getChildType, getEnv, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, getUnionSubtypes, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setDevMode, setLivelinessChecking, setLivelynessChecking, setTypeChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
|
|
7156
|
+
export { addDisposer, addMiddleware, applyAction, applyPatch, applySnapshot, cast, castFlowReturn, castToReferenceSnapshot, castToSnapshot, clone, createActionTrackingMiddleware, createActionTrackingMiddleware2, decorate, destroy, detach, escapeJsonPath, extendInstance, flow, getChildType, getEnv, getIdentifier, getLivelinessChecking, getMembers, getNodeId, getParent, getParentOfType, getPath, getPathParts, getPropertyMembers, getRelativePath, getRoot, getRunningActionContext, getSnapshot, getType, getUnionSubtypes, hasParent, hasParentOfType, isActionContextChildOf, isActionContextThisOrChildOf, isAlive, isArrayType, isFrozenType, isIdentifierType, isLateType, isLiteralType, isMapType, isModelType, isOptionalType, isPrimitiveType, isProtected, isReferenceType, isRefinementType, isRoot, isStateTreeNode, isType, isUnionType, isValidReference, joinJsonPath, onAction, onPatch, onSnapshot, protect, recordActions, recordPatches, resolveIdentifier, resolvePath, setDevMode, setLivelinessChecking, setLivelynessChecking, setTypeChecking, splitJsonPath, types as t, toGenerator, toGeneratorFunction, tryReference, tryResolve, typecheck, types, unescapeJsonPath, unprotect, walk };
|
|
6991
7157
|
//# sourceMappingURL=mobx-state-tree.mjs.map
|