Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor gather source. #71

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,29 @@ export class Ddu {
loader: Loader,
itemLevel: number,
parent?: DduItem,
): AsyncGenerator<DduItem[]> {
): AsyncGenerator<DduItem[], void, undefined> {
const { signal } = this.abortController;
if (signal.aborted) {
return;
}

const itemTransformer = new TransformStream<Item[], DduItem[]>({
transform: (chunk, controller) => {
const newItems = chunk.map((item: Item) =>
this.newDduItem(
index,
source,
sourceOptions,
item,
itemLevel,
)
);
controller.enqueue(newItems);
},
});

try {
const sourceItems = source.gather({
yield* source.gather({
denops,
context: this.context,
options: this.options,
Expand All @@ -454,40 +469,17 @@ export class Ddu {
input: this.input,
parent,
loader,
});
const reader = sourceItems.getReader();
const abort = () => reader.cancel(signal.reason);

try {
signal.addEventListener("abort", abort);

for (;;) {
const chunk = await reader.read();
if (chunk.done || signal.aborted) {
break;
}
const newItems = chunk.value.map((item: Item) =>
this.newDduItem(
index,
source,
sourceOptions,
item,
itemLevel,
)
);

yield newItems;
}
} finally {
signal.removeEventListener("abort", abort);
reader.releaseLock();
}
}).pipeThrough(itemTransformer, { signal });
} catch (e: unknown) {
await errorException(
denops,
e,
`source: ${source.name} "gather()" failed`,
);
if (signal.aborted && e === signal.reason) {
// Aborted by signal, so do nothing.
} else {
await errorException(
denops,
e,
`source: ${source.name} "gather()" failed`,
);
}
}
}

Expand Down
Loading