From 4fb4e101b671056affc6cc67e4670709e36b89c1 Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 13 Feb 2023 08:10:15 +0100 Subject: Simplify types and reduce exports Only export what should be necessary --- src/rx.ts | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/rx.ts b/src/rx.ts index a3420e7..bf01b6d 100644 --- a/src/rx.ts +++ b/src/rx.ts @@ -1,43 +1,48 @@ -// [1.0.0] 2023-02-13 +// [1.0.1] 2023-02-13 // Html -export interface Html { - type: 'Tag' | 'WithVar' -} - -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)) } @@ -46,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 { @@ -55,7 +60,7 @@ export function h( tagName, attributes: {} } - } else if (isChild(x)) { + } else if (isHtml(x)) { return { type: 'Tag', tagName, @@ -84,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, @@ -117,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 @@ -130,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 @@ -145,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 } @@ -260,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 = [] @@ -337,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) @@ -364,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