Skip to content

Commit

Permalink
Merge pull request #132 from Milly/cancelable
Browse files Browse the repository at this point in the history
fix: gather sources does not cancelled on refresh
  • Loading branch information
Shougo authored Dec 29, 2024
2 parents d198836 + 9c203d4 commit 54248fb
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 172 deletions.
91 changes: 41 additions & 50 deletions denops/ddu/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export const main: Entrypoint = (denops: Denops) => {
actions: [],
};
const lock = new Lock(0);
let queuedName: string | null = null;
let queuedRedrawOption: RedrawOption | null = null;
const uiRedrawLock = new Lock(0);

const checkDdu = (name: string) => {
if (!ddus[name]) {
Expand All @@ -82,15 +81,15 @@ export const main: Entrypoint = (denops: Denops) => {
};
const getDdu = (name: string) => {
if (!checkDdu(name)) {
ddus[name].push(new Ddu(getLoader(name)));
ddus[name].push(new Ddu(getLoader(name), uiRedrawLock));
}

return ddus[name].slice(-1)[0];
};
const pushDdu = (name: string) => {
checkDdu(name);

ddus[name].push(new Ddu(getLoader(name)));
ddus[name].push(new Ddu(getLoader(name), uiRedrawLock));

return ddus[name].slice(-1)[0];
};
Expand Down Expand Up @@ -358,57 +357,49 @@ export const main: Entrypoint = (denops: Denops) => {
return items;
},
async redraw(arg1: unknown, arg2: unknown): Promise<void> {
queuedName = ensure(arg1, is.String) as string;
queuedRedrawOption = ensure(arg2, is.Record) as RedrawOption;
const name = ensure(arg1, is.String) as string;
const opt = ensure(arg2, is.Record) as RedrawOption;

// NOTE: must be locked
await lock.lock(async () => {
while (queuedName !== null) {
const name = queuedName;
const opt = queuedRedrawOption;
queuedName = null;
queuedRedrawOption = null;

const ddu = getDdu(name);
const loader = getLoader(name);

if (opt?.check && !(await ddu.checkUpdated(denops))) {
// Mtime check failed
continue;
}
const ddu = getDdu(name);
const loader = getLoader(name);
let signal = ddu.cancelled;

if (opt?.input !== undefined) {
await ddu.setInput(denops, opt.input);
}
if (opt?.check && !(await ddu.checkUpdated(denops))) {
// Mtime check failed
return;
}

// Check volatile sources
const volatiles = ddu.getSourceArgs().map(
(sourceArgs, index) => sourceArgs[0].volatile ? index : -1,
).filter((index) => index >= 0);
if (opt?.input !== undefined) {
await ddu.setInput(denops, opt.input);
}

if (volatiles.length > 0 || opt?.method === "refreshItems") {
await ddu.refresh(
denops,
opt?.method === "refreshItems" ? [] : volatiles,
);
} else if (opt?.method === "uiRedraw") {
await ddu.uiRedraw(denops);
} else {
await ddu.redraw(denops);
}
await ddu.restoreTree(denops);

if (opt?.searchItem) {
await uiSearchItem(
denops,
loader,
ddu.getContext(),
ddu.getOptions(),
opt.searchItem,
);
}
if (opt?.method === "refreshItems") {
signal = await ddu.refresh(denops, [], { restoreTree: true });
} else {
// Check volatile sources
const volatiles = ddu.getSourceArgs().map(
(sourceArgs, index) => sourceArgs[0].volatile ? index : -1,
).filter((index) => index >= 0);

if (volatiles.length > 0) {
signal = await ddu.refresh(denops, volatiles, { restoreTree: true });
} else if (opt?.method === "uiRedraw") {
await ddu.restoreTree(denops, { preventRedraw: true, signal });
await ddu.uiRedraw(denops, { signal });
} else {
await ddu.redraw(denops, { restoreTree: true, signal });
}
});
}

if (opt?.searchItem && !signal.aborted) {
await uiSearchItem(
denops,
loader,
ddu.getContext(),
ddu.getOptions(),
opt.searchItem,
);
}
},
async redrawTree(
arg1: unknown,
Expand Down
Loading

0 comments on commit 54248fb

Please sign in to comment.