Skip to content

Instantly share code, notes, and snippets.

@A-gambit
Created July 3, 2025 16:05
Show Gist options
  • Save A-gambit/952a8b3d607bd076ec7179823f9f6ac9 to your computer and use it in GitHub Desktop.
Save A-gambit/952a8b3d607bd076ec7179823f9f6ac9 to your computer and use it in GitHub Desktop.
// implement class that will fallow interface and expected behaviour below
interface EventEmitter {
on(eventName: string, cb: VoidFunction): void
one(eventName: string, cb: VoidFunction): void
off(eventName: string, cb: VoidFunction): void
emit(eventName: string): void
}
class EventEmitter implements EventEmitter {
//
}
console.clear();
const emitter = new EventEmitter()
const cb = () => {
console.log('foo1')
}
emitter.on('foo', cb)
emitter.one('foo', () => {
console.log('foo2')
})
emitter.emit('foo')
// Output:
// "foo1"
// "foo2"
emitter.emit('foo')
// Output:
// "foo1"
emitter.off('foo', cb)
emitter.emit('foo')
// No output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment