mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-06 08:25:19 +01:00
39 lines
541 B
JavaScript
39 lines
541 B
JavaScript
import {Emitter} from './emitter';
|
|
|
|
export class ExternalStateStream extends Emitter {
|
|
|
|
constructor(get, set) {
|
|
super();
|
|
this.get = get;
|
|
this.set = set;
|
|
}
|
|
|
|
get value() {
|
|
return this.get();
|
|
}
|
|
|
|
set value(v) {
|
|
this.next(v);
|
|
}
|
|
|
|
next(v) {
|
|
this.set(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);
|
|
}
|
|
}
|
|
|
|
|