mst-middlewares provides a collection of prebuilt middlewares for MobX-State-Tree (MST), primarily serving as examples and starting points for developers to create their own custom MST middlewares. Originally part of the `mobx-state-tree` monorepo, it was extracted into its own package to keep the core MST library small. The package is currently at version 6.1.0 and appears to have a release cadence driven by contributions and fixes, with a recent major bump to v6.0.0. Key differentiators include its direct lineage from the official MST repository, offering basic but functional examples like action logging and transactional rollbacks, and encouraging direct modification or copy-pasting of its source for specific project needs rather than relying on them as fully-fledged, heavily-supported features. Developers are encouraged to read the source code to understand how to implement custom middleware for MST's event system.
npm install mst-middlewaresVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the `atomic` middleware, which ensures that any modifications made within a synchronous or asynchronous action are rolled back if the action fails. It shows how to use `addMiddleware` on a model instance and verifies that the state (`m.z`) returns to its initial value after a `flow` action throws an error.
Review any custom middleware that inspects the type of state returned by `recorder.undo()`. Adjust logic to account for this change, as the undo operation will no longer be seen as an `UndoState`.
For critical systems, it is strongly recommended to copy and paste the source code of the desired middleware and tailor it to specific needs, rather than relying solely on the package's default implementations. This allows for full control and custom error handling/feature additions.
Always review the source code of any middleware you plan to use in production to understand its limitations and potential areas for improvement or customization for your specific application.
Ensure `mobx-state-tree` is installed (`npm install mobx-state-tree` or `yarn add mobx-state-tree`) and that `addMiddleware` or `decorate` are being called on a valid MST model instance created with `types.model.create()`.
For CommonJS projects, either configure your project to use ESM (e.g., set `"type": "module"` in `package.json` and adjust file extensions to `.js` or `.mjs`) or use a transpiler like Babel or TypeScript to convert ESM imports to CJS `require` statements during build. Modern Node.js versions support ESM directly when `"type": "module"` is set.