diff options
author | Joris | 2023-02-18 14:30:45 +0100 |
---|---|---|
committer | Joris | 2023-02-19 13:25:29 +0100 |
commit | e1aaa513a444a32c56a9591dd92beb24e66bcf42 (patch) | |
tree | 227fd22cfe5be446955a0c5cb6109b6107632090 /src/rx.ts | |
parent | cda836c875043572eb752a4f68368e7e1c6672f6 (diff) |
Integrate update function with Var
But still don’t expose `Var`, so that it would be still passed as a Rx.
Diffstat (limited to 'src/rx.ts')
-rw-r--r-- | src/rx.ts | 14 |
1 files changed, 7 insertions, 7 deletions
@@ -1,5 +1,3 @@ -// [1.1.0] 2023-02-13 - // Html export type Html @@ -24,7 +22,7 @@ interface Tag { interface WithVar<A> { type: 'WithVar' init: A - getChildren: (v: Var<A>, update: (f: (value: A) => A) => void) => Html + getChildren: (v: Var<A>) => Html } interface Attributes { @@ -90,7 +88,7 @@ export function h( } } -export function withVar<A>(init: A, getChildren: (v: Var<A>, update: (f: (value: A) => A) => void) => Html): WithVar<A> { +export function withVar<A>(init: A, getChildren: (v: Var<A>) => Html): WithVar<A> { return { type: 'WithVar', init, @@ -115,11 +113,13 @@ export class Rx<A> { class Var<A> extends Rx<A> { readonly type: 'Var' readonly id: string + readonly update: (f: (value: A) => A) => void - constructor(id: string) { + constructor(id: string, update: (v: Var<A>) => ((f: ((value: A) => A)) => void)) { super() this.id = id this.type = 'Var' + this.update = update(this) } } @@ -172,7 +172,7 @@ class State { } register<A>(initValue: A) { - const v = new Var(this.varCounter.toString()) + const v = new Var(this.varCounter.toString(), v => (f => this.update(v, f))) this.varCounter += BigInt(1) this.state[v.id] = { value: initValue, @@ -325,7 +325,7 @@ function appendChild(state: State, element: Element, child: Html, lastAdded?: No } else if (isWithVar(child)) { const { init, getChildren } = child const v = state.register(init) - const children = getChildren(v, f => state.update(v, f)) + const children = getChildren(v) const appendRes = appendChild(state, element, children) return { cancel: () => { |