mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 641 forks source link

Make `preSnapshot` and `postSnapshot` actions in middleware ? #1861

Open nspaeth opened 2 years ago

nspaeth commented 2 years ago

Feature request Make implementing preSnapshot and postSnapshot implementable by middleware.

Is your feature request related to a problem? Please describe.

  1. I have some models that describe user files. In order to serialize this data I need to access some global state that I might want to redefine depending on the environment. (E.x., A database interface, a file interface). In order to do this now I would have to set up a complicated set of model builders that take my interface as arguments and build the models.
  2. Sometimes I would like to serialize/deserialize a node based on information in the parent.
  3. It would be nice to be able to globally change how serialization is handled for other reasons. For example, I have a lot of models that are optional and those are automatically serialized as null when empty instead of just being omitted. With a middleware I could globally change that behavior without having to add processors to every model or request the change in MST.

See also: #662

Describe the solution you'd like Currently, middleware does not get the preSnapshot or postSnapshot actions.

  1. Report those actions to middleware.
  2. Use the snapshot returned by the middleware to serialize/deserialize.

Describe alternatives you've considered

  1. Manually applying pre/post snapshot to every model.
  2. Generating models in functions.

Are you willing to (attempt) a PR?

coolsoftwaretyler commented 1 year ago

Hey @nspaeth - I am sorry it took so long for us to get back to you here.

I think this is a good idea, although I'm not exactly sure where it'll land on the roadmap. I am going to mark this issue as PR welcome, but if you've moved on from the thought, I totally understand. It's been almost eighteen months!

Let me know if you're interested in putting something like this together. If not, I'll leave it open for future contributors who have similar needs and want a starting point.

zuhorer commented 1 year ago

issue moved to : https://github.com/coolsoftwaretyler/mst-middlewares/issues/6

coolsoftwaretyler commented 1 year ago

^ Sorry, that's my bad for miscommunicating. This issue is actually an mst feature, so we will not be moving it. Closing over there, leaving open here.