Skip to content

Instantly share code, notes, and snippets.

@akonwi
Created April 22, 2019 15:58
Show Gist options
  • Save akonwi/7ea6e733b4e4b90ab3d8119f6386102b to your computer and use it in GitHub Desktop.
Save akonwi/7ea6e733b4e4b90ab3d8119f6386102b to your computer and use it in GitHub Desktop.
minimal event emitter for single value type
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