import { isFunction } from '../../internal/base' import { curryN } from '../../functional' import { TERMINATOR, isVacuo } from '../metas' import { Data, Mutation, isAtomLike } from '../atoms' import { replayWithLatest } from '../mediators' import { pipeAtom, binaryTweenPipeAtom } from '../helpers' import type { Terminator, Vacuo } from '../metas' import type { AtomLikeOfOutput } from '../atoms' type Mapper = (tar: T, index: number) => V /** * @see {@link dynamicMapT}, {@link staticMapT} */ export const mapT = ( mapper: AtomLikeOfOutput> | Mapper, target: AtomLikeOfOutput ): Data => { if (isFunction(mapper)) { return staticMapT(mapper, target) } else if (isAtomLike(mapper)) { return dynamicMapT(mapper, target) } else { throw (new TypeError('"mapper" is expected to be type of "Function" or "AtomLike".')) } } export interface IMapT_ { (mapper: AtomLikeOfOutput> | Mapper, target: AtomLikeOfOutput): Data (mapper: AtomLikeOfOutput> | Mapper): (target: AtomLikeOfOutput) => Data } /** * @see {@link mapT} */ export const mapT_: IMapT_ = curryN(2, mapT) /** * @see {@link mapT} */ export const dynamicMapT = ( mapper: AtomLikeOfOutput>, target: AtomLikeOfOutput ): Data => { if (!isAtomLike(mapper)) { throw (new TypeError('"mapper" is expected to be type of "AtomLike".')) } if (!isAtomLike(target)) { throw (new TypeError('"target" is expected to be type of "AtomLike".')) } interface WrappedMapper { type: 'mapper' value: Vacuo | Mapper } const wrapMapM = Mutation.ofLiftLeft, WrappedMapper>(prev => ({ type: 'mapper', value: prev })) const wrappedMapD = Data.empty() pipeAtom(wrapMapM, wrappedMapD) interface WrappedTarget { type: 'target' value: Vacuo | T } const wrapTargetM = Mutation.ofLiftLeft(prev => ({ type: 'target', value: prev })) const wrappedTargetD = Data.empty() pipeAtom(wrapTargetM, wrappedTargetD) const mapM = Mutation.ofLiftLeft((() => { const _internalStates: { mapper: boolean target: boolean index: number } = { mapper: false, target: false, index: -1 } const _internalValues: { mapper: Mapper | undefined target: T | undefined } = { mapper: undefined, target: undefined } return (prev: Vacuo | WrappedMapper | WrappedTarget): V | Terminator => { if (isVacuo(prev)) return TERMINATOR if (isVacuo(prev.value)) return TERMINATOR const { type } = prev _internalStates[type] = true _internalValues[type] = prev.value as any if (!_internalStates.mapper || !_internalStates.target) { return TERMINATOR } if (type === 'mapper') { return TERMINATOR } if (type === 'target') { _internalStates.index = _internalStates.index + 1 return _internalValues.mapper!(_internalValues.target!, _internalStates.index) } throw (new TypeError('Unexpected "type".')) } })()) pipeAtom(wrappedMapD, mapM) pipeAtom(wrappedTargetD, mapM) const outputD = Data.empty() pipeAtom(mapM, outputD) binaryTweenPipeAtom(mapper, wrapMapM) binaryTweenPipeAtom(target, wrapTargetM) return outputD } export interface IDynamicMapT_ { (mapper: AtomLikeOfOutput>, target: AtomLikeOfOutput): Data (mapper: AtomLikeOfOutput>): (target: AtomLikeOfOutput) => Data } /** * @see {@link dynamicMapT} */ export const dynamicMapT_: IDynamicMapT_ = curryN(2, dynamicMapT) /** * @see {@link mapT} */ export const staticMapT = ( mapper: Mapper, target: AtomLikeOfOutput ): Data => { if (!isFunction(mapper)) { throw (new TypeError('"mapper" is expected to be type of "Function".')) } return dynamicMapT(replayWithLatest(1, Data.of(mapper)), target) } export interface IStaticMapT_ { (mapper: Mapper, target: AtomLikeOfOutput): Data (mapper: Mapper): (target: AtomLikeOfOutput) => Data } /** * @see {@link staticMapT} */ export const staticMapT_: IStaticMapT_ = curryN(2, staticMapT)