import { curryN } from '../../functional' import { TERMINATOR, isVacuo } from '../metas' import { Data, Mutation, isAtomLike } from '../atoms' import { pipeAtom, binaryTweenPipeAtom } from '../helpers' import type { Vacuo, Terminator } from '../metas' import type { AtomLikeOfOutput } from '../atoms' /** * Take target atom's value while condition atom's value is `true`. */ export const takeWhileT = ( condition: AtomLikeOfOutput, target: AtomLikeOfOutput ): Data => { if (!isAtomLike(condition)) { throw (new TypeError('"condition" is expected to be type of "AtomLike".')) } if (!isAtomLike(target)) { throw (new TypeError('"target" is expected to be type of "AtomLike".')) } interface WrappedCondition { type: 'condition' value: Vacuo | boolean } const wrapConditionM = Mutation.ofLiftLeft(prev => ({ type: 'condition', value: Boolean(prev) })) const wrappedConditionD = Data.empty() pipeAtom(wrapConditionM, wrappedConditionD) interface WrappedTarget { type: 'target' value: Vacuo | V } const wrapTargetM = Mutation.ofLiftLeft(prev => ({ type: 'target', value: prev })) const wrappedTargetD = Data.empty() pipeAtom(wrapTargetM, wrappedTargetD) const takeM = Mutation.ofLiftLeft((() => { const _internalStates: { condition: boolean target: boolean } = { condition: false, target: false } const _internalValues: { condition: boolean target: V | undefined } = { condition: false, target: undefined } return (prev: Vacuo | WrappedCondition | 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.condition || !_internalStates.target) { return TERMINATOR } if (type === 'condition') { return TERMINATOR } if (type === 'target') { return _internalValues.condition ? _internalValues.target! : TERMINATOR } throw (new TypeError('Unexpected "type".')) } })()) pipeAtom(wrappedConditionD, takeM) pipeAtom(wrappedTargetD, takeM) const outputD = Data.empty() pipeAtom(takeM, outputD) binaryTweenPipeAtom(condition, wrapConditionM) binaryTweenPipeAtom(target, wrapTargetM) return outputD } interface ITakeWhileT_ { (condition: AtomLikeOfOutput, target: AtomLikeOfOutput): Data (condition: AtomLikeOfOutput): (target: AtomLikeOfOutput) => Data } /** * @see {@link takeWhileT} */ export const takeWhileT_: ITakeWhileT_ = curryN(2, takeWhileT)