Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rethinking events using ES6 (
const bus = {};
const get = e => (bus[e] = bus[e] || new Set());
export const listeners = new Proxy(bus, { get });
export const emit = (e, ...args) => listeners[e].forEach(fn => fn(...args));
import { listeners, emit } from '//';
// Simply name any property on listeners and it will become an event.
listeners.onUpdate.add(updated => console.log('Updated:', updated));
// Instruct an event to be emitted.
emit('onUpdate', 'Hello world');
// Clear all `onUpdate` events.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.