The Dispatcher in Flux Context
Imagine living in a small town or community where information and events of public interest events are disseminated via a “town crier.”
I grew up in one. One major and recurring event back then was the “community environmental sanitation.” The community governing council picked a Saturday in which all commercial activities were put on-hold until 10am, and all youths and able adults came out en masse to participate in a cleaning of their surroundings and a repair of the public facilities.
So when a future date for such an activity was decided by the council, the “town crier” DISPATCEHD the information thusly:
“People of XYZ town, the community council has…”
- The town crier is non-selective in dispatching this information; its a public broadcast.
- If you were a youth in this community, the information would mean something to you and you would likely take an action — say, mark your calendar.
- If you were a child like I was then, the information would mean nothing to you, so you would tend to ignore it and play on.
In Flux context, the “dispatcher” can be likened to the “town crier”.
In React-Flux, we can think about it this way:
- The community council is the view.
- The decision/information from the council is the action.
- The medium the community council uses to pass information(action) to the town crier is the action creator.
- The piece of information the town crier disseminates to community members is a structures representation of the action.
- The town crier is the dispatcher.
- And the community members can be likened to the stores.
Note that in the small town picture we’ve painted:
- The town crier broadcasts information within the geographic boundaries of the community. Similarly in Flux, the dispatcher exposes a register method and all functions passed to the dispatcher via this register method defines the bounds of the broadcast.
- To avoid misunderstanding, the information/action being broadcast would at minimum, be clear on:
a.) What information it is (Type) and
b.) Any other thing that needs to be known about it (payload)
Action in Flux
Similar to the town crier’s information, an action in Flux is “expected” to convey Type and Payload. Note that this “expectation” is not enforced in any way. However, it is advised that you ensure this expectation is met to avoid headaches that may arise when you use any flux-oriented library that may have some logic strongly dependent on this expectation.
To begin, here are some object literals demonstrating sample action structures:
Let’s build our dispatcher:
Now that we have a dispatcher, lets build a simple action creator for passing payloads to be dispatched as actions to the dispatcher.
Yes, we kept it basic, but it should give the clear understanding needed to dig further and dance the Flux dance in your next project.