import { isArray, isObject } from '../../internal/base' import { TERMINATOR, isTerminator, isVacuo } from '../metas' import { Mutation, Data, isAtomLike } from '../atoms' import { pipeAtom, binaryTweenPipeAtom } from '../helpers' import type { Vacuo, Terminator } from '../metas' import type { AtomLikeOfOutput } from '../atoms' import type { IsTuple } from '../../@types' type StringRecord = Record interface ICombineLatestT { >>( ...sources: [[...V]] ): Data> >>( ...sources: [...V] ): Data> >>( sources: V ): Data> } /** * @see {@link arrayCombineLatestT} {@link objectCombineLatestT} */ export const combineLatestT: ICombineLatestT = (...sources: any[]): any => { if (isAtomLike(sources[0]) || isArray(sources[0])) { return arrayCombineLatestT(...sources) } else if (isObject(sources[0])) { return objectCombineLatestT(sources[0]) } else { throw (new TypeError('"sources" are expected to be type of AtomLike | AtomLike[] | Record.')) } } type ExtractAtomValueInArray = IsTuple extends false ? ( V extends Array ? I extends AtomLikeOfOutput ? I['meta'] : V : V ) : V extends [] ? [] : ( V extends [infer A, ...infer B] ? ( B extends [] ? [A extends AtomLikeOfOutput ? I : never] : [A extends AtomLikeOfOutput ? I : never, ...ExtractAtomValueInArray] ) : V ) export interface IArrayCombineLatestT { >>( ...sources: [[...V]] ): Data> >>( ...sources: [...V] ): Data> } /** * @see {@link combineLatestT} */ export const arrayCombineLatestT: IArrayCombineLatestT = >>( ...sources: [...V] | [[...V]] ): Data> => { let preparedSources: Array> = [] if (sources.length === 1 && isArray(sources[0])) { preparedSources = sources[0] } else { preparedSources = sources as Array> } const inputAtoms = preparedSources.map(source => { if (!isAtomLike(source)) { throw (new TypeError('"sources" are expected to be type of "AtomLike".')) } return source }) const length = preparedSources.length interface WrappedData { id: number value: Vacuo | any } const wrapMutations = Array.from({ length }).map((val, idx) => Mutation.ofLiftLeft((prev) => ({ id: idx, value: prev })) ) const wrappedDatas = Array.from({ length }).map(() => Data.empty()) const combineM = Mutation.ofLiftLeft((() => { const _internalStates = Array.from({ length }) const _internalValues = Array.from({ length }) return (prev: WrappedData | Vacuo): any[] | Terminator => { if (isVacuo(prev)) return TERMINATOR if (isVacuo(prev.value)) return TERMINATOR const { id, value } = prev if (isTerminator(value)) return TERMINATOR _internalStates[id] = true _internalValues[id] = value if (_internalStates.every(val => val)) { return [..._internalValues] } else { return TERMINATOR } } })()) const outputD = Data.empty>() pipeAtom(combineM, outputD) wrappedDatas.forEach(data => { pipeAtom(data, combineM) }) wrapMutations.forEach((wrapMutation, idx) => { pipeAtom(wrapMutation, wrappedDatas[idx]) }) inputAtoms.forEach((atom, idx) => { binaryTweenPipeAtom(atom, wrapMutations[idx]) }) return outputD } type ExtractAtomValueInObject = { [P in keyof T]: T[P] extends AtomLikeOfOutput ? V : never } /** * @see {@link combineLatestT} */ export const objectCombineLatestT = >>( sources: V ): Data> => { const inputAtoms = Object.entries(sources).reduce>>( (acc, [key, atom]) => { if (!isAtomLike(atom)) { throw (new TypeError('"sources" are expected to be type of "AtomLike".')) } acc[key] = atom return acc }, {} ) interface WrappedData { key: string value: Vacuo | any } const wrapMutations = Object.entries(sources).reduce>>( (acc, [key]) => { acc[key] = Mutation.ofLiftLeft((prev) => ({ key: key, value: prev })) return acc }, {} ) const wrappedDatas = Object.entries(sources).reduce>>( (acc, [key]) => { acc[key] = Data.empty() return acc }, {} ) const combineM = Mutation.ofLiftLeft((() => { const _internalStates = Object.keys(sources).reduce>((acc, key) => { acc[key] = false return acc }, {}) const _internalValues = Object.keys(sources).reduce>( (acc, key) => { acc[key] = undefined return acc }, {} ) return (prev: Vacuo | WrappedData): StringRecord | Terminator => { if (isVacuo(prev)) return TERMINATOR if (isVacuo(prev.value)) return TERMINATOR const { key, value } = prev if (isTerminator(value)) return TERMINATOR _internalStates[key] = true _internalValues[key] = value if (Object.values(_internalStates).every(val => val)) { return { ..._internalValues } } else { return TERMINATOR } } })()) const outputD = Data.empty>() pipeAtom(combineM, outputD) Object.values(wrappedDatas).forEach(data => { pipeAtom(data, combineM) }) Object.entries(wrapMutations).forEach(([key, wrapMutation]) => { pipeAtom(wrapMutation, wrappedDatas[key]) }) Object.entries(inputAtoms).forEach(([key, atom]) => { binaryTweenPipeAtom(atom, wrapMutations[key]) }) return outputD }