Skip to content

Commit be40717

Browse files
committed
Add initialItems and initialQuery to MatchProcessor
1 parent e9082f3 commit be40717

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

denops/fall/processor/match.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const THRESHOLD = 100000;
1313
const CHUNK_SIZE = 1000;
1414
const CHUNK_INTERVAL = 100;
1515

16-
export type MatchProcessorOptions = {
16+
export type MatchProcessorOptions<T extends Detail> = {
17+
initialItems?: readonly IdItem<T>[];
18+
initialQuery?: string;
1719
initialIndex?: number;
1820
interval?: number;
1921
threshold?: number;
@@ -32,11 +34,12 @@ export class MatchProcessor<T extends Detail> implements Disposable {
3234
#controller: AbortController = new AbortController();
3335
#processing?: Promise<void>;
3436
#reserved?: () => void;
35-
#items: IdItem<T>[] = [];
37+
#items: IdItem<T>[];
38+
#previousQuery?: string;
3639

3740
constructor(
3841
matchers: readonly [Matcher<T>, ...Matcher<T>[]],
39-
options: MatchProcessorOptions = {},
42+
options: MatchProcessorOptions<T> = {},
4043
) {
4144
this.matchers = new ItemBelt(matchers, {
4245
index: options.initialIndex,
@@ -46,6 +49,8 @@ export class MatchProcessor<T extends Detail> implements Disposable {
4649
this.#chunkSize = options.chunkSize ?? CHUNK_SIZE;
4750
this.#chunkInterval = options.chunkInterval ?? CHUNK_INTERVAL;
4851
this.#incremental = options.incremental ?? false;
52+
this.#items = options.initialItems?.slice() ?? [];
53+
this.#previousQuery = options.initialQuery;
4954
}
5055

5156
get #matcher(): Matcher<T> {
@@ -88,7 +93,12 @@ export class MatchProcessor<T extends Detail> implements Disposable {
8893
options?: { restart?: boolean },
8994
): void {
9095
this.#validateAvailability();
91-
if (this.#processing) {
96+
if (query === this.#previousQuery) {
97+
if (!this.#processing) {
98+
dispatch({ type: "match-processor-succeeded" });
99+
}
100+
return;
101+
} else if (this.#processing) {
92102
// Keep most recent start request for later.
93103
this.#reserved = () => this.start(denops, { items, query }, options);
94104
// If restart is requested, we need to abort the current processing.
@@ -101,6 +111,7 @@ export class MatchProcessor<T extends Detail> implements Disposable {
101111
}
102112
this.#processing = (async () => {
103113
dispatch({ type: "match-processor-started" });
114+
this.#previousQuery = query;
104115
const signal = this.#controller.signal;
105116
const iter = take(
106117
this.#matcher.match(denops, { items, query }, { signal }),

denops/fall/processor/match_test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,19 +399,19 @@ Deno.test("MatchProcessor", async (t) => {
399399
);
400400
processor.start(denops, {
401401
items: [{ id: 0, value: "0", detail: {} }],
402-
query: "",
402+
query: "q0",
403403
});
404404
processor.start(denops, {
405405
items: [{ id: 1, value: "1", detail: {} }],
406-
query: "",
406+
query: "q1",
407407
});
408408
processor.start(denops, {
409409
items: [{ id: 2, value: "2", detail: {} }],
410-
query: "",
410+
query: "q2",
411411
});
412412
processor.start(denops, {
413413
items: [{ id: 3, value: "3", detail: {} }],
414-
query: "",
414+
query: "q3",
415415
});
416416

417417
assertEquals(called, []);
@@ -450,32 +450,32 @@ Deno.test("MatchProcessor", async (t) => {
450450
);
451451
processor.start(denops, {
452452
items: [{ id: 0, value: "0", detail: {} }],
453-
query: "",
453+
query: "q0",
454454
}, { restart: true });
455455
processor.start(denops, {
456456
items: [{ id: 1, value: "1", detail: {} }],
457-
query: "",
457+
query: "q1",
458458
}, { restart: true });
459459
processor.start(denops, {
460460
items: [{ id: 2, value: "2", detail: {} }],
461-
query: "",
461+
query: "q2",
462462
}, { restart: true });
463463
processor.start(denops, {
464464
items: [{ id: 3, value: "3", detail: {} }],
465-
query: "",
465+
query: "q3",
466466
}, { restart: true });
467467

468468
assertEquals(called, []);
469469

470470
notify.notify();
471471
await flushPromises();
472472

473-
assertEquals(called, []);
473+
assertEquals(called, [0]);
474474

475475
notify.notify();
476476
await flushPromises();
477477

478-
assertEquals(called, [3]);
478+
assertEquals(called, [0, 3]);
479479
},
480480
);
481481

0 commit comments

Comments
 (0)