import type { Signal, ValueReactor } from "../reactor-core/index.ts" import { effect, signal } from "../reactor-core/index.ts" import { castValueInitializer } from "./utility.ts" export interface CloneOptions { target: ValueReactor } /** * @description Creates a Signal that mirrors the value of the target ValueReactor. */ export const clone = (options: CloneOptions): Signal => { const { target } = options const result = signal(castValueInitializer(), { onDispose: () => { eTarget.dispose() }, }) const eTarget = effect(() => { const value = target.get() result.set(value) }) return result } export interface PartitionOptions { target: ValueReactor predicate: (value: V) => boolean } /** * @description Partitions values from a ValueReactor into two Signals based on a predicate function. * The first Signal emits values that satisfy the predicate, while the second Signal * emits values that do not satisfy the predicate. */ export const partition = ( options: PartitionOptions, ): [Signal, Signal] => { const { target, predicate } = options const trueSignal = signal(castValueInitializer(), { onDispose: () => { eTarget.dispose() }, }) const falseSignal = signal(castValueInitializer(), { onDispose: () => { eTarget.dispose() }, }) const eTarget = effect(() => { const value = target.get() if (predicate(value) === true) { trueSignal.set(value) } else { falseSignal.set(value) } }) return [trueSignal, falseSignal] }