Core Principles
The Elm Architecture is based on three core principles:- Single Source of Truth: All application state lives in one place (the Model)
- Unidirectional Data Flow: Data flows in one direction through the application
- Pure Functions: State changes are predictable and testable
The Model-Update-View Cycle
Every Bubble Tea application follows this cycle:Model
Your application’s state. This is the single source of truth for what your app knows and displays.
View
A pure function that takes the model and returns what to display. The view never modifies the model.
Unidirectional Data Flow
Data flows in one direction through your application: This unidirectional flow means:- The View never modifies the Model directly
- The Model never calls the View directly
- All state changes go through Update
- Side effects are isolated in Commands
Architecture Diagram
Here’s how the components work together:How It Works
1. Initialization
When your program starts, Bubble Tea callsInit() on your model:
2. The Update Loop
When something happens (a key press, timer tick, HTTP response), Bubble Tea:- Wraps it in a
Msg(message) - Passes it to
Update()along with the current model - Gets back a new model and optional command
- Calls
View()with the new model - Renders the result
3. Rendering
TheView() method is called after every update:
Benefits
Predictable
Same input always produces same output. No hidden state changes.
Testable
Pure functions are easy to test. No mocking required for most logic.
Debuggable
Clear sequence of events. Easy to trace how state changes.
Composable
Models, updates, and views can be composed from smaller pieces.
Example: Counter Application
Here’s a complete example showing the architecture in action:Why The Elm Architecture?
Eliminates Race Conditions
Eliminates Race Conditions
Since all state changes go through Update, you can’t have two parts of your code modifying state simultaneously.
Makes Time Travel Debugging Possible
Makes Time Travel Debugging Possible
Because updates are pure functions, you can replay the exact sequence of messages to reproduce any state.
Simplifies Concurrent Operations
Simplifies Concurrent Operations
Commands handle I/O concurrently, but results come back as messages through the same Update function.
Works Well with Go
Works Well with Go
The pattern maps naturally to Go’s strengths: simple types, clear interfaces, and explicit error handling.
Key Takeaways
The Elm Architecture gives you:
- One way to change state (through Update)
- One place to handle events (the Update function)
- One source of truth (the Model)
Next Steps
Model
Learn about the Model interface and how to structure your application state
Messages
Discover how messages flow through your application
Commands
Understand how to perform I/O operations
Views
Master rendering your application’s UI