Created
July 3, 2025 16:05
-
-
Save A-gambit/952a8b3d607bd076ec7179823f9f6ac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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