class Observable {
constructor(initialValue) {
this._value = initialValue;
this.observers = [];
}
read() {
return this._value;
}
write(newValue) {
if (newValue !== this._value) {
this._value = newValue;
this.notifyObservers();
}
}
subscribe(observer) {
this.observers.push(observer);
}
notifyObservers() {
this.observers.forEach(observer => observer(this._value));
}
}