You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+87-8Lines changed: 87 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -13,11 +13,11 @@
13
13
14
14
## Why?
15
15
16
-
[`mobx-state-tree`](https://mobx-state-tree.js.org/) is great for modeling data and observing changes to it, but it adds a lot of runtime overhead! Raw `mobx` itself adds substantial overhead over plain old JS objects or ES6 classes, and `mobx-state-tree` adds more on top of that. If you want to use your MST data models in a non-reactive or non-observing context, all that runtime overhead for observability is just wasted, as nothing is everchanging.
16
+
[`mobx-state-tree`](https://mobx-state-tree.js.org/) is great for modeling data and observing changes to it, but it adds a lot of runtime overhead! Raw `mobx` itself adds substantial overhead over plain old JS objects or ES6 classes, and `mobx-state-tree` adds more on top of that. If you want to use your MST data models in a non-reactive or non-observing context, all that runtime overhead for observability is just wasted, as nothing is ever-changing.
17
17
18
18
`mobx-quick-tree` implements the same API as MST and exposes the same useful observable instances for use in observable contexts, but adds a second option for instantiating a read-only instance that is 100x faster.
19
19
20
-
If `mobx-state-tree` instances are great for modeling within an "editor" part of an app where nodes and properties are changed all over the place, the performant, read-only instances constructed by `mobx-quick-tree` are great for using within a "read" part of an app that displays data in the tree without ever changing it. For a website builder for example, you might use MST in the page builder area where someone arranges components within a page, and then use MQT in the part of the app that needs to render those webpages frequently.
20
+
If `mobx-state-tree` instances are great for modeling within an "editor" part of an app where nodes and properties are changed all over the place, the performant, read-only instances constructed by `mobx-quick-tree` are great for use within a "read" part of an app that displays data in the tree without ever changing it. For a website builder for example, you might use MST in the page builder area where someone arranges components within a page, and then use MQT in the part of the app that needs to render those web pages frequently.
21
21
22
22
### Two APIs
23
23
@@ -164,7 +164,7 @@ class Car extends ClassModel({
164
164
}
165
165
```
166
166
167
-
Each Class Model **must** be registered with the system using the `@register` decorator in order to be instantiated.
167
+
Each Class Model **must** be registered with the system using the `@register` decorator to be instantiated.
168
168
`@register` is necessary for setting up the internal state of the class and generating the observable MST type.
169
169
170
170
Within Class Model class bodies, refer to the current instance using the standard ES6/JS `this` keyword. `mobx-state-tree` users tend to use `self` within view or action blocks, but Class Models return to using standard JS `this` for performance.
@@ -284,11 +284,11 @@ class Car extends ClassModel({
284
284
}
285
285
```
286
286
287
-
Explicit decoration of views is exactly equivalent to implicit declaration of views without a decorator.
287
+
Explicit decoration of views is exactly equivalent to an implicit declaration of views without a decorator.
288
288
289
289
#### Defining actions with `@action`
290
290
291
-
Class Models support actions on instances, which are functions that change state on the instance or it's children. Class Model actions are exactly the same as `mobx-state-tree` actions defined using the `.actions()` API on a `types.model`. See the [`mobx-state-tree` actions docs](https://mobx-state-tree.js.org/concepts/actions) for more information.
291
+
Class Models support actions on instances, which are functions that change the state of the instance or its children. Class Model actions are exactly the same as `mobx-state-tree` actions defined using the `.actions()` API on a `types.model`. See the [`mobx-state-tree` actions docs](https://mobx-state-tree.js.org/concepts/actions) for more information.
292
292
293
293
To define an action on a Class Model, define a function within a Class Model body, and register it as an action with `@action`.
294
294
@@ -433,6 +433,85 @@ watch.stop();
433
433
434
434
**Note**: Volatile actions will _not_ trigger observers on readonly instances. Readonly instances are not observable because they are readonly (and for performance), and so volatiles aren't observable, and so volatile actions that change them won't fire observers. This makes volatile actions appropriate for reference tracking and implementation that syncs with external systems, but not for general state management. If you need to be able to observe state, use an observable instance.
435
435
436
+
#### Caching view values in snapshots with `snapshottedView`
437
+
438
+
For expensive views, `mobx-quick-tree` supports caching the computed value of the view from an observable instance in the snapshot. This allows the read-only instance to skip re-computing the cached value, and instead return a cached value from the snapshot quickly.
439
+
440
+
To cache a view's value in the snapshot, define a view with the `@snapshottedView` decorator. Each `getSnapshot` call of your observable instance will include the view's result in the snapshot under the view's key.
const car =Car.create({ make: "Toyota", model: "Prius", year: 2008 });
459
+
460
+
// get a snapshot of the observable instance
461
+
const snapshot =getSnapshot(car);
462
+
463
+
// the snapshot will include the cached view value
464
+
snapshot.name; // => "2008 Toyota Prius"
465
+
```
466
+
467
+
Snapshotted views can also transform the value of the view before it is cached in the snapshot. To transform the value of a snapshotted view, pass a function to the `@snapshottedView` decorator that takes the view's value and returns the transformed value.
468
+
469
+
For example, for a view that returns a rich type like a `URL`, we can store the view's value as a string in the snapshot, and re-create the rich type when a read-only instance is created::
Snapshotted views are a complicated beast, and are best avoided until your performance demands less computation on readonly instances.
500
+
501
+
On observable instances, snapshotted views go through the following lifecycle:
502
+
503
+
- when an observable instance is created, any view values in the snapshot are _ignored_
504
+
- like mobx-state-tree, view values aren't computed until the first time they are accessed. On observable instances, snapshotted views will _always_ be recomputed when accessed the first time
505
+
- once a snapshotted view is computed, it will follow the standard mobx-state-tree rules for recomputation, recomputing when any of its dependencies change, and only observing dependencies if it has one or more observers of it's own
506
+
- when the observable instance is snapshotted via `getSnapshot` or an observer of the snapshot, the view will always be computed, and the view's value will be JSON serialized into the snapshot
507
+
508
+
On readonly instances, snapshotted views go through the following lifecycle:
509
+
510
+
- when a readonly instance is created, any snapshotted view values in the snapshot are memoized and stored in the readonly instance
511
+
- snapshotted views are never re-computed on readonly instances, and their value is always returned from the snapshot if present
512
+
- if the incoming snapshot does not have a value for the view, then the view is lazily computed on first access like a normal `@view`, and memoized forever after that
513
+
- when a readonly instance is snapshotted via `getSnapshot` or an observer of the snapshot, the view will be computed if it hasn't been already, and the view's value will be JSON serialized into the snapshot
514
+
436
515
#### References to and from class models
437
516
438
517
Class Models support `types.references` within their properties as well as being the target of `types.reference`s on other models or class models.
@@ -588,7 +667,7 @@ const buildClass = () => {
588
667
someView: view,
589
668
someAction: action,
590
669
},
591
-
"Example"
670
+
"Example",
592
671
);
593
672
};
594
673
@@ -728,7 +807,7 @@ class Student extends addName(
728
807
firstName:types.string,
729
808
lastName:types.string,
730
809
homeroom:types.string,
731
-
})
810
+
}),
732
811
) {}
733
812
734
813
@register
@@ -737,7 +816,7 @@ class Teacher extends addName(
0 commit comments