@@ -5,21 +5,23 @@ import type { Detail, IdItem } from "jsr:@vim-fall/core@^0.3.0/item";
55import type { CollectParams , Source } from "jsr:@vim-fall/core@^0.3.0/source" ;
66
77import { Chunker } from "../lib/chunker.ts" ;
8+ import { UniqueOrderedList } from "../lib/unique_ordered_list.ts" ;
89import { dispatch } from "../event.ts" ;
910
1011const THRESHOLD = 100000 ;
1112const CHUNK_SIZE = 1000 ;
1213const CHUNK_INTERVAL = 100 ;
1314
14- export type CollectProcessorOptions = {
15+ export type CollectProcessorOptions < T extends Detail > = {
16+ initialItems ?: readonly IdItem < T > [ ] ;
1517 threshold ?: number ;
1618 chunkSize ?: number ;
1719 chunkInterval ?: number ;
1820} ;
1921
2022export class CollectProcessor < T extends Detail > implements Disposable {
2123 readonly #controller: AbortController = new AbortController ( ) ;
22- readonly #items: IdItem < T > [ ] = [ ] ;
24+ readonly #items: UniqueOrderedList < IdItem < T > > ;
2325 readonly #threshold: number ;
2426 readonly #chunkSize: number ;
2527 readonly #chunkInterval: number ;
@@ -28,15 +30,23 @@ export class CollectProcessor<T extends Detail> implements Disposable {
2830
2931 constructor (
3032 readonly source : Source < T > ,
31- options : CollectProcessorOptions = { } ,
33+ options : CollectProcessorOptions < T > = { } ,
3234 ) {
3335 this . #threshold = options . threshold ?? THRESHOLD ;
3436 this . #chunkSize = options . chunkSize ?? CHUNK_SIZE ;
3537 this . #chunkInterval = options . chunkInterval ?? CHUNK_INTERVAL ;
38+ this . #items = new UniqueOrderedList < IdItem < T > > (
39+ options . initialItems ?? [ ] ,
40+ {
41+ // We need to compare "value" rather than "id" for uniqueness,
42+ // to implement the "resume" functionality correctly.
43+ identifier : ( item ) => item . value ,
44+ } ,
45+ ) ;
3646 }
3747
38- get items ( ) {
39- return this . #items;
48+ get items ( ) : readonly IdItem < T > [ ] {
49+ return this . #items. items ;
4050 }
4151
4252 #validateAvailability( ) : void {
@@ -67,7 +77,7 @@ export class CollectProcessor<T extends Detail> implements Disposable {
6777 this . #threshold,
6878 ) ;
6979 const update = ( chunk : Iterable < IdItem < T > > ) => {
70- const offset = this . #items. length ;
80+ const offset = this . #items. size ;
7181 this . #items. push (
7282 ...map ( chunk , ( item , i ) => ( { ...item , id : i + offset } ) ) ,
7383 ) ;
0 commit comments