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
+
1
19
import type { Denops } from "jsr:@denops/std@^7.3.2" ;
2
20
import { take } from "jsr:@core/iterutil@^0.9.0/async/take" ;
3
21
import { map } from "jsr:@core/iterutil@^0.9.0/map" ;
@@ -8,17 +26,60 @@ import { Chunker } from "../lib/chunker.ts";
8
26
import { UniqueOrderedList } from "../lib/unique_ordered_list.ts" ;
9
27
import { dispatch } from "../event.ts" ;
10
28
29
+ /** Default maximum number of items to collect */
11
30
const THRESHOLD = 100000 ;
31
+
32
+ /** Default number of items to process in each chunk */
12
33
const CHUNK_SIZE = 1000 ;
34
+
35
+ /** Default interval in milliseconds between chunk updates */
13
36
const CHUNK_INTERVAL = 100 ;
14
37
38
+ /**
39
+ * Configuration options for the CollectProcessor.
40
+ *
41
+ * @template T - The type of detail data associated with each item
42
+ */
15
43
export type CollectProcessorOptions < T extends Detail > = {
44
+ /** Initial items to populate the processor with (useful for resume) */
16
45
initialItems ?: readonly IdItem < T > [ ] ;
46
+
47
+ /** Maximum number of items to collect (default: 100000) */
17
48
threshold ?: number ;
49
+
50
+ /** Number of items to process before emitting an update (default: 1000) */
18
51
chunkSize ?: number ;
52
+
53
+ /** Maximum time in ms between updates regardless of chunk size (default: 100) */
19
54
chunkInterval ?: number ;
20
55
} ;
21
56
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
+ */
22
83
export class CollectProcessor < T extends Detail > implements Disposable {
23
84
readonly #controller: AbortController = new AbortController ( ) ;
24
85
readonly #items: UniqueOrderedList < IdItem < T > > ;
@@ -28,6 +89,12 @@ export class CollectProcessor<T extends Detail> implements Disposable {
28
89
#processing?: Promise < void > ;
29
90
#paused?: PromiseWithResolvers < void > ;
30
91
92
+ /**
93
+ * Creates a new CollectProcessor.
94
+ *
95
+ * @param source - The source to collect items from
96
+ * @param options - Configuration options
97
+ */
31
98
constructor (
32
99
readonly source : Source < T > ,
33
100
options : CollectProcessorOptions < T > = { } ,
@@ -45,10 +112,20 @@ export class CollectProcessor<T extends Detail> implements Disposable {
45
112
) ;
46
113
}
47
114
115
+ /**
116
+ * Gets the currently collected items.
117
+ *
118
+ * @returns An array of collected items with assigned IDs
119
+ */
48
120
get items ( ) : readonly IdItem < T > [ ] {
49
121
return this . #items. items ;
50
122
}
51
123
124
+ /**
125
+ * Validates that the processor is not disposed.
126
+ *
127
+ * @throws Error if the processor is disposed
128
+ */
52
129
#validateAvailability( ) : void {
53
130
try {
54
131
this . #controller. signal . throwIfAborted ( ) ;
@@ -60,6 +137,26 @@ export class CollectProcessor<T extends Detail> implements Disposable {
60
137
}
61
138
}
62
139
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
+ */
63
160
start (
64
161
denops : Denops ,
65
162
params : CollectParams ,
@@ -107,6 +204,20 @@ export class CollectProcessor<T extends Detail> implements Disposable {
107
204
} ) ;
108
205
}
109
206
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
+ */
110
221
pause ( ) : void {
111
222
this . #validateAvailability( ) ;
112
223
if ( ! this . #processing) {
@@ -118,6 +229,9 @@ export class CollectProcessor<T extends Detail> implements Disposable {
118
229
} ) ;
119
230
}
120
231
232
+ /**
233
+ * Resumes a paused collection process.
234
+ */
121
235
#resume( ) : void {
122
236
if ( ! this . #paused) {
123
237
return ;
@@ -126,6 +240,12 @@ export class CollectProcessor<T extends Detail> implements Disposable {
126
240
this . #paused = undefined ;
127
241
}
128
242
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
+ */
129
249
[ Symbol . dispose ] ( ) : void {
130
250
try {
131
251
this . #controller. abort ( null ) ;
0 commit comments