@jbrowse/mobx-state-tree 5.8.7 → 5.9.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.
@@ -700,7 +700,6 @@ function walk(target, processor) {
700
700
  assertIsStateTreeNode(target, 1);
701
701
  assertIsFunction(processor, 2);
702
702
  const node = getStateTreeNode(target);
703
- // tslint:disable-next-line:no_unused-variable
704
703
  node.getChildren().forEach(child => {
705
704
  if (isStateTreeNode(child.storedValue)) {
706
705
  walk(child.storedValue, processor);
@@ -2438,8 +2437,11 @@ function addMiddleware(target, handler, includeHooks = true) {
2438
2437
  */
2439
2438
  function decorate(handler, fn, includeHooks = true) {
2440
2439
  const middleware = { handler, includeHooks };
2441
- fn.$mst_middleware = fn.$mst_middleware || [];
2442
- fn.$mst_middleware.push(middleware);
2440
+ const f = fn;
2441
+ if (!f.$mst_middleware) {
2442
+ f.$mst_middleware = [];
2443
+ }
2444
+ f.$mst_middleware.push(middleware);
2443
2445
  return fn;
2444
2446
  }
2445
2447
  class CollectedMiddlewares {
@@ -2448,8 +2450,9 @@ class CollectedMiddlewares {
2448
2450
  middlewares = [];
2449
2451
  constructor(node, fn) {
2450
2452
  // we just push middleware arrays into an array of arrays to avoid making copies
2451
- if (fn.$mst_middleware) {
2452
- this.middlewares.push(fn.$mst_middleware);
2453
+ const mw = fn.$mst_middleware;
2454
+ if (mw) {
2455
+ this.middlewares.push(mw);
2453
2456
  }
2454
2457
  let n = node;
2455
2458
  // Find all middlewares. Optimization: cache this?
@@ -2491,7 +2494,7 @@ function runMiddleWares(node, baseCall, originalFn) {
2491
2494
  return action(originalFn)(...call.args);
2492
2495
  }
2493
2496
  // skip hooks if asked to
2494
- if (!middleware.includeHooks && Hook[call.name]) {
2497
+ if (!middleware.includeHooks && call.name in Hook) {
2495
2498
  return runNextMiddleware(call);
2496
2499
  }
2497
2500
  let nextInvoked = false;
@@ -3169,17 +3172,15 @@ function freeze(value) {
3169
3172
  * Recursively freeze a value (if not in production)
3170
3173
  */
3171
3174
  function deepFreeze(value) {
3172
- if (!devMode()) {
3173
- return value;
3174
- }
3175
- freeze(value);
3176
- if (isPlainObject(value)) {
3177
- Object.keys(value).forEach(propKey => {
3178
- if (!isPrimitive(value[propKey]) &&
3179
- !Object.isFrozen(value[propKey])) {
3180
- deepFreeze(value[propKey]);
3175
+ if (devMode()) {
3176
+ freeze(value);
3177
+ if (isPlainObject(value)) {
3178
+ for (const v of Object.values(value)) {
3179
+ if (!isPrimitive(v) && !Object.isFrozen(v)) {
3180
+ deepFreeze(v);
3181
+ }
3181
3182
  }
3182
- });
3183
+ }
3183
3184
  }
3184
3185
  return value;
3185
3186
  }
@@ -3545,8 +3546,9 @@ function createFlowSpawner(name, generator) {
3545
3546
  parentEvent: parentContext,
3546
3547
  parentActionEvent: parentActionContext
3547
3548
  };
3549
+ const spawnerWithMw = spawner;
3548
3550
  function wrap(fn, type, arg) {
3549
- fn.$mst_middleware = spawner.$mst_middleware; // pick up any middleware attached to the flow
3551
+ fn.$mst_middleware = spawnerWithMw.$mst_middleware; // pick up any middleware attached to the flow
3550
3552
  return runWithActionContext({
3551
3553
  ...contextBase,
3552
3554
  type,
@@ -3559,7 +3561,7 @@ function createFlowSpawner(name, generator) {
3559
3561
  gen = generator(...initArgs);
3560
3562
  onFulfilled(undefined); // kick off the flow
3561
3563
  };
3562
- init.$mst_middleware = spawner.$mst_middleware;
3564
+ init.$mst_middleware = spawnerWithMw.$mst_middleware;
3563
3565
  runWithActionContext({
3564
3566
  ...contextBase,
3565
3567
  type: "flow_spawn",
@@ -4738,11 +4740,9 @@ class ModelType extends ComplexType {
4738
4740
  }
4739
4741
  // the goal of this is to make sure actions using "this" can call themselves,
4740
4742
  // while still allowing the middlewares to register them
4741
- const middlewares = action2.$mst_middleware; // make sure middlewares are not lost
4742
4743
  const boundAction = action2.bind(actions);
4743
- boundAction._isFlowAction =
4744
- action2._isFlowAction || false;
4745
- boundAction.$mst_middleware = middlewares;
4744
+ boundAction._isFlowAction = action2._isFlowAction ?? false;
4745
+ boundAction.$mst_middleware = action2.$mst_middleware;
4746
4746
  const actionInvoker = createActionInvoker(self, name, boundAction);
4747
4747
  actions[name] = actionInvoker;
4748
4748
  (!devMode() ? addHiddenFinalProp : addHiddenWritableProp)(self, name, actionInvoker);
@@ -4868,36 +4868,35 @@ class ModelType extends ComplexType {
4868
4868
  intercept(instance, this.willChange);
4869
4869
  observe(instance, this.didChange);
4870
4870
  }
4871
- willChange(chg) {
4872
- // TODO: mobx typings don't seem to take into account that newValue can be set even when removing a prop
4873
- const change = chg;
4871
+ willChange(change) {
4874
4872
  const node = getStateTreeNode(change.object);
4875
4873
  const subpath = change.name;
4876
4874
  node.assertWritable({ subpath });
4877
- const childType = node.type.properties[subpath];
4878
- // only properties are typed, state are stored as-is references
4879
- if (childType) {
4880
- typecheckInternal(childType, change.newValue);
4881
- change.newValue = childType.reconcile(node.getChildNode(subpath), change.newValue, node, subpath);
4875
+ // MST's removeChild sets the property to undefined rather than deleting it,
4876
+ // so mobx only ever delivers "update"/"add" changes here, never "remove".
4877
+ if (change.type !== "remove") {
4878
+ const childType = node.type.properties[subpath];
4879
+ // only properties are typed, state are stored as-is references
4880
+ if (childType) {
4881
+ typecheckInternal(childType, change.newValue);
4882
+ change.newValue = childType.reconcile(node.getChildNode(subpath), change.newValue, node, subpath);
4883
+ }
4882
4884
  }
4883
4885
  return change;
4884
4886
  }
4885
- didChange(chg) {
4886
- // TODO: mobx typings don't seem to take into account that newValue can be set even when removing a prop
4887
- const change = chg;
4887
+ didChange(change) {
4888
4888
  const childNode = getStateTreeNode(change.object);
4889
4889
  const childType = childNode.type.properties[change.name];
4890
- if (!childType) {
4891
- // don't emit patches for volatile state
4892
- return;
4890
+ // skip volatile state (no childType) and never-actually-occurring "remove" changes
4891
+ if (childType && change.type !== "remove") {
4892
+ const oldChildValue = change.type === "update" ? change.oldValue.snapshot : undefined;
4893
+ childNode.emitPatch({
4894
+ op: "replace",
4895
+ path: escapeJsonPath(change.name),
4896
+ value: change.newValue.snapshot,
4897
+ oldValue: oldChildValue
4898
+ }, childNode);
4893
4899
  }
4894
- const oldChildValue = change.oldValue ? change.oldValue.snapshot : undefined;
4895
- childNode.emitPatch({
4896
- op: "replace",
4897
- path: escapeJsonPath(change.name),
4898
- value: change.newValue.snapshot,
4899
- oldValue: oldChildValue
4900
- }, childNode);
4901
4900
  }
4902
4901
  getChildren(node) {
4903
4902
  const res = [];
@@ -5072,7 +5071,6 @@ class CoreType extends SimpleType {
5072
5071
  this.flags = flags;
5073
5072
  this.checker = checker;
5074
5073
  this.initializer = initializer;
5075
- this.flags = flags;
5076
5074
  }
5077
5075
  describe() {
5078
5076
  return this.name;
@@ -5103,7 +5101,6 @@ class CoreType extends SimpleType {
5103
5101
  * })
5104
5102
  * ```
5105
5103
  */
5106
- // tslint:disable-next-line:variable-name
5107
5104
  const string = new CoreType("string", TypeFlags.String, v => typeof v === "string");
5108
5105
  /**
5109
5106
  * `types.number` - Creates a type that can only contain a numeric value.
@@ -5117,7 +5114,6 @@ const string = new CoreType("string", TypeFlags.String, v => typeof v === "strin
5117
5114
  * })
5118
5115
  * ```
5119
5116
  */
5120
- // tslint:disable-next-line:variable-name
5121
5117
  const number = new CoreType("number", TypeFlags.Number, v => typeof v === "number");
5122
5118
  /**
5123
5119
  * `types.integer` - Creates a type that can only contain an integer value.
@@ -5130,7 +5126,6 @@ const number = new CoreType("number", TypeFlags.Number, v => typeof v === "numbe
5130
5126
  * })
5131
5127
  * ```
5132
5128
  */
5133
- // tslint:disable-next-line:variable-name
5134
5129
  const integer = new CoreType("integer", TypeFlags.Integer, v => isInteger(v));
5135
5130
  /**
5136
5131
  * `types.float` - Creates a type that can only contain an float value.
@@ -5143,7 +5138,6 @@ const integer = new CoreType("integer", TypeFlags.Integer, v => isInteger(v));
5143
5138
  * })
5144
5139
  * ```
5145
5140
  */
5146
- // tslint:disable-next-line:variable-name
5147
5141
  const float = new CoreType("float", TypeFlags.Float, v => isFloat(v));
5148
5142
  /**
5149
5143
  * `types.finite` - Creates a type that can only contain an finite value.
@@ -5156,7 +5150,6 @@ const float = new CoreType("float", TypeFlags.Float, v => isFloat(v));
5156
5150
  * })
5157
5151
  * ```
5158
5152
  */
5159
- // tslint:disable-next-line:variable-name
5160
5153
  const finite = new CoreType("finite", TypeFlags.Finite, v => isFinite(v));
5161
5154
  /**
5162
5155
  * `types.boolean` - Creates a type that can only contain a boolean value.
@@ -5170,7 +5163,6 @@ const finite = new CoreType("finite", TypeFlags.Finite, v => isFinite(v));
5170
5163
  * })
5171
5164
  * ```
5172
5165
  */
5173
- // tslint:disable-next-line:variable-name
5174
5166
  const boolean = new CoreType("boolean", TypeFlags.Boolean, v => typeof v === "boolean");
5175
5167
  /**
5176
5168
  * `types.null` - The type of the value `null`