aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoris Guyonvarch2025-08-31 14:22:55 +0200
committerJoris Guyonvarch2025-08-31 14:22:55 +0200
commitb71c7c2ceca0fa7d853e90a33f285745a1304531 (patch)
tree3d1e8eba33c3a0893109607c2b8845a5a7e4d0e3
parent85cfbf6da3e19639de9d7812edfb33463e50d709 (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.ts14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/rx.ts b/src/rx.ts
index 958abcb..df69d31 100644
--- a/src/rx.ts
+++ b/src/rx.ts
@@ -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,