Skip to content

Commit e9082f3

Browse files
committed
Use UniqueOrderedList and add initialItems to CollectProcessor
1 parent d381b95 commit e9082f3

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

denops/fall/lib/unique_ordered_list_test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ Deno.test("UniqueOrderedList: initializes with items", () => {
6767
{ id: 1, name: "Alice" },
6868
{ id: 2, name: "Bob" },
6969
]);
70-
70+
7171
// Pushing duplicate IDs is properly prevented
7272
list.push({ id: 1, name: "Alice (another dup)" });
7373
assertEquals(list.size, 2); // Still 2, duplicate was ignored
7474
});
7575

7676
Deno.test("UniqueOrderedList: works without identifier function", () => {
7777
const list = new UniqueOrderedList<number>([1, 2, 3, 2, 1]); // Has duplicates
78-
78+
7979
// Initial duplicates are filtered
8080
assertEquals(list.size, 3);
8181
assertEquals(list.items, [1, 2, 3]);
82-
82+
8383
list.push(4, 2, 5); // 2 is duplicate and will be ignored
8484
assertEquals(list.size, 5);
8585
assertEquals(list.items, [1, 2, 3, 4, 5]);

denops/fall/processor/collect.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ import type { Detail, IdItem } from "jsr:@vim-fall/core@^0.3.0/item";
55
import type { CollectParams, Source } from "jsr:@vim-fall/core@^0.3.0/source";
66

77
import { Chunker } from "../lib/chunker.ts";
8+
import { UniqueOrderedList } from "../lib/unique_ordered_list.ts";
89
import { dispatch } from "../event.ts";
910

1011
const THRESHOLD = 100000;
1112
const CHUNK_SIZE = 1000;
1213
const 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

2022
export 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

Comments
 (0)