diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/observable.ts | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/observable.ts b/src/observable.ts index bd66963..f43e6e2 100644 --- a/src/observable.ts +++ b/src/observable.ts @@ -14,6 +14,14 @@ export class OVar<T> { this._value = initial; } + static interval<T>(update: () => T, interval: number): OVar<T> { + const o = new OVar(update()) + const i = setInterval(() => o.value = update(), interval) + o.cancel_source = () => clearInterval(i) + o.weak = true; + return o + } + get value() { return this._value } set value(v: T) { this._value = v; this.change() } change() { this.observers.forEach(o => o(this._value)) } @@ -43,8 +51,9 @@ export class OVar<T> { } liftA2<U, V>(other: OVar<U>, fn: (x: T, y: U) => V): OVar<V> { const uv = new OVar(fn(this.value, other.value)) - uv.cancel_source = this.onchange(x => uv.value = fn(x, other.value)) - uv.cancel_source = other.onchange(y => uv.value = fn(this.value, y)) + const c1 = this.onchange(x => uv.value = fn(x, other.value)) + const c2 = other.onchange(y => uv.value = fn(this.value, y)) + uv.cancel_source = () => { c1(); c2() } uv.weak = true return uv; } |