diff options
| author | Joris | 2023-02-13 08:10:15 +0100 | 
|---|---|---|
| committer | Joris | 2023-02-13 08:10:15 +0100 | 
| commit | 4fb4e101b671056affc6cc67e4670709e36b89c1 (patch) | |
| tree | a9901778dac779c2d39dc080ec8cb15dedc1a74a /src | |
| parent | 8de1a9e6d4d572e7a0e05a2469560de1be0ee4f2 (diff) | |
Simplify types and reduce exports1.0.1
Only export what should be necessary
Diffstat (limited to 'src')
| -rw-r--r-- | src/rx.ts | 57 | 
1 files changed, 29 insertions, 28 deletions
@@ -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<any>  +  | Array<Html> +  | Rx<Html>  + +interface Tag {    type: 'Tag'    tagName: string    attributes: Attributes -  children?: Array<Child> +  children?: Array<Html>    onmount?: (element: Element) => void    onunmount?: (element: Element) => void  } -export interface WithVar<A> extends Html { +interface WithVar<A> {    type: 'WithVar'    init: A -  getChildren: (v: Var<A>, update: (f: (value: A) => A) => void) => Child +  getChildren: (v: Var<A>, update: (f: (value: A) => A) => void) => Html  }  interface Attributes {    [key: string]: Rx<AttributeValue> | AttributeValue  } -export type AttributeValue +type AttributeValue    = string    | number    | boolean    | ((event: Event) => void)    | ((element: Element) => void) -export type Child = false | undefined | string | number | Html | Rx<Child> | Array<Child> - -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> = T | Array<ValueOrArray<T>>  export function h(    tagName: string, -  x?: Attributes | Child, -  ...children: Array<Child> +  x?: Attributes | Html, +  ...children: Array<Html>  ): 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<A>(init: A, getChildren: (v: Var<A>, update: (f: (value: A) => A) => void) => Child): WithVar<A> { +export function withVar<A>(init: A, getChildren: (v: Var<A>, update: (f: (value: A) => A) => void) => Html): WithVar<A> {    return {      type: 'WithVar',      init, @@ -117,7 +122,7 @@ class Var<A> extends Rx<A> {    }  } -export class Map<A, B> extends Rx<B> { +class Map<A, B> extends Rx<B> {    readonly type: 'Map'    readonly rx: Rx<A>    readonly f: (value: A) => B @@ -130,7 +135,7 @@ export class Map<A, B> extends Rx<B> {    }  } -export class FlatMap<A, B> extends Rx<B> { +class FlatMap<A, B> extends Rx<B> {    readonly type: 'FlatMap'    readonly rx: Rx<A>    readonly f: (value: A) => Rx<B> @@ -145,9 +150,9 @@ export class FlatMap<A, B> extends Rx<B> {  // 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<Cancelable> = []      let removes: Array<Removable> = [] @@ -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<A>(x: any): x is Html { -  return isTag<A>(x) || isWithVar<A>(x) -} -  function isTag<A>(x: any): x is Tag {    return x !== undefined && x.type === "Tag"  }  | 
