react-query
Version:
Hooks for managing, caching and syncing asynchronous and remote data in React
26 lines (21 loc) • 470 B
JavaScript
export class Subscribable {
constructor() {
this.listeners = [];
this.subscribe = this.subscribe.bind(this);
}
subscribe(listener) {
this.listeners.push(listener);
this.onSubscribe();
return () => {
this.listeners = this.listeners.filter(x => x !== listener);
this.onUnsubscribe();
};
}
hasListeners() {
return this.listeners.length > 0;
}
onSubscribe() {// Do nothing
}
onUnsubscribe() {// Do nothing
}
}