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 { Vacuo, Terminator } from '../metas' import type { AtomLikeOfOutput } from '../atoms' type Effect = ( emit: (value: V) => void, target: T, immediateReturn: (value: V) => void ) => any /** * `effectT` transfer the value's emittion authority to the `effect` function, * so developer can control whether, when and what value will be emitted. * * @param effect A function that controls the value's emittion. * When there is a target value received, the effect function will be invoked. * It takes an emit function as first argument, invoke it with the value to emit it. * And takes the target value as second argument, an value set function * whose value will be emitted just after effect function's execution. * @param target Atom * @return Data * * @see {@link dynamicEffectT}, {@link staticEffectT} */ export const effectT = ( effect: Effect, target: AtomLikeOfOutput ): Data => { if (isFunction(effect)) { return staticEffectT(effect, target) } else if (isAtomLike(effect)) { return dynamicEffectT(effect, target) } else { throw (new TypeError('"effect" is expected to be type of "Function" or "AtomLike".')) } } interface IEffectT_ { (effect: Effect, target: AtomLikeOfOutput): Data (effect: Effect): (target: AtomLikeOfOutput) => Data } /** * @see {@link effectT} */ export const effectT_: IEffectT_ = curryN(2, effectT) /** * @see {@link effectT} */ export const dynamicEffectT = ( effect: AtomLikeOfOutput>, target: AtomLikeOfOutput ): Data => { if (!isAtomLike(effect)) { throw (new TypeError('"effect" is expected to be type of "AtomLike".')) } if (!isAtomLike(target)) { throw (new TypeError('"target" is expected to be type of "AtomLike".')) } interface WrappedEffect { type: 'effect' value: Vacuo | Effect } const wrapEffectM = Mutation.ofLiftLeft, WrappedEffect>(prev => ({ type: 'effect', value: prev })) const wrappedEffectD = Data.empty() pipeAtom(wrapEffectM, wrappedEffectD) interface WrappedTarget { type: 'target' value: Vacuo | T } const wrapTargetM = Mutation.ofLiftLeft(prev => ({ type: 'target', value: prev })) const wrappedTargetD = Data.empty() pipeAtom(wrapTargetM, wrappedTargetD) interface PrivateData { type: symbol value: V } const privateDataType = Symbol('privateData') const effectM = Mutation.ofLiftLeft((() => { const _internalStates: { effect: boolean target: boolean } = { effect: false, target: false } const _internalValues: { effect: Effect | undefined target: T | undefined } = { effect: undefined, target: undefined } return (prev: Vacuo | WrappedEffect | WrappedTarget | PrivateData): V | Terminator => { if (isVacuo(prev)) return TERMINATOR if (isVacuo(prev.value)) return TERMINATOR const { type } = prev if (type === 'effect') { _internalStates[type] = true if (!isFunction(prev.value)) { throw (new TypeError('"effect" is expected to be type of "Function".')) } _internalValues[type] = prev.value } else if (type === 'target') { _internalStates[type] = true _internalValues[type] = prev.value } else if (type === privateDataType) { return prev.value } else { throw (new TypeError('Unexpected "type".')) } if (!_internalStates.effect || !_internalStates.target) { return TERMINATOR } if (type === 'effect') { return TERMINATOR } else if (type === 'target') { let hasSetReturned = false let _returned: V | Terminator = TERMINATOR const result = _internalValues.effect!( (value) => { // Using "mutate" instead of "triggerTransformation" so that the "effectMutation" can update it's datar. // effectM.triggerTransformation(() => value) effectM.mutate(Data.of({ type: privateDataType, value })) }, _internalValues.target!, (returned) => { hasSetReturned = true _returned = returned } ) return hasSetReturned ? _returned : (result ?? TERMINATOR) } else { throw (new TypeError('Unexpected "type".')) } } })()) pipeAtom(wrappedEffectD, effectM) pipeAtom(wrappedTargetD, effectM) const outputD = Data.empty() pipeAtom(effectM, outputD) binaryTweenPipeAtom(effect, wrapEffectM) binaryTweenPipeAtom(target, wrapTargetM) return outputD } interface IDynamicEffectT_ { (effect: AtomLikeOfOutput>, target: AtomLikeOfOutput): Data (effect: AtomLikeOfOutput>): (target: AtomLikeOfOutput) => Data } /** * @see {@link dynamicEffectT} */ export const dynamicEffectT_: IDynamicEffectT_ = curryN(2, dynamicEffectT) /** * @see {@link effectT} */ export const staticEffectT = ( effect: Effect, target: AtomLikeOfOutput ): Data => { if (!isFunction(effect)) { throw (new TypeError('"effect" is expected to be type of "Function".')) } return dynamicEffectT(replayWithLatest(1, Data.of(effect)), target) } interface IStaticEffectT_ { (effect: Effect, target: AtomLikeOfOutput): Data (effect: Effect): (target: AtomLikeOfOutput) => Data } /** * @see {@link staticEffectT} */ export const staticEffectT_: IStaticEffectT_ = curryN(2, staticEffectT)