import { createMapArrayState, MapArrayFn, MapArrayKeyFn, mapArrayUpdate } from "./internals/map-array.js"; import { $obj, Expression, get, watch } from "./signals.js"; export { MapArrayFn, MapArrayKeyFn } from "./internals/map-array.js"; /** * Map each entry in an iterable expression until the current lifecycle is disposed. * * + The current context is available in the map function. * + Evaluation is stopped when the current lifecycle is disposed. * + Teardown hooks from within the map function are called when an entry is removed or when the current lifecycle is disposed. * + Returns an expression to access the latest result. * * @param keyFn A function to get the key for a given value. If not specified, the value is used as key. */ export function mapArray(inputs: Expression>, fn: MapArrayFn, O>, keyFn?: MapArrayKeyFn>): () => O[] { const state = createMapArrayState(); const output = $obj([]); watch(() => { const update = mapArrayUpdate(state, get(inputs), fn, keyFn); if (update) { output.inert.splice(update.s, update.p.length, ...update.n.map(s => s.o)); output.notify(); } }); return () => output.value; }