import type { CollectorPackage } from "../shared/collector"; export type CollectorEnrichmentGenerationSnapshot = Map; function collectorUrlKey(url: string): string { return url.trim(); } export function beginCollectorEnrichment( packages: CollectorPackage[], current: Map ): CollectorEnrichmentGenerationSnapshot { const snapshot = new Map(); for (const link of packages.flatMap((pkg) => pkg.links)) { const url = collectorUrlKey(link.url); const generation = (current.get(url) ?? 0) + 1; current.set(url, generation); snapshot.set(url, generation); } return snapshot; } export function pruneCollectorEnrichmentGenerations( current: Map, packages: CollectorPackage[], activeSnapshots: Iterable> ): void { const retainedUrls = new Set(packages.flatMap((pkg) => pkg.links.map((link) => collectorUrlKey(link.url)))); for (const snapshot of activeSnapshots) { for (const url of snapshot.keys()) retainedUrls.add(url); } for (const url of current.keys()) { if (!retainedUrls.has(url)) current.delete(url); } } export function filterCurrentCollectorEnrichment( packages: CollectorPackage[], requested: CollectorEnrichmentGenerationSnapshot, current: ReadonlyMap ): CollectorPackage[] { return packages.flatMap((pkg) => { const links = pkg.links.filter((link) => { const url = collectorUrlKey(link.url); return requested.get(url) === current.get(url); }); return links.length > 0 ? [{ ...pkg, links }] : []; }); }