BareMetal.js

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.init('counter', 0);

state.update(key, value)

Updates a state variable and synchronously triggers all callbacks subscribed to this specific key.

state.update('counter', 1);

state.get(key)

Returns the current synchronous value of the state key.

const currentCount = state.get('counter');

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.

const unsub = state.subscribe('counter', (value) => { document.getElementById('count-display').innerText = value; }); // Unsubscribe later to avoid memory leaks unsub();

state.publish(event, payload)

Fires a standard, one-off Pub/Sub event across the application.

state.publish('USER_LOGIN', { username: 'Admin' });

state.on(event, callback)

Listens for a Pub/Sub event.

Returns: An unsubscribe function.

const unsubEvent = state.on('USER_LOGIN', (payload) => { console.log('Logged in as', payload.username); });