From 8b84a732436fc6c84ce4b912b548648eb91c9312 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 19 Feb 2023 13:16:53 +0100 Subject: Rename withVar to withState --- src/example.ts | 24 ++++++++++++------------ src/rx.ts | 34 ++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/example.ts b/src/example.ts index 1362fbd..5ff3fba 100644 --- a/src/example.ts +++ b/src/example.ts @@ -1,7 +1,7 @@ -import { h, withVar, mount, RxAble } from 'rx' +import { h, withState, mount, sequence2, RxAble } from 'rx' const imbricatedMaps = - withVar(1, counter => [ + withState(1, counter => [ counterComponent({ value: counter, onSub: () => counter.update(n => n - 1), @@ -20,7 +20,7 @@ const imbricatedMaps = ]) const flatMap = - withVar(1, counter => [ + withState(1, counter => [ counterComponent({ value: counter, onSub: () => counter.update(n => n - 1), @@ -36,7 +36,7 @@ const flatMap = ]) const checkbox = - withVar(false, checked => [ + withState(false, checked => [ checkboxComponent({ label: 'Checkbox', isChecked: checked, @@ -46,7 +46,7 @@ const checkbox = ]) const rxChildren = - withVar(3, count => [ + withState(3, count => [ h('input', { type: 'number', value: count, @@ -60,7 +60,7 @@ const rxChildren = ]) const nodesCancel = - withVar(false, checked => [ + withState(false, checked => [ checkboxComponent({ label: 'Checkbox', isChecked: checked, @@ -70,7 +70,7 @@ const nodesCancel = ]) const counters = - withVar>([], counters => { + withState>([], counters => { return [ counterComponent({ value: counters.map(cs => cs.length), @@ -97,7 +97,7 @@ const counters = }) const rows = - withVar(1, count => [ + withState(1, count => [ h('input', { type: 'number', value: count, @@ -107,13 +107,13 @@ const rows = ]) const chrono = - withVar(false, isChecked => [ + withState(false, isChecked => [ checkboxComponent({ label: 'Show counter', isChecked, onCheck: b => isChecked.update(_ => b) }), - isChecked.map(b => b && withVar(0, elapsed => { + isChecked.map(b => b && withState(0, elapsed => { const interval = window.setInterval( () => elapsed.update(n => n + 1), 1000 @@ -127,8 +127,8 @@ const chrono = ]) const doubleMapChild = - withVar(true, isEven => - withVar('', search => { + withState(true, isEven => + withState('', search => { const books = [...Array(50).keys()] const filteredBooks = isEven.flatMap(f => search.map(s => diff --git a/src/rx.ts b/src/rx.ts index 5028ec1..ea4f6a4 100644 --- a/src/rx.ts +++ b/src/rx.ts @@ -1,14 +1,16 @@ +// Rx 1.0.1 + // Html export type Html - = false - | undefined - | string - | number - | Tag - | WithVar + = false + | undefined + | string + | number + | Tag + | WithState | Array - | Rx + | Rx interface Tag { type: 'Tag' @@ -19,8 +21,8 @@ interface Tag { onunmount?: (element: Element) => void } -interface WithVar { - type: 'WithVar' +interface WithState { + type: 'WithState' init: A getChildren: (v: Var) => Html } @@ -40,8 +42,8 @@ type AttributeValue function isHtml(x: any): x is Html { return (typeof x === 'string' || typeof x === 'number' - || isTag(x) - || isWithVar(x) + || isTag(x) + || isWithState(x) || isRx(x) || Array.isArray(x)) } @@ -88,9 +90,9 @@ export function h( } } -export function withVar(init: A, getChildren: (v: Var) => Html): WithVar { +export function withState(init: A, getChildren: (v: Var) => Html): WithState { return { - type: 'WithVar', + type: 'WithState', init, getChildren } @@ -322,7 +324,7 @@ function appendChild(state: State, element: Element, child: Html, lastAdded?: No remove: () => element.removeChild(childElement), lastAdded: childElement, } - } else if (isWithVar(child)) { + } else if (isWithState(child)) { const { init, getChildren } = child const v = state.register(init) const children = getChildren(v) @@ -374,8 +376,8 @@ function isTag(x: any): x is Tag { return x !== undefined && x.type === "Tag" } -function isWithVar(x: any): x is WithVar { - return x !== undefined && x.type === "WithVar" +function isWithState(x: any): x is WithState { + return x !== undefined && x.type === "WithState" } function appendNode(base: Element, node: Node, lastAdded?: Node) { -- cgit v1.2.3