2019-08-21 21:19:44 +08:00
|
|
|
import { Disposable } from "./Disposable";
|
|
|
|
import { Observable } from "./Observable";
|
|
|
|
import { WritableProperty } from "./WritableProperty";
|
2019-08-23 04:45:01 +08:00
|
|
|
import { is_property } from "./Property";
|
|
|
|
import { AbstractProperty } from "./AbstractProperty";
|
2019-08-21 21:19:44 +08:00
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
export class SimpleProperty<T> extends AbstractProperty<T> implements WritableProperty<T> {
|
2019-08-21 21:19:44 +08:00
|
|
|
readonly is_writable_property = true;
|
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
constructor(private _val: T) {
|
2019-08-21 21:19:44 +08:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
get val(): T {
|
|
|
|
return this._val;
|
2019-08-21 21:19:44 +08:00
|
|
|
}
|
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
set val(val: T) {
|
|
|
|
if (val !== this._val) {
|
|
|
|
this._val = val;
|
|
|
|
this.emit();
|
2019-08-21 21:19:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
update(f: (value: T) => T): void {
|
|
|
|
this.val = f(this.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
bind(observable: Observable<T>): Disposable {
|
2019-08-21 21:19:44 +08:00
|
|
|
if (is_property(observable)) {
|
2019-08-23 04:45:01 +08:00
|
|
|
this.val = observable.val;
|
2019-08-21 21:19:44 +08:00
|
|
|
}
|
|
|
|
|
2019-08-23 04:45:01 +08:00
|
|
|
return observable.observe(v => (this.val = v));
|
2019-08-21 21:19:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bind_bi(property: WritableProperty<T>): Disposable {
|
|
|
|
const bind_1 = this.bind(property);
|
|
|
|
const bind_2 = property.bind(this);
|
|
|
|
return {
|
|
|
|
dispose(): void {
|
|
|
|
bind_1.dispose();
|
|
|
|
bind_2.dispose();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|