From 81bcb1aedc760f8af4bb43e5f96444926dfdd991 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 23 Mar 2025 15:55:30 +0100 Subject: Add withState6 and map6 --- src/rx.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/src/rx.ts b/src/rx.ts index db91bdb..4d0101e 100644 --- a/src/rx.ts +++ b/src/rx.ts @@ -13,6 +13,7 @@ export type Html | WithState3 | WithState4 | WithState5 + | WithState6 | Array | Rx @@ -55,6 +56,12 @@ interface WithState5 { getChildren: (v1: Var, v2: Var, v3: Var, v4: Var, v5: Var) => Html } +interface WithState6 { + type: 'WithState6' + init: [A, B, C, D, E, F] + getChildren: (v1: Var, v2: Var, v3: Var, v4: Var, v5: Var, v6: Var) => Html +} + interface Attributes { [key: string]: Rx | AttributeValue } @@ -161,6 +168,14 @@ export function withState5(init: [A, B, C, D, E], getChildren: (v } } +export function withState6(init: [A, B, C, D, E, F], getChildren: (v1: Var, v2: Var, v3: Var, v4: Var, v5: Var, v6: Var) => Html): WithState6 { + return { + type: 'WithState6', + init, + getChildren + } +} + // Rx export type RxAble = Rx | A @@ -175,20 +190,24 @@ export class Rx { } } -export function map2(rx: [Rx, Rx], f: (a: A, b: B) => C): Rx { - return rx[0].flatMap(a => rx[1].map(b => f(a, b))) +export function map2(rx: [Rx, Rx], fn: (a: A, b: B) => C): Rx { + return rx[0].flatMap(a => rx[1].map(b => fn(a, b))) } -export function map3(rx: [Rx, Rx, Rx], f: (a: A, b: B, c: C) => D): Rx { - return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].map(c => f(a, b, c)))) +export function map3(rx: [Rx, Rx, Rx], fn: (a: A, b: B, c: C) => D): Rx { + return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].map(c => fn(a, b, c)))) } -export function map4(rx: [Rx, Rx, Rx, Rx], f: (a: A, b: B, c: C, d: D) => E): Rx { - return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].flatMap(c => rx[3].map(d => f(a, b, c, d))))) +export function map4(rx: [Rx, Rx, Rx, Rx], fn: (a: A, b: B, c: C, d: D) => E): Rx { + return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].flatMap(c => rx[3].map(d => fn(a, b, c, d))))) } -export function map5(rx: [Rx, Rx, Rx, Rx, Rx], f: (a: A, b: B, c: C, d: D, e: E) => F): Rx { - return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].flatMap(c => rx[3].flatMap(d => rx[4].map(e => f(a, b, c, d, e)))))) +export function map5(rx: [Rx, Rx, Rx, Rx, Rx], fn: (a: A, b: B, c: C, d: D, e: E) => F): Rx { + return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].flatMap(c => rx[3].flatMap(d => rx[4].map(e => fn(a, b, c, d, e)))))) +} + +export function map6(rx: [Rx, Rx, Rx, Rx, Rx, Rx], fn: (a: A, b: B, c: C, d: D, e: E, f: F) => G): Rx { + return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].flatMap(c => rx[3].flatMap(d => rx[4].flatMap(e => rx[5].map(f => fn(a, b, c, d, e, f))))))) } class Pure extends Rx { @@ -569,6 +588,30 @@ function appendChild(state: State, element: Element, child: Html, lastAdded?: No remove: () => appendRes.remove(), lastAdded: appendRes.lastAdded } + } else if (isWithState6(child)) { + const { init, getChildren } = child + const [ init1, init2, init3, init4, init5, init6 ] = init + const v1 = state.register(init1) + const v2 = state.register(init2) + const v3 = state.register(init3) + const v4 = state.register(init4) + const v5 = state.register(init5) + const v6 = state.register(init6) + const children = getChildren(v1, v2, v3, v4, v5, v6) + const appendRes = appendChild(state, element, children) + return { + cancel: () => { + appendRes.cancel() + state.unregister(v1) + state.unregister(v2) + state.unregister(v3) + state.unregister(v4) + state.unregister(v5) + state.unregister(v6) + }, + remove: () => appendRes.remove(), + lastAdded: appendRes.lastAdded + } } else if (isRx(child)) { const rxBase = document.createTextNode('') appendNode(element, rxBase, lastAdded) @@ -628,6 +671,10 @@ function isWithState5(x: any): x is WithState5 { return x !== undefined && x.type === "WithState5" } +function isWithState6(x: any): x is WithState6 { + return x !== undefined && x.type === "WithState6" +} + function appendNode(base: Element, node: Node, lastAdded?: Node) { if (lastAdded !== undefined) { base.insertBefore(node, lastAdded.nextSibling) -- cgit v1.2.3