Skip to content

Commit

Permalink
Merge 43b5c69 into 8b8e839
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Mar 7, 2019
2 parents 8b8e839 + 43b5c69 commit 552bf76
Show file tree
Hide file tree
Showing 22 changed files with 589 additions and 309 deletions.
5 changes: 2 additions & 3 deletions README.md
Expand Up @@ -983,8 +983,7 @@ Note that since MST v3 `types.array` and `types.map` are wrapped in `types.optio
## Utility types

- `types.union(options?: { dispatcher?: (snapshot) => Type, eager?: boolean }, types...)` create a union of multiple types. If the correct type cannot be inferred unambiguously from a snapshot, provide a dispatcher function to determine the type. When `eager` flag is set to `true` (default) - the first matching type will be used, if set to `false` the type check will pass only if exactly 1 type matches.
- `types.optional(type, defaultValue)` marks an value as being optional (in e.g. a model). If a value is not provided the `defaultValue` will be used instead. If `defaultValue` is a function, it will be evaluated. This can be used to generate, for example, IDs or timestamps upon creation.
- `types.optionalNull(type, defaultValue)` is like `types.optional` except that it uses `null` rather than `undefined` to decide when to use a default value.
- `types.optional(type, defaultValue, optionalValues?)` marks an value as being optional (in e.g. a model). If a value is not provided/`undefined` (or set to any of the primitive values passed as an optional `optionalValues` array) the `defaultValue` will be used instead. If `defaultValue` is a function, it will be evaluated. This can be used to generate, for example, IDs or timestamps upon creation.
- `types.literal(value)` can be used to create a literal type, where the only possible value is specifically that value. This is very powerful in combination with `union`s. E.g. `temperature: types.union(types.literal("hot"), types.literal("cold"))`.
- `types.enumeration(name?, options: string[])` creates an enumeration. This method is a shorthand for a union of string literals. If you are using typescript and want to create a type based on an string enum (e.g. `enum Color { ... }`) then use `types.enumeration<Color>("Color", Object.values(Color))`, where the `"Color"` name argument is optional.
- `types.refinement(name?, baseType, (snapshot) => boolean)` creates a type that is more specific than the base type, e.g. `types.refinement(types.string, value => value.length > 5)` to create a type of strings that can only be longer then 5.
Expand Down Expand Up @@ -1167,7 +1166,7 @@ The following service can generate MST models based on JSON: https://transform.n

### `optionals` and default value functions

`types.optional` and `types.optionalNull` can take an optional function parameter which will be invoked each time a default value is needed. This is useful to generate timestamps, identifiers or even complex objects, for example:
`types.optional` can take an optional function parameter which will be invoked each time a default value is needed. This is useful to generate timestamps, identifiers or even complex objects, for example:

`createdDate: types.optional(types.Date, () => new Date())`

Expand Down
2 changes: 1 addition & 1 deletion changelog.md
@@ -1,4 +1,4 @@
- Added `types.optionalNull` through [#1188](https://github.com/mobxjs/mobx-state-tree/pull/1188) by [@xaviergonz](https://github.com/xaviergonz)
- Added an optional third argument to `types.optional` that allows to set alternative optional values other than just `undefined` through [#1192](https://github.com/mobxjs/mobx-state-tree/pull/1192) by [@xaviergonz](https://github.com/xaviergonz)
- Fixed detaching arrays/maps killing their children [#1173](https://github.com/mobxjs/mobx-state-tree/issues/1173) through [#1175](https://github.com/mobxjs/mobx-state-tree/pull/1175) by [@xaviergonz](https://github.com/xaviergonz)
- Added `types.snapshotProcessor` [#947](https://github.com/mobxjs/mobx-state-tree/issues/947) through [#1165](https://github.com/mobxjs/mobx-state-tree/pull/1165) by [@xaviergonz](https://github.com/xaviergonz). This feature will eventually deprecate `postProcessSnapshot` and `preProcessSnapshot` from models in a next major version.
- Performance improvement for event handlers so they consume less RAM through [#1160](https://github.com/mobxjs/mobx-state-tree/pull/1160) by [@xaviergonz](https://github.com/xaviergonz)
Expand Down
81 changes: 50 additions & 31 deletions docs/API/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/mobx-state-tree/__tests__/core/api.test.ts
Expand Up @@ -95,7 +95,6 @@ const TYPES = stringToArray(`
safeReference,
union,
optional,
optionalNull,
literal,
maybe,
maybeNull,
Expand Down
8 changes: 4 additions & 4 deletions packages/mobx-state-tree/__tests__/core/jsonpatch.test.ts
Expand Up @@ -63,7 +63,7 @@ test("it should apply deep patches to arrays", () => {
Node,
{ id: 1, children: [{ id: 2 }] },
(n: Instance<typeof Node>) => {
const children = n.children as Instance<typeof Node>[]
const children = (n.children as unknown) as Instance<typeof Node>[]
children[0].text = "test" // update
children[0] = cast({ id: 2, text: "world" }) // this reconciles; just an update
children[0] = cast({ id: 4, text: "coffee" }) // new object
Expand Down Expand Up @@ -112,7 +112,7 @@ test("it should apply deep patches to arrays with object instances", () => {
Node,
{ id: 1, children: [{ id: 2 }] },
(n: Instance<typeof Node>) => {
const children = n.children as Instance<typeof Node>[]
const children = (n.children as unknown) as Instance<typeof Node>[]
children[0].text = "test" // update
children[0] = Node.create({ id: 2, text: "world" }) // this does not reconcile, new instance is provided
children[0] = Node.create({ id: 4, text: "coffee" }) // new object
Expand Down Expand Up @@ -150,7 +150,7 @@ test("it should apply non flat patches", () => {
Node,
{ id: 1 },
(n: Instance<typeof Node>) => {
const children = n.children as Instance<typeof Node>[]
const children = (n.children as unknown) as Instance<typeof Node>[]
children.push(
cast({
id: 2,
Expand Down Expand Up @@ -188,7 +188,7 @@ test("it should apply non flat patches with object instances", () => {
Node,
{ id: 1 },
(n: Instance<typeof Node>) => {
const children = n.children as Instance<typeof Node>[]
const children = (n.children as unknown) as Instance<typeof Node>[]
children.push(
Node.create({
id: 2,
Expand Down

0 comments on commit 552bf76

Please sign in to comment.