EventTarget as a Zero-Dependency State Bus
EventTarget as a Zero-Dependency State Bus
The browser's native EventTarget is a complete pub/sub mechanism — enough to back an app's state store without Redux, a custom EventEmitter, or a framework's reactivity. The localization-helper's store owns one EventTarget, emits named events on mutation (change:dirty, change:keys), and the UI re-renders on those events, not on any component lifecycle:
const _emitter = new EventTarget()
export function subscribe(type, fn) {
_emitter.addEventListener(type, fn)
return () => _emitter.removeEventListener(type, fn) // unsubscribe is the return value
}(store.ts:21,300)
What it buys: zero dependencies, standard semantics every developer already knows, and a subscribe() that hands back its own cleanup — so teardown is impossible to forget. State lives in one module; views are dumb subscribers that re-render when their event fires.
This is the concrete, batteries-included instance of the Reactive State Bus Pattern: a minimal pub/sub for UI reactivity without framework overhead. The lesson is that the platform often already ships the primitive — reaching for a library is a choice, not a requirement, when the need is just "notify subscribers that state changed."