mobx-state-tree 5.4.2-pre.1 → 6.0.0-pre.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.
@@ -339,7 +339,7 @@ export declare function isAlive(target: IAnyStateTreeNode): boolean;
339
339
  */
340
340
  export declare function addDisposer(target: IAnyStateTreeNode, disposer: IDisposer): IDisposer;
341
341
  /**
342
- * Returns the environment of the current state tree. For more info on environments,
342
+ * Returns the environment of the current state tree, or throws. For more info on environments,
343
343
  * see [Dependency injection](https://github.com/mobxjs/mobx-state-tree#dependency-injection)
344
344
  *
345
345
  * Please note that in child nodes access to the root is only possible
@@ -351,6 +351,14 @@ export declare function addDisposer(target: IAnyStateTreeNode, disposer: IDispos
351
351
  * @returns
352
352
  */
353
353
  export declare function getEnv<T = any>(target: IAnyStateTreeNode): T;
354
+ /**
355
+ * Returns whether the current state tree has environment or not.
356
+ *
357
+ * @export
358
+ * @param {IStateTreeNode} target
359
+ * @return {boolean}
360
+ */
361
+ export declare function hasEnv(target: IAnyStateTreeNode): boolean;
354
362
  /**
355
363
  * Performs a depth first walk through a tree.
356
364
  */
package/dist/index.d.ts CHANGED
@@ -1,4 +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, 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
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, hasEnv, 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
4
  toGeneratorFunction, toGenerator } from "./internal";
@@ -794,7 +794,7 @@ function addDisposer(target, disposer) {
794
794
  return disposer;
795
795
  }
796
796
  /**
797
- * Returns the environment of the current state tree. For more info on environments,
797
+ * Returns the environment of the current state tree, or throws. For more info on environments,
798
798
  * see [Dependency injection](https://github.com/mobxjs/mobx-state-tree#dependency-injection)
799
799
  *
800
800
  * Please note that in child nodes access to the root is only possible
@@ -811,9 +811,26 @@ function getEnv(target) {
811
811
  var node = getStateTreeNode(target);
812
812
  var env = node.root.environment;
813
813
  if (!env)
814
- return EMPTY_OBJECT;
814
+ throw fail("Failed to find the environment of ".concat(node, " ").concat(node.path));
815
815
  return env;
816
816
  }
817
+ /**
818
+ * Returns whether the current state tree has environment or not.
819
+ *
820
+ * @export
821
+ * @param {IStateTreeNode} target
822
+ * @return {boolean}
823
+ */
824
+ function hasEnv(target) {
825
+ // check all arguments
826
+ if (process.env.NODE_ENV !== "production") {
827
+ if (!isStateTreeNode(target))
828
+ fail("expected first argument to be a mobx-state-tree node, got " + target + " instead");
829
+ }
830
+ var node = getStateTreeNode(target);
831
+ var env = node.root.environment;
832
+ return !!env;
833
+ }
817
834
  /**
818
835
  * Performs a depth first walk through a tree.
819
836
  */
@@ -2134,9 +2151,9 @@ var ObjectNode = /** @class */ (function (_super) {
2134
2151
  writable: true,
2135
2152
  value: function (basePatch, source) {
2136
2153
  if (this._internalEventsHasSubscribers("patch" /* InternalEvents.Patch */)) {
2137
- var localizedPatch = extend({}, basePatch, {
2138
- path: source.path.substr(this.path.length) + "/" + basePatch.path // calculate the relative path of the patch
2139
- });
2154
+ // calculate the relative path of the patch
2155
+ var path = source.path.substr(this.path.length) + (basePatch.path ? "/" + basePatch.path : "");
2156
+ var localizedPatch = extend({}, basePatch, { path: path });
2140
2157
  var _a = __read(splitPatch(localizedPatch), 2), patch = _a[0], reversePatch = _a[1];
2141
2158
  this._internalEventsEmit("patch" /* InternalEvents.Patch */, patch, reversePatch);
2142
2159
  }
@@ -5605,6 +5622,15 @@ var ArrayType = /** @class */ (function (_super) {
5605
5622
  oldValue: change.oldValue ? change.oldValue.snapshot : undefined
5606
5623
  }, node);
5607
5624
  case "splice":
5625
+ // Perf: emit one patch for clear and replace ops.
5626
+ if (change.removedCount && change.addedCount === change.object.length) {
5627
+ return void node.emitPatch({
5628
+ op: "replace",
5629
+ path: "",
5630
+ value: node.snapshot,
5631
+ oldValue: change.removed.map(function (node) { return node.snapshot; })
5632
+ }, node);
5633
+ }
5608
5634
  for (var i = change.removedCount - 1; i >= 0; i--)
5609
5635
  node.emitPatch({
5610
5636
  op: "remove",
@@ -5615,7 +5641,7 @@ var ArrayType = /** @class */ (function (_super) {
5615
5641
  node.emitPatch({
5616
5642
  op: "add",
5617
5643
  path: "" + (change.index + i),
5618
- value: node.getChildNode("" + (change.index + i)).snapshot,
5644
+ value: change.added[i].snapshot,
5619
5645
  oldValue: undefined
5620
5646
  }, node);
5621
5647
  return;
@@ -8588,6 +8614,7 @@ exports.getRoot = getRoot;
8588
8614
  exports.getRunningActionContext = getRunningActionContext;
8589
8615
  exports.getSnapshot = getSnapshot;
8590
8616
  exports.getType = getType;
8617
+ exports.hasEnv = hasEnv;
8591
8618
  exports.hasParent = hasParent;
8592
8619
  exports.hasParentOfType = hasParentOfType;
8593
8620
  exports.isActionContextChildOf = isActionContextChildOf;