Gathering detailed insights and metrics for mst-middlewares
Gathering detailed insights and metrics for mst-middlewares
Gathering detailed insights and metrics for mst-middlewares
Gathering detailed insights and metrics for mst-middlewares
mobx-devtools-mst
Allows debugging mobx-state-tree roots. See [mobx-devtools/README.md](https://github.com/mobxjs/mobx-devtools/blob/master/README.md#mobx-state-tree).
mobx-state-tree
Opinionated, transactional, MobX powered state container
mst-persist
Persist and hydrate MobX-state-tree stores
@atproto/repo
atproto repo and MST implementation
npm install mst-middlewares
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5 Stars
11 Commits
2 Forks
1 Watching
1 Branches
3 Contributors
Updated on 30 Aug 2024
TypeScript (97.1%)
JavaScript (2.9%)
Cumulative downloads
Total Downloads
Last day
5.3%
2,080
Compared to previous day
Last week
5.7%
10,810
Compared to previous week
Last month
7.2%
43,606
Compared to previous month
Last year
-22.1%
648,936
Compared to previous year
Migrated from this commit in MobX-State-Tree in the work to undo the monorepo set up for MST.
The MST package ships with some prebuilt middlewares, which serves mainly as examples on how to write your own middleware. The source of each middleware can be found in this github directory, you are encouraged to read them!
The middlewares are bundled separately to keep the core package small, and can be included using:
1import { MiddlewareName } from "mst-middlewares";
The middlewares serve as example and are supported on a best effort bases. The goal of these middlewares is that if they are critical to your system, you can simply copy paste them and further tailor them towards your specific needs.
For the exact description of all middleware events offered by MST, see the api docs
Feel free to contribute to these middlewares and improve them on your experience. The middlewares must be written in TypeScript. Any additional test for your middleware should be written inside the test folder
This is the most basic of middlewares: It logs all direct action invocations. Example:
1import { simpleActionLogger } from "mst-middlewares"; 2 3// .. type definitions ... 4 5const store = Store.create({ 6 todos: [{ title: "test " }], 7}); 8 9mst.addMiddleware(store, simpleActionLogger); 10 11store.todos[0].setTitle("hello world"); 12 13// Prints: 14// [MST] /todos/0/setTitle
For a more sophisticated logger, see action-logger which also logs process invocations and continuations
This is a little more sophisticated middlewares: It logs all direct action invocations and also every flow that spawns, returns or throws. Example:
1import { actionLogger } from "mst-middlewares"; 2 3// .. type definitions ... 4 5const store = Store.create({ 6 todos: [{ title: "test " }], 7}); 8 9mst.addMiddleware(store, actionLogger); 10 11store.todos[0].setTitle("hello world");
This will print something like `[MST]
[MST] #5 action - /todos/0/setTitle
[MST] #5 flow_spawn - /todos/0/setTitle
[MST] #5 flow_spawn - /todos/0/helper2
[MST] #5 flow_return - /todos/0/helper2
[MST] #5 flow_return - /todos/0/setTitle
The number ("#5"
) indicates the id of the original action invocation that lead directly or indirectly to the flow being spawned.
For more details on the meaning of the action types see the middleware docs.
This middleware rolls back if a synchronous or asynchronous action process fails.
The exception itself is not eaten, but any modifications that are made during the sync/async action will be rollback, by reverse applying any pending patches. Can be connected to a model by using either addMiddleware
or decorate
Example:
1import { types, addMiddleware, flow } from "mobx-state-tree"; 2import { atomic } from "mst-middlewares"; 3 4const TestModel = types 5 .model({ 6 z: 1, 7 }) 8 // example with addMiddleware 9 .actions((self) => { 10 addMiddleware(self, atomic); 11 12 return { 13 inc: flow(function* (x) { 14 yield delay(2); 15 self.z += x; 16 yield delay(2); 17 self.z += x; 18 throw "Oops"; 19 }), 20 }; 21 }) 22 // example with decorate 23 .actions((self) => { 24 return { 25 inc: decorate( 26 atomic, 27 flow(function* (x) { 28 yield delay(2); 29 self.z += x; 30 yield delay(2); 31 self.z += x; 32 throw "Oops"; 33 }) 34 ), 35 }; 36 }); 37 38const m = TestModel.create(); 39m.inc(3).catch((error) => { 40 t.is(error, "Oops"); 41 t.is(m.z, 1); // Not 7! The change was rolled back 42});
This built in model can be used as stand alone store or as part of your state tree and adds time travelling capabilities. It records all emitted snapshots by a tree and exposes the following methods / views:
canUndo: boolean
canRedo: boolean
undo()
redo()
history
: array with all recorded statesThe state of the TimeTraveller itself is stored in a Mobx state tree, meaning that you can freely snapshot your state including its history. This means that it is possible to store your app state including the undo stack in for example local storage. (but beware that stringify-ing will not benefit from structural sharing).
Usage inside a state tree:
1import { TimeTraveller } from "mst-middlewares"; 2 3export const Store = types.model({ 4 todos: types.array(Todo), 5 history: types.optional(TimeTraveller, { targetPath: "../todos" }), 6}); 7 8const store = Store.create(); 9 10// later: 11if (store.history.canUndo) store.history.undo(); 12// etc
Note that the targetPath
is a path relative to the TimeTraveller
instance that will indicate which part of the tree will be snapshotted. Please make sure the targetPath doesn't point to a parent of the time traveller, as that would start recording it's own history..... In other words, targetPath: "../"
-> Boom💥
To instantiate the TimeTraveller
as a stand-alone state tree, pass in the the store through context:
1import { TimeTraveller } from "mst-middlewares"; 2 3export const Store = types.model({ 4 todos: types.array(Todo), 5}); 6 7const store = Store.create(); 8const timeTraveller = TimeTraveller.create({}, { targetStore: store }); 9 10// later: 11if (timeTraveller.canUndo) timeTraveller.undo(); 12// etc
The UndoManager
is the more fine grained TimeTraveller
.
Because it records patches instead of snapshots, it is better at dealing with concurrent and asynchronous processes.
The differences to the TimeTraveller
make it useful to implement end-user undo / redo.
For an in-depth explanation why undo / redo should be patch, not snapshot, based, check out the second half of the React Next talk: MobX-state-tree, React but for data
Differences to the TimeTraveller
:
undo
/ redo
applies all inverted patches / patches for a recorded action / process instead of snapshots.API:
history: { patches: [], inversePatches [] }[]
canUndo: boolean
true if there is at least one undo level availablecanRedo: boolean
true if there is at least one redo level availableundoLevels: number
number of undo levels availableredoLevels: number
number of redo levels availableundo()
undo the last operationredo()
redo the last operationwithoutUndo(() => fn)
patches for actions / processes within the fn are not recorded.withoutUndoFlow(fn*)
patches the fn* are not recorded.startGroup(() => fn)
can be used to start a group, all patches within a group are saved as one history entry.stopGroup()
can be used to stop the recording of patches for the grouped history entry.clear({ undo?: true, redo: true }?)
clear the history.Setup and API usage examples:
The setup is very similar to the one of the TimeTraveller
.
The UndoManager
automatically records all the actions within the tree it is attached to.
If you want the history to be a part of your store:
1import { UndoManager } from "mst-middlewares"; 2 3export const Store = types 4 .model({ 5 todos: types.array(Todo), 6 history: types.optional(UndoManager, {}), 7 }) 8 .actions((self) => { 9 // you could create your undoManger anywhere but before your first needed action within the undoManager 10 setUndoManager(self); 11 12 return { 13 addTodo(todo) { 14 self.todos.push(todo); 15 }, 16 // to use the undoManager to wrap the afterCreate action 17 // of the StoreModel it's necessary to set it within the store model like above 18 // afterCreate: () => undoManager.withoutUndo(() => { action() }) 19 }; 20 }); 21 22export let undoManager = {}; 23export const setUndoManager = (targetStore) => { 24 undoManager = targetStore.history; 25}; 26const store = Store.create();
To record the changes into a separate tree:
1import { UndoManager } from "mst-middlewares"; 2 3export const Store = types 4 .model({ 5 todos: types.array(Todo), 6 }) 7 .actions((self) => { 8 // you could create your undoManger anywhere but before your first needed action within the undoManager 9 setUndoManager(self); 10 11 return { 12 addTodo(todo) { 13 self.todos.push(todo); 14 }, 15 // to use the undoManager to wrap the afterCreate action 16 // of the StoreModel it's necessary to set it within the store model like above 17 // afterCreate: () => undoManager.withoutUndo(() => { action() }) 18 }; 19 }); 20 21export let undoManager = {}; 22export const setUndoManager = (targetStore) => { 23 undoManager = UndoManager.create({}, { targetStore }); 24}; 25const store = Store.create();
If you want a limited history of actions to be recorded, for example to keep track of the last 10 actions only, you must provide a maxHistoryLength
value through environment data.
1// with history in your store 2const store = Store.create({}, { maxHistoryLength: 10 }); 3 4// with history in a separate store 5export const setUndoManager = (targetStore) => { 6 undoManager = UndoManager.create({}, { targetStore, maxHistoryLength: 10 }); 7};
If you want to record changes in lifecycle hooks as well, you must provide an includeHooks
flag:
1import { UndoManager } from "mst-middlewares"; 2 3export const Store = types 4 .model({ 5 todos: types.array(Todo), 6 }) 7 .actions((self) => { 8 setUndoManager(self); 9 10 return { 11 afterCreate() { 12 self.todos.push({ title: "New Todo" }); 13 }, 14 addTodo(todo) { 15 self.todos.push(todo); 16 }, 17 }; 18 }); 19 20export let undoManager = {}; 21export const setUndoManager = (targetStore) => { 22 undoManager = UndoManager.create({}, { targetStore, includeHooks: true }); 23}; 24const store = Store.create();
Undo/ Redo:
1import { undoManager } from "../Store"; 2 3// if the undoManger is created within another tree 4const undo = () => undoManager.canUndo && undoManager.undo(); 5const redo = () => undoManager.canRedo && undoManager.redo();
WithoutUndo - within a react component:
1import {undoManger} from '../Store' 2 3... 4 5setPersonName = () => { 6 // the action setPersonName won't be saved onto the history, you could add more than one action. 7 undoManger.withoutUndo(() => store.setPersonName('firstName', 'lastName')) 8} 9 10render() { 11 return ( 12 <div onClick={this.setPersonName}> 13 SetPersonName 14 </div> 15 ) 16} 17 18...
WithoutUndo - declarative:
1import { types } from "mobx-state-tree"; 2import { UndoManager } from "mst-middlewares"; 3 4const PersonModel = types 5 .model("PersonModel", { 6 firstName: types.string, 7 lastName: types.string, 8 }) 9 .actions((self) => { 10 return { 11 // setPersonName won't be recorded anymore in general 12 setPersonName: (firstName, lastName) => 13 undoManager.withoutUndo(() => { 14 self.firstName = firstName; 15 self.lastName = lastName; 16 }), 17 }; 18 }); 19 20const StoreModel = types 21 .model("StoreModel", { 22 persons: types.map(PersonModel), 23 }) 24 .actions((self) => { 25 setUndoManager(self); 26 27 return { 28 addPerson(firstName, lastName) { 29 persons.put({ firstName, lastName }); 30 }, 31 }; 32 }); 33 34export let undoManager = {}; 35export const setUndoManager = (targetStore) => { 36 undoManager = UndoManager.create({}, { targetStore }); 37}; 38export const Store = StoreModel.create({});
WithoutUndoFlow - declarative:
1import {undoManager} from './Store/' 2 3... 4 5.actions(self => { 6 function updateBooks(json) { 7 self.books.values().forEach(book => (book.isAvailable = false)) 8 json.forEach(bookJson => { 9 self.books.put(bookJson) 10 self.books.get(bookJson.id).isAvailable = true 11 }) 12 } 13 14 function* loadBooks() { 15 try { 16 const json = yield self.shop.fetch("/books.json") 17 updateBooks(json) 18 } catch (err) { 19 console.error("Failed to load books ", err) 20 } 21 } 22 23 return { 24 loadBooks: () => undoManager.withoutUndoFlow(loadBooks)() 25 // same as: undoManager.withoutUndo(() => flow(loadBooks))() 26 } 27})
StartGroup, StopGroup - within a react component:
1import {undoManager} from '../Store'
2...
3handleStop = (mousePosition, { dx, dy }) => {
4 this.stopTrackingDrag();
5 undoManager.stopGroup();
6}
7
8handleDrag = (mousePosition, { dx, dy }) => {
9 const { view, parentNode } = this.props;
10 // only one history entry will be created for the whole dragging
11 // therefore all patches will be merged to one history entry while the group is active
12 undoManager.startGroup(() =>
13 parentNode.moveSelectedNodes({ dx: dx / view.zoom, dy: dy / view.zoom })
14 );
15}
16...
The Redux 'middleware' is not literally middleware, but provides two useful methods for Redux interoperability:
asReduxStore(mstStore, middlewares?)
creates a tiny proxy around a MST tree that conforms to the redux store api.
This makes it possible to use MST inside a redux application.
See the redux-todomvc example for more details.
connectReduxDevtools(remoteDevDependency, mstStore, options?)
connects a MST tree to the Redux devtools. Pass in the remoteDev
dependency to set up the connect (only one at a time). See this example for a setup example.
The options object is optional and has the following options:
logIdempotentActionSteps
: true
by default due to possible performance penalty because of the internal usage of onPatch. When set to false
it will skip reporting of actions and flow action "steps" that do not end up in an actual change in the model (except when an error is thrown), thus reducing the amount of noise in the logs.logChildActions
: false
by default. When set to true
it will report actions that are executed inside a root actions. When set to false
it will not.logArgsNearName
: true
by default. When true
it will log the arguments near the action name (truncated if too long), when false
it won't.No vulnerabilities found.
No security vulnerabilities found.