import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { getRegistry } from "yt-dlp-transcript-common/jobs/registry"; import { READ_X_TRENDING_KIND, REPARSE_X_TRENDING_KIND } from "yt-dlp-transcript-common/lib/xTrending/config"; import { isXTrendingLibrarySync } from "yt-dlp-transcript-common/lib/xTrending/library-server"; import { armXTrendingWatch, type WatchScanDeps } from "yt-dlp-transcript-common/controller/xTrendingWatch"; import { readXTrendingAction } from "../../channels/[slug]/xTrendingActions"; // THE WATCH'S EDITOR SIDE (lane x-trending-2): what the scan // (common/controller/xTrendingWatch.ts) runs on in this editor — its job // registry and its own read action, so a tick is exactly the job a person // starts — shared by `watch-trending {"tick": true}` and the timer // instrumentation.ts arms. export function xTrendingWatchDeps(): WatchScanDeps { const paths = getPaths(); return { paths, log: (line) => console.log(line), // A trending read, a reparse, or a capture of a LIBRARY, waiting its turn // or running — whatever the library (review 1 M3): every library's ticks // are one stream, so the scan never stacks one read per library on X's // queue. busy: () => xTrendingJobWaiting(getRegistry().list(), (slug) => isXTrendingLibrarySync(paths.channelsDir, slug)), enqueue: async (tick) => { // Marked as the watch's: its job asks the watch's jar rule at its start. const started = await readXTrendingAction({ slug: tick.slug, url: tick.url, topic: tick.topic, watch: true, ...(tick.tabs ? { tabs: tick.tabs } : {}) }); if (!started.ok) return { ok: false, error: started.error }; void started.stream.cancel(); return { ok: true, jobId: started.jobId }; }, }; } type JobLike = { kind: string; status: string; channelSlug?: string }; // A trending job of ANY library that is queued or running. export function xTrendingJobWaiting(jobs: ReadonlyArray, isLibrary: (slug: string) => boolean): boolean { return jobs.some( (j) => (j.status === "queued" || j.status === "running") && (j.kind === READ_X_TRENDING_KIND || j.kind === REPARSE_X_TRENDING_KIND || (j.kind === "capture-posts" && !!j.channelSlug && isLibrary(j.channelSlug))), ); } // Armed once per process, below the idle gate (armXTrendingWatch refuses an // idle boot itself too). export function armXTrendingWatchLane(): boolean { return armXTrendingWatch(xTrendingWatchDeps()); }