react-eventsync
Version:
React-compatible global state management via DOM event streams. No context, no prop-drilling, no re-render headaches.
32 lines (24 loc) • 800 B
JavaScript
// src/store.js
// Global state logic for eventsync
import { getByPath, setByPath } from './utils.js';
import { dispatchEvent } from './events.js';
let globalState = {};
let initialized = false;
// Initialize the global state and optionally set initial values
export function initGlobalState(initialState = {}) {
if (initialized) throw new Error('Global state already initialized');
globalState = { ...initialState };
initialized = true;
}
export function getGlobalState(path) {
return getByPath(globalState, path);
}
export function setGlobalState(path, value) {
const prev = getByPath(globalState, path);
if (prev === value) return;
globalState = setByPath(globalState, path, value);
dispatchEvent({ path, value });
}
export function _getAllState() {
return globalState;
}