State objects emit an event whenever a property gets written to on their associated Proxy.
By default, state changes are only queued up and processed in a microtask, allowing for batch-processing.
This means that for repeated changes to the same property, the user can decide to process all the changes in one go or even discard everything but the last value of each property.
That way one can easily avoid some types of unnecessary redraws, network requests, etc.
To make code more readable, the constructor defaults to adding an event listener that checks for a method called [prop]Changed
in the state object, and calls it with the new value. This behaviour can be disabled by setting the methods
option to false.
The state object also has a getter and setter pair for the `state` attribute, which simply accesses this same attribute on the proxy object. This is simply a convenience feature to save some typing on single-variable states.
defer
methods
A simple counter state that prints a new count to the console every time it gets updated.
This example uses an event listener instead to get notified of all property changes. Collecting changes into a map is an easy way of de-duplicating their values and keeping only the last one.