state_groups 0.2.1
state_groups: ^0.2.1 copied to clipboard
State management for the rest of us. State_groups is an easy way of managing state. In fact it doesn't manage state at all, it's stateless and only focuses on messaging.
state_groups #
Stateless state management solution #
Most state management solutions are overly convoluted because they try to do two things: manage the internal state and update the visible state. State groups, on the other hand, only updates the visible state. You have to manage the internal state yourself. This way it can be significantly simpler than other state management solutions.
To use it simply create a StateGroup and subscribe to it. You can then start notifying your StateGroup to have all listeners automatically update. It's that easy.
Usage #
Creating State Groups #
First create a state group. For this example we will create a global variable although they don't have to be global.
StateGroup exampleStateGroup = StateGroup();
You can also specify a generic type as the message type. Here we'll use void as we won't be sending any messages but if you wanted to send a message you could use an enum instead.
StateGroup<void> exampleStateGroup = StateGroup<void>();
Registering To A StateGroup #
There are two steps here. First change the state class to extend from SyncState instead of State. This will also requiring adding an additional generic type. This should be the same generic type used in the previous step We'll leave this as void for now.
class FooState extends State<Foo>
becomes
class FooState extends SyncState<void, Foo>
Then make the constructor call super with the state group you would like to subscribe to.
FooState() : super(exampleStateGroup);
Notifying A StateGroup #
Now you can notify a StateGroup whenever you want all the members of the group to update or check if they should update. To do so use the notifyAll() command like so:
exampleStateGroup.notifyAll();
And that's it. The StateGroup will handle everything else automatically.