atom
Atoms are observable state containers at the core of elementos. Atom is a vague/overloaded term in programming, but don't let them scare you, they're basically just little data stores that will notify you when their state has changed.
All state in elementos originates in atoms and then propagates outward via derived values, notifying observers along the way.
Basic
In their simplest form, we may instantiate an atom with only a default value. This will give us one default action, set
that we may use to update the atom's state.
State updates should be immutable
When setting an atom's state, we should do so immutably because internally elementos uses referential equality to memoize values. If we mutate state, elementos will not understand that the state has changed and observer effects will fail to run.
Custom Actions
Alternatively, we may define custom actions for our atom that restrict the way in which we modify its underlying state.
Observation
Atoms would be pretty useless without a way to observe them and run effects againt their state changes. observe
let's us run effects when the atom's previous state and new state are not referentially equal (compared via Object.is
).
onObserverChange
Atoms allow us to hook into information about their observers as well. Whenever an observer subscribes/unsubscribes from an atom, we will become notified via onObserverChange callbacks.