{"id":"6b093ddebd4755deb67ee06b","slug":"event-emitter","title":"Your own EventEmitter","summary":"Write a simplified version of Node.js `EventEmitter` – on, once, off, emit and the special `error` event. The Observer pattern in practice, with the traps of modifying the list during emission.","servedLang":"en","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"easy","topics":["observer"],"techs":["typescript","javascript","python"],"tags":["design","events","observer","node","javascript"],"languages":[{"lang":"typescript","runner":"browser","starter":"type Listener = (...args: any[]) => void;\n\nclass EventEmitter {\n  /** Registers a listener; returns an unsubscribe() function. */\n  on(event: string, listener: Listener): () => void {\n    throw new Error('Not implemented');\n  }\n\n  /** Like on(), but the listener is removed before its first call. */\n  once(event: string, listener: Listener): () => void {\n    throw new Error('Not implemented');\n  }\n\n  /** Removes the most recently added registration of listener; true when removed. */\n  off(event: string, listener: Listener): boolean {\n    throw new Error('Not implemented');\n  }\n\n  /** Calls the listeners (snapshot taken at the start); true when there were any. */\n  emit(event: string, ...args: unknown[]): boolean {\n    throw new Error('Not implemented');\n  }\n\n  listenerCount(event: string): number {\n    throw new Error('Not implemented');\n  }\n}\n","testCount":9,"tests":"test('emit calls listeners in order with arguments', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  e.on('msg', (a, b) => calls.push('first:' + a + b));\n  e.on('msg', (a, b) => calls.push('second:' + a + b));\n  expect(e.emit('msg', 'hi', 1)).toBe(true);\n  expect(calls).toEqual(['first:hi1', 'second:hi1']);\n  expect(e.emit('other')).toBe(false);\n});\n\ntest('unsubscribe removes exactly that registration', () => {\n  const e = new EventEmitter();\n  let count = 0;\n  const listener = () => count++;\n  const offFirst = e.on('tick', listener);\n  e.on('tick', listener);\n  expect(e.listenerCount('tick')).toBe(2);\n  offFirst();\n  offFirst();\n  expect(e.listenerCount('tick')).toBe(1);\n  e.emit('tick');\n  expect(count).toBe(1);\n});\n\ntest('once is called only once and can be removed with off', () => {\n  const e = new EventEmitter();\n  let count = 0;\n  const init = () => count++;\n  e.once('ready', init);\n  e.emit('ready');\n  e.emit('ready');\n  expect(count).toBe(1);\n  expect(e.listenerCount('ready')).toBe(0);\n  e.once('ready', init);\n  expect(e.off('ready', init)).toBe(true);\n  expect(e.emit('ready')).toBe(false);\n  expect(count).toBe(1);\n});\n\ntest('off removes the most recent registration and reports the result', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  const a = (x) => calls.push('a' + x);\n  e.on('x', a);\n  e.once('x', a);\n  expect(e.off('x', a)).toBe(true);\n  e.emit('x', 1);\n  e.emit('x', 2);\n  expect(calls).toEqual(['a1', 'a2']);\n  expect(e.off('x', () => {})).toBe(false);\n  expect(e.off('missing', a)).toBe(false);\n});\n\ntest('the listener list is frozen when emit starts', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  const second = () => calls.push('second');\n  e.on('go', () => {\n    calls.push('first');\n    e.off('go', second);\n    e.on('go', () => calls.push('late'));\n  });\n  e.on('go', second);\n  e.emit('go');\n  expect(calls).toEqual(['first', 'second']);\n  calls.length = 0;\n  e.emit('go');\n  expect(calls).toEqual(['first', 'late']);\n});\n\ntest('once listener re-emitting the same event is not called again', () => {\n  const e = new EventEmitter();\n  let count = 0;\n  e.once('ping', () => {\n    count++;\n    e.emit('ping');\n  });\n  e.emit('ping');\n  expect(count).toBe(1);\n});\n\ntest('a throwing listener stops the emission', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  e.on('job', () => {\n    throw new Error('listener failed');\n  });\n  e.on('job', () => calls.push('never'));\n  expect(() => e.emit('job')).toThrow('listener failed');\n  expect(calls).toEqual([]);\n});\n\ntest('error event without listeners throws', () => {\n  const e = new EventEmitter();\n  const boom = new Error('boom');\n  expect(() => e.emit('error', boom)).toThrow('boom');\n  expect(() => e.emit('error', 'just a string')).toThrow('Unhandled error');\n  let handled = null;\n  e.on('error', (err) => (handled = err));\n  expect(e.emit('error', boom)).toBe(true);\n  expect(handled).toBe(boom);\n});\n\ntest('performance: 100 000 registrations and emissions', () => {\n  const e = new EventEmitter();\n  let total = 0;\n  for (let i = 0; i < 100000; i++) e.on('event' + (i % 1000), (x) => (total += x));\n  for (let i = 0; i < 1000; i++) e.emit('event' + i, 1);\n  expect(total).toBe(100000);\n  for (let i = 0; i < 100000; i++) e.once('single', () => total++);\n  e.emit('single');\n  expect(total).toBe(200000);\n  expect(e.listenerCount('single')).toBe(0);\n});\n"},{"lang":"javascript","runner":"browser","starter":"class EventEmitter {\n  /** Registers a listener; returns an unsubscribe() function. */\n  on(event, listener) {\n    throw new Error('Not implemented');\n  }\n\n  /** Like on(), but the listener is removed before its first call. */\n  once(event, listener) {\n    throw new Error('Not implemented');\n  }\n\n  /** Removes the most recently added registration of listener; true when removed. */\n  off(event, listener) {\n    throw new Error('Not implemented');\n  }\n\n  /** Calls the listeners (snapshot taken at the start); true when there were any. */\n  emit(event, ...args) {\n    throw new Error('Not implemented');\n  }\n\n  listenerCount(event) {\n    throw new Error('Not implemented');\n  }\n}\n","testCount":9,"tests":"test('emit calls listeners in order with arguments', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  e.on('msg', (a, b) => calls.push('first:' + a + b));\n  e.on('msg', (a, b) => calls.push('second:' + a + b));\n  expect(e.emit('msg', 'hi', 1)).toBe(true);\n  expect(calls).toEqual(['first:hi1', 'second:hi1']);\n  expect(e.emit('other')).toBe(false);\n});\n\ntest('unsubscribe removes exactly that registration', () => {\n  const e = new EventEmitter();\n  let count = 0;\n  const listener = () => count++;\n  const offFirst = e.on('tick', listener);\n  e.on('tick', listener);\n  expect(e.listenerCount('tick')).toBe(2);\n  offFirst();\n  offFirst();\n  expect(e.listenerCount('tick')).toBe(1);\n  e.emit('tick');\n  expect(count).toBe(1);\n});\n\ntest('once is called only once and can be removed with off', () => {\n  const e = new EventEmitter();\n  let count = 0;\n  const init = () => count++;\n  e.once('ready', init);\n  e.emit('ready');\n  e.emit('ready');\n  expect(count).toBe(1);\n  expect(e.listenerCount('ready')).toBe(0);\n  e.once('ready', init);\n  expect(e.off('ready', init)).toBe(true);\n  expect(e.emit('ready')).toBe(false);\n  expect(count).toBe(1);\n});\n\ntest('off removes the most recent registration and reports the result', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  const a = (x) => calls.push('a' + x);\n  e.on('x', a);\n  e.once('x', a);\n  expect(e.off('x', a)).toBe(true);\n  e.emit('x', 1);\n  e.emit('x', 2);\n  expect(calls).toEqual(['a1', 'a2']);\n  expect(e.off('x', () => {})).toBe(false);\n  expect(e.off('missing', a)).toBe(false);\n});\n\ntest('the listener list is frozen when emit starts', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  const second = () => calls.push('second');\n  e.on('go', () => {\n    calls.push('first');\n    e.off('go', second);\n    e.on('go', () => calls.push('late'));\n  });\n  e.on('go', second);\n  e.emit('go');\n  expect(calls).toEqual(['first', 'second']);\n  calls.length = 0;\n  e.emit('go');\n  expect(calls).toEqual(['first', 'late']);\n});\n\ntest('once listener re-emitting the same event is not called again', () => {\n  const e = new EventEmitter();\n  let count = 0;\n  e.once('ping', () => {\n    count++;\n    e.emit('ping');\n  });\n  e.emit('ping');\n  expect(count).toBe(1);\n});\n\ntest('a throwing listener stops the emission', () => {\n  const e = new EventEmitter();\n  const calls = [];\n  e.on('job', () => {\n    throw new Error('listener failed');\n  });\n  e.on('job', () => calls.push('never'));\n  expect(() => e.emit('job')).toThrow('listener failed');\n  expect(calls).toEqual([]);\n});\n\ntest('error event without listeners throws', () => {\n  const e = new EventEmitter();\n  const boom = new Error('boom');\n  expect(() => e.emit('error', boom)).toThrow('boom');\n  expect(() => e.emit('error', 'just a string')).toThrow('Unhandled error');\n  let handled = null;\n  e.on('error', (err) => (handled = err));\n  expect(e.emit('error', boom)).toBe(true);\n  expect(handled).toBe(boom);\n});\n\ntest('performance: 100 000 registrations and emissions', () => {\n  const e = new EventEmitter();\n  let total = 0;\n  for (let i = 0; i < 100000; i++) e.on('event' + (i % 1000), (x) => (total += x));\n  for (let i = 0; i < 1000; i++) e.emit('event' + i, 1);\n  expect(total).toBe(100000);\n  for (let i = 0; i < 100000; i++) e.once('single', () => total++);\n  e.emit('single');\n  expect(total).toBe(200000);\n  expect(e.listenerCount('single')).toBe(0);\n});\n"},{"lang":"python","runner":"browser","starter":"from typing import Any, Callable\n\n\nclass EventEmitter:\n    def on(self, event: str, listener: Callable[..., Any]) -> Callable[[], None]:\n        \"\"\"Registers a listener; returns an unsubscribe() function.\"\"\"\n        raise NotImplementedError\n\n    def once(self, event: str, listener: Callable[..., Any]) -> Callable[[], None]:\n        \"\"\"Like on(), but the listener is removed before its first call.\"\"\"\n        raise NotImplementedError\n\n    def off(self, event: str, listener: Callable[..., Any]) -> bool:\n        \"\"\"Removes the most recently added registration of listener; True when removed.\"\"\"\n        raise NotImplementedError\n\n    def emit(self, event: str, *args: Any) -> bool:\n        \"\"\"Calls the listeners (snapshot taken at the start); True when there were any.\"\"\"\n        raise NotImplementedError\n\n    def listener_count(self, event: str) -> int:\n        raise NotImplementedError\n","testCount":9,"tests":"def eq(got, expected, what):\n    assert got == expected, f\"{what}: expected {expected!r}, got {got!r}\"\n\n\ndef test_emit_calls_listeners_in_order_with_arguments():\n    e = EventEmitter()\n    calls = []\n    e.on(\"msg\", lambda a, b: calls.append(f\"first:{a}{b}\"))\n    e.on(\"msg\", lambda a, b: calls.append(f\"second:{a}{b}\"))\n    eq(e.emit(\"msg\", \"hi\", 1), True, \"emit() with listeners\")\n    eq(calls, [\"first:hi1\", \"second:hi1\"], \"calls\")\n    eq(e.emit(\"other\"), False, \"emit() without listeners\")\n\n\ndef test_unsubscribe_removes_exactly_that_registration():\n    e = EventEmitter()\n    count = [0]\n\n    def listener():\n        count[0] += 1\n\n    off_first = e.on(\"tick\", listener)\n    e.on(\"tick\", listener)\n    eq(e.listener_count(\"tick\"), 2, \"listener_count\")\n    off_first()\n    off_first()\n    eq(e.listener_count(\"tick\"), 1, \"listener_count after unsubscribe\")\n    e.emit(\"tick\")\n    eq(count[0], 1, \"calls\")\n\n\ndef test_once_is_called_once_and_can_be_removed_with_off():\n    e = EventEmitter()\n    count = [0]\n\n    def init():\n        count[0] += 1\n\n    e.once(\"ready\", init)\n    e.emit(\"ready\")\n    e.emit(\"ready\")\n    eq(count[0], 1, \"calls\")\n    eq(e.listener_count(\"ready\"), 0, \"listener_count\")\n    e.once(\"ready\", init)\n    eq(e.off(\"ready\", init), True, \"off() of a once listener\")\n    eq(e.emit(\"ready\"), False, \"emit() after off()\")\n    eq(count[0], 1, \"calls\")\n\n\ndef test_off_removes_the_most_recent_registration():\n    e = EventEmitter()\n    calls = []\n\n    def a(x):\n        calls.append(f\"a{x}\")\n\n    e.on(\"x\", a)\n    e.once(\"x\", a)\n    eq(e.off(\"x\", a), True, \"off()\")\n    e.emit(\"x\", 1)\n    e.emit(\"x\", 2)\n    eq(calls, [\"a1\", \"a2\"], \"calls\")\n    eq(e.off(\"x\", lambda: None), False, \"off() of an unknown listener\")\n    eq(e.off(\"missing\", a), False, \"off() of an unknown event\")\n\n\ndef test_listener_list_is_frozen_when_emit_starts():\n    e = EventEmitter()\n    calls = []\n\n    def second():\n        calls.append(\"second\")\n\n    def first():\n        calls.append(\"first\")\n        e.off(\"go\", second)\n        e.on(\"go\", lambda: calls.append(\"late\"))\n\n    e.on(\"go\", first)\n    e.on(\"go\", second)\n    e.emit(\"go\")\n    eq(calls, [\"first\", \"second\"], \"first emit\")\n    calls.clear()\n    e.emit(\"go\")\n    eq(calls, [\"first\", \"late\"], \"second emit\")\n\n\ndef test_once_listener_re_emitting_is_not_called_again():\n    e = EventEmitter()\n    count = [0]\n\n    def ping():\n        count[0] += 1\n        e.emit(\"ping\")\n\n    e.once(\"ping\", ping)\n    e.emit(\"ping\")\n    eq(count[0], 1, \"calls\")\n\n\ndef test_throwing_listener_stops_the_emission():\n    e = EventEmitter()\n    calls = []\n\n    def failing():\n        raise ValueError(\"listener failed\")\n\n    e.on(\"job\", failing)\n    e.on(\"job\", lambda: calls.append(\"never\"))\n    with raises(ValueError, match=\"listener failed\"):\n        e.emit(\"job\")\n    eq(calls, [], \"calls after the failure\")\n\n\ndef test_error_event_without_listeners_raises():\n    e = EventEmitter()\n    boom = ValueError(\"boom\")\n    with raises(ValueError, match=\"boom\"):\n        e.emit(\"error\", boom)\n    with raises(RuntimeError, match=\"Unhandled error\"):\n        e.emit(\"error\", \"just a string\")\n    handled = []\n    e.on(\"error\", handled.append)\n    eq(e.emit(\"error\", boom), True, \"emit('error') with a listener\")\n    assert handled == [boom], f\"handled: {handled!r}\"\n\n\ndef test_performance_100k_registrations():\n    e = EventEmitter()\n    total = [0]\n\n    def add(x):\n        total[0] += x\n\n    for i in range(100_000):\n        e.on(f\"event{i % 1000}\", add)\n    for i in range(1000):\n        e.emit(f\"event{i}\", 1)\n    eq(total[0], 100_000, \"total after emits\")\n\n    def bump():\n        total[0] += 1\n\n    for _ in range(100_000):\n        e.once(\"single\", bump)\n    e.emit(\"single\")\n    eq(total[0], 200_000, \"total after once listeners\")\n    eq(e.listener_count(\"single\"), 0, \"listener_count('single')\")\n"}],"questionCount":null,"xp":10,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:54.007Z","requestedLang":"en","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"EventEmitter","marks":[{"type":"code"}]},{"type":"text","text":" is the heart of Node.js – streams, HTTP servers and sockets all emit events. Interviews often ask: \"write your own\". The task checks the "},{"type":"text","text":"Observer","marks":[{"type":"bold"}]},{"type":"text","text":" pattern and a few subtleties that are easy to trip over."}]},{"type":"heading","attrs":{"level":2,"id":"task"},"content":[{"type":"text","text":"Task"}]},{"type":"paragraph","content":[{"type":"text","text":"An "},{"type":"text","text":"EventEmitter","marks":[{"type":"code"}]},{"type":"text","text":" class with these methods (Python: "},{"type":"text","text":"listener_count","marks":[{"type":"code"}]},{"type":"text","text":" instead of "},{"type":"text","text":"listenerCount","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"*args","marks":[{"type":"code"}]},{"type":"text","text":" arguments):"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Method"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Behaviour"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"on(event, listener)","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"registers a listener; returns an "},{"type":"text","text":"unsubscribe()","marks":[{"type":"code"}]},{"type":"text","text":" function (calling it again is safe)"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"once(event, listener)","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"like "},{"type":"text","text":"on","marks":[{"type":"code"}]},{"type":"text","text":", but the listener is removed "},{"type":"text","text":"before","marks":[{"type":"bold"}]},{"type":"text","text":" its first call; also returns "},{"type":"text","text":"unsubscribe()","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"off(event, listener)","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"removes "},{"type":"text","text":"one","marks":[{"type":"bold"}]},{"type":"text","text":" registration of this listener (the most recently added); works for "},{"type":"text","text":"once","marks":[{"type":"code"}]},{"type":"text","text":" listeners too; returns "},{"type":"text","text":"true","marks":[{"type":"code"}]},{"type":"text","text":" when something was removed"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"emit(event, ...args)","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"synchronously calls the listeners in registration order with "},{"type":"text","text":"args","marks":[{"type":"code"}]},{"type":"text","text":"; returns "},{"type":"text","text":"true","marks":[{"type":"code"}]},{"type":"text","text":" when any listener was registered"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"listenerCount(event)","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"number of listeners registered for the event"}]}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Rules:"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"the same listener registered twice is called twice,"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"the list of listeners is "},{"type":"text","text":"frozen when ","marks":[{"type":"bold"}]},{"type":"text","text":"emit","marks":[{"type":"code"}]},{"type":"text","text":" starts","marks":[{"type":"bold"}]},{"type":"text","text":": listeners added during emission are called from the next emit on, and removing one during emission does not stop the current round,"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"an exception thrown by a listener stops the emission and propagates out of "},{"type":"text","text":"emit","marks":[{"type":"code"}]},{"type":"text","text":" (as in Node.js),"}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"emit('error', err)","marks":[{"type":"code"}]},{"type":"text","text":" with no "},{"type":"text","text":"error","marks":[{"type":"code"}]},{"type":"text","text":" listeners throws "},{"type":"text","text":"err","marks":[{"type":"code"}]},{"type":"text","text":" (if it is an "},{"type":"text","text":"Error","marks":[{"type":"code"}]},{"type":"text","text":" / exception), otherwise an "},{"type":"text","text":"Error","marks":[{"type":"code"}]},{"type":"text","text":" / "},{"type":"text","text":"RuntimeError","marks":[{"type":"code"}]},{"type":"text","text":" with the message "},{"type":"text","text":"Unhandled error","marks":[{"type":"code"}]},{"type":"text","text":"."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"example"},"content":[{"type":"text","text":"Example"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Code"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Effect"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"const off = e.on('msg', log)","marks":[{"type":"code"}]},{"type":"text","text":"; "},{"type":"text","text":"e.emit('msg', 'hi', 1)","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"log('hi', 1)","marks":[{"type":"code"}]},{"type":"text","text":", returns "},{"type":"text","text":"true","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"e.once('ready', init)","marks":[{"type":"code"}]},{"type":"text","text":"; "},{"type":"text","text":"e.emit('ready')","marks":[{"type":"code"}]},{"type":"text","text":"; "},{"type":"text","text":"e.emit('ready')","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"init","marks":[{"type":"code"}]},{"type":"text","text":" called once"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"off()","marks":[{"type":"code"}]},{"type":"text","text":"; "},{"type":"text","text":"e.emit('msg')","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"nothing happens, returns "},{"type":"text","text":"false","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"e.emit('error', new Error('boom'))","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"throws "},{"type":"text","text":"Error('boom')","marks":[{"type":"code"}]}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"constraints"},"content":[{"type":"text","text":"Constraints"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Up to 100,000 registrations and emissions per test; event names are strings."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"expected-complexity"},"content":[{"type":"text","text":"Expected complexity"}]},{"type":"paragraph","content":[{"type":"text","text":"on","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"once","marks":[{"type":"code"}]},{"type":"text","text":", "},{"type":"text","text":"listenerCount","marks":[{"type":"code"}]},{"type":"text","text":" – "},{"type":"text","text":"O(1)","marks":[{"type":"bold"}]},{"type":"text","text":"; "},{"type":"text","text":"emit","marks":[{"type":"code"}]},{"type":"text","text":" – "},{"type":"text","text":"O(k)","marks":[{"type":"bold"}]},{"type":"text","text":" for "},{"type":"text","text":"k","marks":[{"type":"code"}]},{"type":"text","text":" listeners; "},{"type":"text","text":"off","marks":[{"type":"code"}]},{"type":"text","text":" – "},{"type":"text","text":"O(k)","marks":[{"type":"bold"}]},{"type":"text","text":"."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/event-emitter/","title":"LeetCode 2694 – Event Emitter","difficulty":null}],"articles":[{"id":"6b08bd3c80884893657cc9dd","slug":"observer","title":"Observer: notify everyone without knowing anyone"}],"topicsInfo":[{"slug":"observer","title":"Observer","group":"behavioral","icon":"eye"}],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:54.009Z","updatedAt":"2026-11-27T08:22:54.009Z"}