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

fix: gather sources does not cancelled on refresh #132

Merged
merged 3 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
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
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
Loading