Reactive State & Event Bus
BareMetal.state methods
The state manager is accessible via BareMetal.state (or its alias BareMetal.events in some contexts). It manages a reactive store that binds cleanly to your DOM.
state.init(key, defaultValue)
Initializes a piece of state. If the key already exists, it is ignored.
state.update(key, value)
Updates a state variable and synchronously triggers all callbacks subscribed to this specific key.
state.get(key)
Returns the current synchronous value of the state key.
state.subscribe(key, callback)
Subscribes a function to changes on a specific state key.
Returns: A function that, when called, unsubscribes the callback.
Note: The callback is invoked immediately upon subscription with the current value.
state.publish(event, payload)
Fires a standard, one-off Pub/Sub event across the application.
state.on(event, callback)
Listens for a Pub/Sub event.
Returns: An unsubscribe function.