mobx-state-tree 5.3.0 → 5.4.0-pre.1

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 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
- id: types.identifier,
36
- firstName: types.string,
37
- lastName: types.string
35
+ id: types.identifier,
36
+ firstName: types.string,
37
+ lastName: types.string
38
38
  })
39
39
  const Tweet = types.model({
40
- id: types.identifier,
41
- author: types.reference(Author), // stores just the `id` reference!
42
- body: types.string,
43
- timestamp: types.number
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
- authors: types.array(Author),
49
- tweets: types.array(Tweet)
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
- id: "jamon",
55
- firstName: "Jamon",
56
- lastName: "Holmgren"
54
+ id: "jamon",
55
+ firstName: "Jamon",
56
+ lastName: "Holmgren"
57
57
  })
58
58
 
59
59
  const tweet = Tweet.create({
60
- id: "1",
61
- author: jamon.id, // just the ID needed here
62
- body: "Hello world!",
63
- timestamp: Date.now()
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
- authors: [jamon],
69
- tweets: [tweet]
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
- return <div>Hello, {rootStore.authors[0].firstName}!</div>
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
- - [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.
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, toGeneratorFunction, toGenerator } from "./internal";
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";
@@ -5846,23 +5846,21 @@ var defaultObjectOptions = {
5846
5846
  };
5847
5847
  function toPropertiesObject(declaredProps) {
5848
5848
  var keysList = Object.keys(declaredProps);
5849
- var alreadySeenKeys = {};
5849
+ var alreadySeenKeys = new Set();
5850
5850
  keysList.forEach(function (key) {
5851
- if (alreadySeenKeys[key]) {
5852
- throw fail$1(key + " is declared twice in the model. Model should not contain same keys");
5853
- }
5854
- else {
5855
- alreadySeenKeys[key] = true;
5851
+ if (alreadySeenKeys.has(key)) {
5852
+ throw fail$1(key + " is declared twice in the model. Model should not contain the same keys");
5856
5853
  }
5854
+ alreadySeenKeys.add(key);
5857
5855
  });
5858
5856
  // loop through properties and ensures that all items are types
5859
5857
  return keysList.reduce(function (props, key) {
5860
- var _a, _b, _c;
5861
5858
  // warn if user intended a HOOK
5862
- if (key in Hook)
5859
+ if (key in Hook) {
5863
5860
  throw fail$1("Hook '" + key + "' was defined as property. Hooks should be defined as part of the actions");
5861
+ }
5864
5862
  // the user intended to use a view
5865
- var descriptor = Object.getOwnPropertyDescriptor(props, key);
5863
+ var descriptor = Object.getOwnPropertyDescriptor(declaredProps, key);
5866
5864
  if ("get" in descriptor) {
5867
5865
  throw fail$1("Getters are not supported as properties. Please use views instead");
5868
5866
  }
@@ -5870,38 +5868,32 @@ function toPropertiesObject(declaredProps) {
5870
5868
  var value = descriptor.value;
5871
5869
  if (value === null || value === undefined) {
5872
5870
  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)`?");
5873
- // its a primitive, convert to its type
5874
5871
  }
5872
+ // its a primitive, convert to its type
5875
5873
  else if (isPrimitive(value)) {
5876
- return Object.assign({}, props, (_a = {},
5877
- _a[key] = optional(getPrimitiveFactoryFromValue(value), value),
5878
- _a));
5879
- // map defaults to empty object automatically for models
5874
+ props[key] = optional(getPrimitiveFactoryFromValue(value), value);
5880
5875
  }
5876
+ // map defaults to empty object automatically for models
5881
5877
  else if (value instanceof MapType) {
5882
- return Object.assign({}, props, (_b = {},
5883
- _b[key] = optional(value, {}),
5884
- _b));
5878
+ props[key] = optional(value, {});
5885
5879
  }
5886
5880
  else if (value instanceof ArrayType) {
5887
- return Object.assign({}, props, (_c = {}, _c[key] = optional(value, []), _c));
5888
- // its already a type
5889
- }
5890
- else if (isType(value)) {
5891
- return props;
5892
- // its a function, maybe the user wanted a view?
5881
+ props[key] = optional(value, []);
5893
5882
  }
5883
+ // its already a type
5884
+ else if (isType(value)) ;
5885
+ // its a function, maybe the user wanted a view?
5894
5886
  else if (devMode() && typeof value === "function") {
5895
5887
  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?");
5896
- // no other complex values
5897
5888
  }
5889
+ // no other complex values
5898
5890
  else if (devMode() && typeof value === "object") {
5899
5891
  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.");
5900
- // WTF did you pass in mate?
5901
5892
  }
5902
5893
  else {
5903
5894
  throw fail$1("Invalid type definition for property '" + key + "', cannot infer a type from a value like '" + value + "' (" + typeof value + ")");
5904
5895
  }
5896
+ return props;
5905
5897
  }, declaredProps);
5906
5898
  }
5907
5899
  /**
@@ -8611,6 +8603,7 @@ exports.resolvePath = resolvePath;
8611
8603
  exports.setLivelinessChecking = setLivelinessChecking;
8612
8604
  exports.setLivelynessChecking = setLivelynessChecking;
8613
8605
  exports.splitJsonPath = splitJsonPath;
8606
+ exports.t = types;
8614
8607
  exports.toGenerator = toGenerator;
8615
8608
  exports.toGeneratorFunction = toGeneratorFunction;
8616
8609
  exports.tryReference = tryReference;