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 { Vacuo, Terminator } from '../metas' import type { AtomLikeOfOutput, MutationLike } from '../atoms' interface WrappedTo { type: 'to' value: Vacuo | T } interface WrappedFrom { type: 'from' value: Vacuo | F } const switchTacheFactory = ( mutationFactory: () => Mutation | WrappedTo, iT | Terminator> ) => { return (to: AtomLikeOfOutput, from: AtomLikeOfOutput): Data => { if (!isAtomLike(to)) { throw (new TypeError('"to" is expected to be type of "AtomLike".')) } if (!isAtomLike(from)) { throw (new TypeError('"from" is expected to be type of "AtomLike".')) } const wrapToM = Mutation.ofLiftLeft>(prev => ({ type: 'to', value: prev })) const wrappedToD = Data.empty>() pipeAtom(wrapToM, wrappedToD) const wrapFromM = Mutation.ofLiftLeft>(prev => ({ type: 'from', value: prev })) const wrappedFromD = Data.empty>() pipeAtom(wrapFromM, wrappedFromD) // const switchM = Mutation.ofLiftLeft(operation) const switchM = mutationFactory() pipeAtom(wrappedToD, switchM) pipeAtom(wrappedFromD, switchM) const outputD = Data.empty() pipeAtom(switchM, outputD) binaryTweenPipeAtom(to, wrapToM) binaryTweenPipeAtom(from, wrapFromM) return outputD } } /** * switch Tache will emits a "to" value when "from" value comes. * * If there is not "to" value to emit when "from" value comes, * it will emit a TERMINATOR which will stop the stream. * * @see {@link dynamicSwitchT}, {@link staticSwitchT}, {@link promiseSwitchT} */ export const switchT = ( to: AtomLikeOfOutput | T, from: AtomLikeOfOutput ): Data => { if (isAtomLike(to)) { return dynamicSwitchT(to as AtomLikeOfOutput, from) } else { return staticSwitchT(to as T, from) } } interface ISwitchT_ { (to: AtomLikeOfOutput | T, from: AtomLikeOfOutput): Data (to: AtomLikeOfOutput | T): (from: AtomLikeOfOutput) => Data } /** * @see {@link switchT} */ export const switchT_: ISwitchT_ = curryN(2, switchT) /** * @see {@link switchT} */ export const dynamicSwitchT: (to: AtomLikeOfOutput, from: AtomLikeOfOutput) => Data = switchTacheFactory((): Mutation | WrappedTo, T| Terminator> => { return Mutation.ofLiftLeft((() => { const _internalStates: { from: boolean to: boolean } = { from: false, to: false } const _internalValues: { from: F | undefined to: T | undefined } = { from: undefined, to: undefined } return (prev: Vacuo | WrappedFrom | WrappedTo): T | 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.from || !_internalStates.to) { return TERMINATOR } if (type === 'to') { return TERMINATOR } if (type === 'from') { return _internalValues.to! } throw (new TypeError('Unexpected "type".')) } })()) }) interface IDynamicSwitchT_ { (to: AtomLikeOfOutput, from: AtomLikeOfOutput): Data (to: AtomLikeOfOutput): (from: AtomLikeOfOutput) => Data } /** * @see {@link dynamicSwitchT} */ export const dynamicSwitchT_: IDynamicSwitchT_ = curryN(2, dynamicSwitchT) /** * @see {@link switchT} */ export const staticSwitchT = ( to: T, from: AtomLikeOfOutput ): Data => { return dynamicSwitchT(replayWithLatest(1, Data.of(to)), from) } interface IStaticSwitchT_ { (to: T, from: AtomLikeOfOutput): Data (to: T): (from: AtomLikeOfOutput) => Data } /** * @see {@link staticSwitchT} */ export const staticSwitchT_: IStaticSwitchT_ = curryN(2, staticSwitchT) /** * promiseSwitch Tache will emits a "to" value when "from" value comes. * * If there is no "to" value to emit when "from" value comes, * it will make a promise that to emit "to" value when it comes later. * * @see {@link switchT} */ export const promiseSwitchT: (to: AtomLikeOfOutput, from: AtomLikeOfOutput) => Data = switchTacheFactory((): Mutation | WrappedTo, T| Terminator> => { interface PrivateData { type: symbol value: T } const privateDataType = Symbol('privateData') const switchM: Mutation | WrappedTo | PrivateData, T | Terminator> = Mutation.ofLiftLeft((() => { const _internalStates: { from: boolean to: boolean promise: boolean } = { from: false, to: false, promise: false } const _internalValues: { from: F | undefined to: T | undefined } = { from: undefined, to: undefined } return (prev, cur, mutation): T | Terminator => { if (isVacuo(prev)) return TERMINATOR if (isVacuo(prev.value)) return TERMINATOR const { type } = prev if (type === 'from' || type === 'to') { _internalStates[type] = true _internalValues[type] = prev.value as any } if (type === privateDataType) { return prev.value } if (type === 'to') { if (_internalStates.promise) { // mutation.triggerTransformation(() => _internalValues.to) mutation!.mutate(Data.of | WrappedTo | PrivateData>({ type: privateDataType, value: prev.value })) _internalStates.promise = false } return TERMINATOR } if (type === 'from') { if (_internalStates.to) { return _internalValues.to! } else { _internalStates.promise = true return TERMINATOR } } throw (new TypeError('Unexpected "type".')) } })()) return switchM as Mutation | WrappedTo, T | Terminator> }) interface IPromiseSwitchT_ { (to: AtomLikeOfOutput, from: AtomLikeOfOutput): Data (to: AtomLikeOfOutput): (from: AtomLikeOfOutput) => Data } /** * @see {@link promiseSwitchT} */ export const promiseSwitchT_: IPromiseSwitchT_ = curryN(2, promiseSwitchT)