How Mobius state management works
masterMobius manages application state through a functional reactive loop consisting of four core concepts:
- Model: A data structure representing a snapshot of your application state (e.g., an
Int, astruct, or anenum). - Events: Messages sent to the framework to request a state change (e.g., an
enumrepresenting user actions). - Update Function: A pure function that takes the current
Modeland anEventand returns the next state. To handle side effects, it returns aNext<Model, Effect>type. - Effects & Effect Handlers: Effects are descriptions of side effects to be performed (e.g., playing a sound, network calls). Effect Handlers are the components that actually execute these effects.
By separating the logic of what should happen (the update function) from how it happens (the Effect Handler), Mobius ensures high testability and separation of concerns.
// Example of the core loop structure
let application = Mobius.loop(update: update, effectHandler: effectHandler)
.start(from: initialModel)
application.dispatchEvent(.someEvent)