Skip to content

Commit

Permalink
Merge pull request #69 from Milly/cancel-gather
Browse files Browse the repository at this point in the history
Abort item gathering when quit UI
  • Loading branch information
Shougo authored Jul 14, 2023
2 parents 785ab75 + 145fce1 commit 77bc8d5
Showing 1 changed file with 62 additions and 30 deletions.
92 changes: 62 additions & 30 deletions denops/ddu/ddu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export class Ddu {
private userOptions: UserOptions = {};
private initialized = false;
private quitted = false;
private cancelToRefresh = false;
private cancelledToRefresh = false;
private abortController = new AbortController();
private redrawLock = new Lock(0);
private startTime = 0;
private expandedPaths = new Set<string[]>();
Expand Down Expand Up @@ -171,7 +172,7 @@ export class Ddu {
}

if (!this.options?.refresh) {
this.quitted = false;
this.resetQuitted();

if (this.searchPath) {
// Redraw only without regather items.
Expand Down Expand Up @@ -216,7 +217,7 @@ export class Ddu {
ui.isInitialized = false;

this.initialized = false;
this.quitted = false;
this.resetQuitted();

// Source onInit() must be called before UI
for (const userSource of this.options.sources) {
Expand Down Expand Up @@ -279,13 +280,13 @@ export class Ddu {
this.startTime = Date.now();

// Clean up previous gather state
this.cancelToRefresh = true;
this.cancelToRefresh();
for (const state of Object.values(this.gatherStates)) {
while (!state.done) {
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
this.cancelToRefresh = false;
this.resetCancelledToRefresh();

// Initialize UI window
if (!this.options.sync) {
Expand Down Expand Up @@ -350,10 +351,6 @@ export class Ddu {
0,
)
) {
if (this.shouldStopCurrentContext()) {
break;
}

let path = sourceOptions.path;
if (path === "") {
// Use current directory instead
Expand Down Expand Up @@ -433,6 +430,11 @@ export class Ddu {
itemLevel: number,
parent?: DduItem,
): AsyncGenerator<DduItem[]> {
const { signal } = this.abortController;
if (signal.aborted) {
return;
}

try {
const sourceItems = source.gather({
denops,
Expand All @@ -444,22 +446,32 @@ export class Ddu {
parent,
loader,
});
const reader = sourceItems.getReader();
const abort = () => reader.cancel(signal.reason);

for await (const chunk of sourceItems) {
if (this.shouldStopCurrentContext()) {
return;
}
const newItems = chunk.map((item: Item) =>
this.newDduItem(
index,
source,
sourceOptions,
item,
itemLevel,
)
);
try {
signal.addEventListener("abort", abort);

yield newItems;
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();
}
} catch (e: unknown) {
await errorException(
Expand Down Expand Up @@ -691,6 +703,30 @@ export class Ddu {
quit() {
// NOTE: quitted flag must be called after uiQuit().
this.quitted = true;
this.abortController.abort("quit");
}

private resetQuitted() {
this.quitted = false;
this.resetAbortController();
}

private cancelToRefresh() {
this.cancelledToRefresh = true;
this.abortController.abort("cancelToRefresh");
}

private resetCancelledToRefresh() {
this.cancelledToRefresh = false;
this.resetAbortController();
}

private resetAbortController() {
if (
!this.shouldStopCurrentContext() && this.abortController.signal.aborted
) {
this.abortController = new AbortController();
}
}

async uiAction(
Expand Down Expand Up @@ -1005,11 +1041,11 @@ export class Ddu {

if (flags & ActionFlags.RefreshItems) {
// Restore quitted flag before refresh and redraw
this.quitted = false;
this.resetQuitted();
await this.refresh(denops);
} else if (uiOptions.persist || flags & ActionFlags.Persist) {
// Restore quitted flag before refresh and redraw
this.quitted = false;
this.resetQuitted();
await ui.redraw({
denops,
context: this.context,
Expand Down Expand Up @@ -1126,10 +1162,6 @@ export class Ddu {
parent,
)
) {
if (this.shouldStopCurrentContext()) {
return;
}

await this.callColumns(
denops,
sourceOptions.columns,
Expand Down Expand Up @@ -1361,7 +1393,7 @@ export class Ddu {
}

shouldStopCurrentContext(): boolean {
return this.quitted || this.cancelToRefresh;
return this.quitted || this.cancelledToRefresh;
}

getContext() {
Expand Down

0 comments on commit 77bc8d5

Please sign in to comment.