Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rethinking events using ES6 (https://tbranyen.com/events)
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 '//tbranyen.com/events';
// 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.
listeners.onUpdate.clear();
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.