From b2bcdcc99e42015085b4d0d63e7c94b2d4f84e24 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 14 May 2024 01:17:20 +0200 Subject: interval updated ovar --- src/observable.ts | 13 +++++++++++-- 1 file 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 { this._value = initial; } + static interval(update: () => T, interval: number): OVar { + 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 { } liftA2(other: OVar, fn: (x: T, y: U) => V): OVar { 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; } -- cgit v1.2.3-70-g09d2