staging-tester / ocean-landing-serve / src/ lib/ live/ removals.svelte.ts

import { untrack } from 'svelte';

// Calls `remove` for every item that leaves `list`. Settings rows remove items from local state in their markup, so
// the matching request is sent from here.
export function onRemoved<T extends { id: string }>(list: () => T[], remove: (id: string) => Promise<unknown>) {
	let known = new Set(untrack(() => list()).map((item) => item.id));
	$effect(() => {
		const now = new Set(list().map((item) => item.id));
		for (const id of known) if (!now.has(id)) void remove(id);
		known = now;
	});
}