launch.io
Version:
Launch.IO is an Ultra Hip, Simple, and Fast, Time Traveling React State Management Library
30 lines (29 loc) • 623 B
JavaScript
export default class Observable {
constructor(value) {
this.value = value;
this.listeners = new Set();
}
_update(value) {
this.value = value;
this.emit();
}
emit() {
for (const listener of this.listeners) {
listener(this.snapshot());
}
}
update(value) {
this._update(value);
}
snapshot() {
return this.value;
}
subscribe(callback) {
this.listeners.add(callback);
return {
disconnect: () => {
this.listeners.delete(callback);
},
};
}
}