Created
April 22, 2019 15:58
-
-
Save akonwi/7ea6e733b4e4b90ab3d8119f6386102b to your computer and use it in GitHub Desktop.
minimal event emitter for single value type
This file contains 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
interface Listener<T> { | |
(value: T): void; | |
} | |
class Echo<T> implements Disposable { | |
private listeners = new Set<Listener<T>>(); | |
listen(listener: (value: T) => void) { | |
this.listeners.add(listener); | |
return new Disposable(() => { | |
this.listeners.delete(listener); | |
}); | |
} | |
push(value: T) { | |
this.listeners.forEach(listener => listener(value)); | |
} | |
dispose() { | |
this.listeners.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment