import { isArray, isPlainObject } from '../../internal/base' import { TERMINATOR, isTerminator, isVacuo } from '../metas' import { isAtomLike, Mutation, Data } from '../atoms' import { pipeAtom, binaryTweenPipeAtom } from '../helpers' import type { Terminator, Vacuo } from '../metas' import type { AtomLikeOfOutput } from '../atoms' type StringRecord = Record interface ICombineT { ( ...sources: Array> | [Array>] ): Data> ( source: StringRecord> ): Data> } /** * @see {@link arrayCombineT} {@link objectCombineT} */ export const combineT: ICombineT = (...sources: any[]): any => { if (isAtomLike(sources[0]) || isArray(sources[0])) { return arrayCombineT(...sources) } else if (isPlainObject(sources[0])) { return objectCombineT(sources[0]) } else { throw (new TypeError('"sources" are expected to be type of AtomLike | AtomLike[] | Record.')) } } /** * @see {@link combineT} */ export const arrayCombineT = ( ...sources: Array> | [Array>] ): 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 | V } 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): Array | 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 return [..._internalValues] } })()) const outputD = Data.of>(Array.from({ length })) 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 } /** * @see {@link combineT} */ export const objectCombineT = ( sources: StringRecord> ): 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 | V } 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 return { ..._internalValues } } })()) const outputD = Data.of>(Object.keys(sources).reduce>((acc, key) => { acc[key] = undefined return acc }, {})) 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 }