mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-09 01:44:19 +01:00
38 lines
538 B
JavaScript
38 lines
538 B
JavaScript
import {Emitter} from './emitter';
|
|
|
|
export class StateStream extends Emitter {
|
|
|
|
constructor(initialValue) {
|
|
super();
|
|
this._value = initialValue;
|
|
}
|
|
|
|
get value() {
|
|
return this._value;
|
|
}
|
|
|
|
set value(v) {
|
|
this.next(v);
|
|
}
|
|
|
|
next(v) {
|
|
this._value = v;
|
|
super.next(v);
|
|
}
|
|
|
|
update(updater) {
|
|
this.value = updater(this._value);
|
|
}
|
|
|
|
mutate(mutator) {
|
|
mutator(this._value);
|
|
this.next(this._value);
|
|
}
|
|
|
|
attach(observer) {
|
|
observer(this._value);
|
|
return super.attach(observer);
|
|
}
|
|
}
|
|
|
|
|