diff options
author | Joris Guyonvarch | 2025-08-31 14:22:55 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2025-08-31 14:22:55 +0200 |
commit | b71c7c2ceca0fa7d853e90a33f285745a1304531 (patch) | |
tree | 3d1e8eba33c3a0893109607c2b8845a5a7e4d0e3 | |
parent | 85cfbf6da3e19639de9d7812edfb33463e50d709 (diff) |
Add Var.now
When interfacing with non Rx stuff, this can be handy to get the current
value of a Var.
-rw-r--r-- | src/rx.ts | 14 |
1 files changed, 12 insertions, 2 deletions
@@ -270,12 +270,18 @@ export class Var<A> extends Rx<A> { readonly type: 'Var' readonly id: string readonly update: (f: (value: A) => A) => void + readonly now: () => A - constructor(id: string, update: (v: Var<A>) => ((f: ((value: A) => A)) => void)) { + constructor( + id: string, + update: (v: Var<A>) => ((f: ((value: A) => A)) => void), + now: (v: Var<A>) => (() => A) + ) { super() this.id = id this.type = 'Var' this.update = update(this) + this.now = now(this) } } @@ -350,7 +356,11 @@ class State { } register<A>(initValue: A) { - const v = new Var(this.varCounter.toString(), v => (f => this.update(v, f))) + const v = new Var( + this.varCounter.toString(), + v => (f => this.update(v, f)), + v => () => this.get(v) + ) this.varCounter += BigInt(1) this.state[v.id] = { value: initValue, |