TGViewer
JavaScript JavaScript @javascript · 31K subscribers
Post #3560 325
CHALLENGE

class EventBus {
#listeners = new Map();
on(event, cb) {
if (!this.#listeners.has(event)) this.#listeners.set(event, []);
this.#listeners.get(event).push(cb);
return this;
}
emit(event, ...args) {
const cbs = this.#listeners.get(event) || [];
return cbs.map(cb => cb(...args));
}
}

const bus = new EventBus();
bus.on('data', x => x * 2).on('data', x => x + 1);
const results = bus.emit('data', 5);
console.log(results, bus.emit('missing', 1));
  • ❤ 1
More from @javascript
  1. Oct 15, 2026Post #3561
  2. Oct 14, 2026💻 Deno is Joining Cloudflare (But…) Maybe David Bushell saw the writing on the wall, as t…
  3. Oct 14, 2026Post #3558
  4. Oct 14, 2026CHALLENGE const obj = { value: 42, getValue: function() { return this.value; } }; const un…
  5. Oct 13, 2026Post #3556
  6. Oct 13, 2026CHALLENGE function* inner() { const x = yield 1; yield x + 1; return 100; } function* oute…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →