1+ /**
2+ * @module processor/collect
3+ *
4+ * Collection processor for vim-fall.
5+ *
6+ * This module provides the CollectProcessor class which manages the collection
7+ * of items from a source. It handles:
8+ *
9+ * - Asynchronous item collection with progress updates
10+ * - Chunking for performance optimization
11+ * - Item deduplication and ID assignment
12+ * - Pause/resume functionality
13+ * - Threshold limiting to prevent memory issues
14+ *
15+ * The processor emits events during collection to update the UI and coordinate
16+ * with other components.
17+ */
18+
119import type { Denops } from "jsr:@denops/std@^7.3.2" ;
220import { take } from "jsr:@core/iterutil@^0.9.0/async/take" ;
321import { map } from "jsr:@core/iterutil@^0.9.0/map" ;
@@ -8,17 +26,60 @@ import { Chunker } from "../lib/chunker.ts";
826import { UniqueOrderedList } from "../lib/unique_ordered_list.ts" ;
927import { dispatch } from "../event.ts" ;
1028
29+ /** Default maximum number of items to collect */
1130const THRESHOLD = 100000 ;
31+
32+ /** Default number of items to process in each chunk */
1233const CHUNK_SIZE = 1000 ;
34+
35+ /** Default interval in milliseconds between chunk updates */
1336const CHUNK_INTERVAL = 100 ;
1437
38+ /**
39+ * Configuration options for the CollectProcessor.
40+ *
41+ * @template T - The type of detail data associated with each item
42+ */
1543export type CollectProcessorOptions < T extends Detail > = {
44+ /** Initial items to populate the processor with (useful for resume) */
1645 initialItems ?: readonly IdItem < T > [ ] ;
46+
47+ /** Maximum number of items to collect (default: 100000) */
1748 threshold ?: number ;
49+
50+ /** Number of items to process before emitting an update (default: 1000) */
1851 chunkSize ?: number ;
52+
53+ /** Maximum time in ms between updates regardless of chunk size (default: 100) */
1954 chunkInterval ?: number ;
2055} ;
2156
57+ /**
58+ * Processor responsible for collecting items from a source.
59+ *
60+ * The CollectProcessor manages the asynchronous collection of items from a source,
61+ * handling chunking, deduplication, and progress updates. It can be paused and
62+ * resumed, making it suitable for long-running collection operations.
63+ *
64+ * @template T - The type of detail data associated with each item
65+ *
66+ * @example
67+ * ```typescript
68+ * const processor = new CollectProcessor(fileSource, {
69+ * threshold: 50000,
70+ * chunkSize: 500,
71+ * });
72+ *
73+ * // Start collecting
74+ * processor.start(denops, { args: ["--hidden"] });
75+ *
76+ * // Access collected items
77+ * console.log(processor.items.length);
78+ *
79+ * // Pause if needed
80+ * processor.pause();
81+ * ```
82+ */
2283export class CollectProcessor < T extends Detail > implements Disposable {
2384 readonly #controller: AbortController = new AbortController ( ) ;
2485 readonly #items: UniqueOrderedList < IdItem < T > > ;
@@ -28,6 +89,12 @@ export class CollectProcessor<T extends Detail> implements Disposable {
2889 #processing?: Promise < void > ;
2990 #paused?: PromiseWithResolvers < void > ;
3091
92+ /**
93+ * Creates a new CollectProcessor.
94+ *
95+ * @param source - The source to collect items from
96+ * @param options - Configuration options
97+ */
3198 constructor (
3299 readonly source : Source < T > ,
33100 options : CollectProcessorOptions < T > = { } ,
@@ -45,10 +112,20 @@ export class CollectProcessor<T extends Detail> implements Disposable {
45112 ) ;
46113 }
47114
115+ /**
116+ * Gets the currently collected items.
117+ *
118+ * @returns An array of collected items with assigned IDs
119+ */
48120 get items ( ) : readonly IdItem < T > [ ] {
49121 return this . #items. items ;
50122 }
51123
124+ /**
125+ * Validates that the processor is not disposed.
126+ *
127+ * @throws Error if the processor is disposed
128+ */
52129 #validateAvailability( ) : void {
53130 try {
54131 this . #controller. signal . throwIfAborted ( ) ;
@@ -60,6 +137,26 @@ export class CollectProcessor<T extends Detail> implements Disposable {
60137 }
61138 }
62139
140+ /**
141+ * Starts or resumes the collection process.
142+ *
143+ * If collection is already in progress and paused, this will resume it.
144+ * Otherwise, it starts a new collection process.
145+ *
146+ * The method emits the following events:
147+ * - `collect-processor-started`: When collection begins
148+ * - `collect-processor-updated`: When new items are collected
149+ * - `collect-processor-succeeded`: When collection completes successfully
150+ * - `collect-processor-failed`: If an error occurs
151+ *
152+ * @param denops - The Denops instance
153+ * @param params - Parameters to pass to the source's collect method
154+ *
155+ * @example
156+ * ```typescript
157+ * processor.start(denops, { args: ["--hidden", "--no-ignore"] });
158+ * ```
159+ */
63160 start (
64161 denops : Denops ,
65162 params : CollectParams ,
@@ -107,6 +204,20 @@ export class CollectProcessor<T extends Detail> implements Disposable {
107204 } ) ;
108205 }
109206
207+ /**
208+ * Pauses the collection process.
209+ *
210+ * The collection can be resumed by calling `start()` again.
211+ * This is useful for temporarily stopping collection to free up
212+ * resources or when the user navigates away.
213+ *
214+ * @example
215+ * ```typescript
216+ * processor.pause();
217+ * // Later...
218+ * processor.start(denops, params); // Resumes from where it left off
219+ * ```
220+ */
110221 pause ( ) : void {
111222 this . #validateAvailability( ) ;
112223 if ( ! this . #processing) {
@@ -118,6 +229,9 @@ export class CollectProcessor<T extends Detail> implements Disposable {
118229 } ) ;
119230 }
120231
232+ /**
233+ * Resumes a paused collection process.
234+ */
121235 #resume( ) : void {
122236 if ( ! this . #paused) {
123237 return ;
@@ -126,6 +240,12 @@ export class CollectProcessor<T extends Detail> implements Disposable {
126240 this . #paused = undefined ;
127241 }
128242
243+ /**
244+ * Disposes of the processor and cancels any ongoing collection.
245+ *
246+ * This method is called automatically when the processor is no longer needed.
247+ * It aborts the collection process and cleans up resources.
248+ */
129249 [ Symbol . dispose ] ( ) : void {
130250 try {
131251 this . #controller. abort ( null ) ;
0 commit comments