diff options
author | Joris | 2025-03-23 15:55:30 +0100 |
---|---|---|
committer | Joris | 2025-03-23 15:55:30 +0100 |
commit | 81bcb1aedc760f8af4bb43e5f96444926dfdd991 (patch) | |
tree | 93dbb49ef858e776f97394d4754de73f73776fe1 /src | |
parent | 9c1c50c30bc4a74851338f9bb844ee625ba0bd65 (diff) |
Add withState6 and map6
Diffstat (limited to 'src')
-rw-r--r-- | src/rx.ts | 63 |
1 files changed, 55 insertions, 8 deletions
@@ -13,6 +13,7 @@ export type Html | WithState3<any, any, any> | WithState4<any, any, any, any> | WithState5<any, any, any, any, any> + | WithState6<any, any, any, any, any, any> | Array<Html> | Rx<Html> @@ -55,6 +56,12 @@ interface WithState5<A, B, C, D, E> { getChildren: (v1: Var<A>, v2: Var<B>, v3: Var<C>, v4: Var<D>, v5: Var<E>) => Html } +interface WithState6<A, B, C, D, E, F> { + type: 'WithState6' + init: [A, B, C, D, E, F] + getChildren: (v1: Var<A>, v2: Var<B>, v3: Var<C>, v4: Var<D>, v5: Var<E>, v6: Var<F>) => Html +} + interface Attributes { [key: string]: Rx<AttributeValue> | AttributeValue } @@ -161,6 +168,14 @@ export function withState5<A, B, C, D, E>(init: [A, B, C, D, E], getChildren: (v } } +export function withState6<A, B, C, D, E, F>(init: [A, B, C, D, E, F], getChildren: (v1: Var<A>, v2: Var<B>, v3: Var<C>, v4: Var<D>, v5: Var<E>, v6: Var<F>) => Html): WithState6<A, B, C, D, E, F> { + return { + type: 'WithState6', + init, + getChildren + } +} + // Rx export type RxAble<A> = Rx<A> | A @@ -175,20 +190,24 @@ export class Rx<A> { } } -export function map2<A, B, C>(rx: [Rx<A>, Rx<B>], f: (a: A, b: B) => C): Rx<C> { - return rx[0].flatMap(a => rx[1].map(b => f(a, b))) +export function map2<A, B, C>(rx: [Rx<A>, Rx<B>], fn: (a: A, b: B) => C): Rx<C> { + return rx[0].flatMap(a => rx[1].map(b => fn(a, b))) } -export function map3<A, B, C, D>(rx: [Rx<A>, Rx<B>, Rx<C>], f: (a: A, b: B, c: C) => D): Rx<D> { - return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].map(c => f(a, b, c)))) +export function map3<A, B, C, D>(rx: [Rx<A>, Rx<B>, Rx<C>], fn: (a: A, b: B, c: C) => D): Rx<D> { + return rx[0].flatMap(a => rx[1].flatMap(b => rx[2].map(c => fn(a, b, c)))) } -export function map4<A, B, C, D, E>(rx: [Rx<A>, Rx<B>, Rx<C>, Rx<D>], f: (a: A, b: B, c: C, d: D) => E): Rx<E> { - 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<A, B, C, D, E>(rx: [Rx<A>, Rx<B>, Rx<C>, Rx<D>], fn: (a: A, b: B, c: C, d: D) => E): Rx<E> { + 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<A, B, C, D, E, F>(rx: [Rx<A>, Rx<B>, Rx<C>, Rx<D>, Rx<E>], f: (a: A, b: B, c: C, d: D, e: E) => F): Rx<F> { - 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<A, B, C, D, E, F>(rx: [Rx<A>, Rx<B>, Rx<C>, Rx<D>, Rx<E>], fn: (a: A, b: B, c: C, d: D, e: E) => F): Rx<F> { + 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<A, B, C, D, E, F, G>(rx: [Rx<A>, Rx<B>, Rx<C>, Rx<D>, Rx<E>, Rx<F>], fn: (a: A, b: B, c: C, d: D, e: E, f: F) => G): Rx<G> { + 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<A> extends Rx<A> { @@ -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<A, B, C, D, E>(x: any): x is WithState5<A, B, C, D, E> { return x !== undefined && x.type === "WithState5" } +function isWithState6<A, B, C, D, E, F>(x: any): x is WithState6<A, B, C, D, E, F> { + return x !== undefined && x.type === "WithState6" +} + function appendNode(base: Element, node: Node, lastAdded?: Node) { if (lastAdded !== undefined) { base.insertBefore(node, lastAdded.nextSibling) |