From b71c7c2ceca0fa7d853e90a33f285745a1304531 Mon Sep 17 00:00:00 2001
From: Joris Guyonvarch
Date: Sun, 31 Aug 2025 14:22:55 +0200
Subject: Add Var.now
When interfacing with non Rx stuff, this can be handy to get the current
value of a Var.
---
src/rx.ts | 14 ++++++++++++--
1 file 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 extends Rx {
readonly type: 'Var'
readonly id: string
readonly update: (f: (value: A) => A) => void
+ readonly now: () => A
- constructor(id: string, update: (v: Var) => ((f: ((value: A) => A)) => void)) {
+ constructor(
+ id: string,
+ update: (v: Var) => ((f: ((value: A) => A)) => void),
+ now: (v: Var) => (() => A)
+ ) {
super()
this.id = id
this.type = 'Var'
this.update = update(this)
+ this.now = now(this)
}
}
@@ -350,7 +356,11 @@ class State {
}
register(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,
--
cgit v1.2.3