From dfdde2daf238628dfb110c807a5249c31b903af1 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 13 Feb 2023 08:18:42 +0100 Subject: Upgrade rx to 1.0.1 and add type hints --- src/lib/rx.ts | 57 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'src/lib/rx.ts') diff --git a/src/lib/rx.ts b/src/lib/rx.ts index 1b41c27..bf01b6d 100644 --- a/src/lib/rx.ts +++ b/src/lib/rx.ts @@ -1,41 +1,48 @@ -// Html +// [1.0.1] 2023-02-13 -export interface Html { - type: 'Tag' | 'WithVar' -} +// Html -export interface Tag extends Html { +export type Html + = false + | undefined + | string + | number + | Tag + | WithVar + | Array + | Rx + +interface Tag { type: 'Tag' tagName: string attributes: Attributes - children?: Array + children?: Array onmount?: (element: Element) => void onunmount?: (element: Element) => void } -export interface WithVar extends Html { +interface WithVar { type: 'WithVar' init: A - getChildren: (v: Var, update: (f: (value: A) => A) => void) => Child + getChildren: (v: Var, update: (f: (value: A) => A) => void) => Html } interface Attributes { [key: string]: Rx | AttributeValue } -export type AttributeValue +type AttributeValue = string | number | boolean | ((event: Event) => void) | ((element: Element) => void) -export type Child = false | undefined | string | number | Html | Rx | Array - -function isChild(x: any): x is Child { +function isHtml(x: any): x is Html { return (typeof x === 'string' || typeof x === 'number' - || isHtml(x) + || isTag(x) + || isWithVar(x) || isRx(x) || Array.isArray(x)) } @@ -44,8 +51,8 @@ type ValueOrArray = T | Array> export function h( tagName: string, - x?: Attributes | Child, - ...children: Array + x?: Attributes | Html, + ...children: Array ): Tag { if (x === undefined || x === false) { return { @@ -53,7 +60,7 @@ export function h( tagName, attributes: {} } - } else if (isChild(x)) { + } else if (isHtml(x)) { return { type: 'Tag', tagName, @@ -82,7 +89,7 @@ export function h( } } -export function withVar(init: A, getChildren: (v: Var, update: (f: (value: A) => A) => void) => Child): WithVar { +export function withVar(init: A, getChildren: (v: Var, update: (f: (value: A) => A) => void) => Html): WithVar { return { type: 'WithVar', init, @@ -115,7 +122,7 @@ class Var extends Rx { } } -export class Map extends Rx { +class Map extends Rx { readonly type: 'Map' readonly rx: Rx readonly f: (value: A) => B @@ -128,7 +135,7 @@ export class Map extends Rx { } } -export class FlatMap extends Rx { +class FlatMap extends Rx { readonly type: 'FlatMap' readonly rx: Rx readonly f: (value: A) => Rx @@ -143,9 +150,9 @@ export class FlatMap extends Rx { // Mount -export function mount(child: Child): Cancelable { +export function mount(html: Html): Cancelable { const state = new State() - let appendRes = appendChild(state, document.body, child) + let appendRes = appendChild(state, document.body, html) return appendRes.cancel } @@ -258,7 +265,7 @@ interface AppendResult { lastAdded?: Node } -function appendChild(state: State, element: Element, child: Child, lastAdded?: Node): AppendResult { +function appendChild(state: State, element: Element, child: Html, lastAdded?: Node): AppendResult { if (Array.isArray(child)) { let cancels: Array = [] let removes: Array = [] @@ -335,7 +342,7 @@ function appendChild(state: State, element: Element, child: Child, lastAdded?: N remove: voidRemove, lastAdded: rxBase } - const cancelRx = rxRun(state, child, (value: Child) => { + const cancelRx = rxRun(state, child, (value: Html) => { appendRes.cancel() appendRes.remove() appendRes = appendChild(state, element, value, rxBase) @@ -362,10 +369,6 @@ function appendChild(state: State, element: Element, child: Child, lastAdded?: N } } -function isHtml(x: any): x is Html { - return isTag(x) || isWithVar(x) -} - function isTag(x: any): x is Tag { return x !== undefined && x.type === "Tag" } -- cgit v1.2.3