mobxjs / mobx.dart

MobX for the Dart language. Hassle-free, reactive state-management for your Dart and Flutter apps.
https://mobx.netlify.app
MIT License
2.4k stars 310 forks source link

Separate State from Actions #883

Closed leon-volq closed 1 year ago

leon-volq commented 1 year ago

Hello, I wonder if it's possible to organize Mobx as Actions<->View<->State

I would like to see my state as a single object, or single Observable state tree. And access it from actions completely separated from the state.

Multiple stores have been the source of all my problems since React 0.11, and having actions inside the Store makes them more like Controllers with Models, at least I feel it like that.


  // Somehow create a single observable tree
  Map SingleStateTree = {
    'user': {
      'username': 'rambo',
      'loggedIn': true,
    },
    'twitts': [
      { 'user': 'rambo', 'text': 'hello world' }
    ],
  };

  // actions can change whatever nested state in our single state tree
  void action() {
    Map newTwitt = { 'user': 'mobx', 'text': 'its cool!' };
    // This reports changes to observables
    SingleStateTree['twitts'].add(newTwitt);
  }

  // Observers work as they normally do
  // ...

Hopefully, this makes sense, I want to see my state as a single object with nested objects, and not as a Store with nested Stores that need to communicate between them, if actions are separated and have access to all state they don't need to.

Maybe I'm asking too much from dart without mirrors but maybe there's a way, or maybe it's completely possible with a bit of extra boilerplate and I'm just missing it XD.

kind of like Baobab.js, Cerebral.js or Overmind.js

Thank you very much, Mobx seems very good!

altynbek132 commented 1 year ago

Maybe you should use Observable collections such ObservableMap, List?

leon-volq commented 1 year ago

@altynbek132 I ended up doing a Store Class with more Store Class inside of it

It is extremely verbose compared to javascript similar approaches but it does what its suppose to do :)

Closing the issue since I don't think there is an easy way to do this now that I now more about Mobx dart