Skip to content

Commit

Permalink
Optimize loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Oct 15, 2023
1 parent 1ebbb24 commit 2c13eff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
12 changes: 8 additions & 4 deletions denops/ddc/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ export * as op from "https://deno.land/x/denops_std@v5.0.1/option/mod.ts";
export * as fn from "https://deno.land/x/denops_std@v5.0.1/function/mod.ts";
export * as vars from "https://deno.land/x/denops_std@v5.0.1/variable/mod.ts";
export * as autocmd from "https://deno.land/x/denops_std@v5.0.1/autocmd/mod.ts";
export * as base64 from "https://deno.land/std@0.203.0/encoding/base64.ts";
export * as base64 from "https://deno.land/std@0.204.0/encoding/base64.ts";
export {
assertEquals,
equal,
} from "https://deno.land/std@0.203.0/assert/mod.ts";
export { parse, toFileUrl } from "https://deno.land/std@0.203.0/path/mod.ts";
} from "https://deno.land/std@0.204.0/assert/mod.ts";
export {
basename,
parse,
toFileUrl,
} from "https://deno.land/std@0.204.0/path/mod.ts";
export {
deadline,
DeadlineError,
} from "https://deno.land/std@0.203.0/async/mod.ts";
} from "https://deno.land/std@0.204.0/async/mod.ts";
export { TimeoutError } from "https://deno.land/x/msgpack_rpc@v4.0.1/response_waiter.ts";
export { spy } from "https://deno.land/x/mock@0.15.2/mock.ts";
export { ensure, is } from "https://deno.land/x/unknownutil@v3.9.0/mod.ts";
Expand Down
38 changes: 22 additions & 16 deletions denops/ddc/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SourceName,
UiName,
} from "./types.ts";
import { Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";
import { basename, Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";

export class Loader {
private uis: Record<UiName, BaseUi<BaseUiParams>> = {};
Expand All @@ -23,23 +23,30 @@ export class Loader {
};
private checkPaths: Record<string, boolean> = {};
private registerLock = new Lock(0);
private cachedPaths: Record<string, string> = {};
private prevRuntimepath = "";

async autoload(
denops: Denops,
type: DdcExtType,
name: string,
) {
const paths = await globpath(
denops,
`denops/@ddc-${type}s/`,
this.getAlias(type, name) ?? name,
);
const runtimepath = await op.runtimepath.getGlobal(denops);
if (runtimepath !== this.prevRuntimepath) {
this.cachedPaths = await globpath(
denops,
"denops/@ddc-*s",
);
this.prevRuntimepath = runtimepath;
}

const key = `@ddc-${type}s/${this.getAlias(type, name) ?? name}`;

if (paths.length === 0) {
if (!this.cachedPaths[key]) {
return;
}

await this.registerPath(type, paths[0]);
await this.registerPath(type, this.cachedPaths[key]);
}

registerAlias(type: DdcExtType, alias: string, base: string) {
Expand Down Expand Up @@ -119,28 +126,27 @@ export class Loader {
async function globpath(
denops: Denops,
search: string,
file: string,
): Promise<string[]> {
): Promise<Record<string, string>> {
const runtimepath = await op.runtimepath.getGlobal(denops);

const check: Record<string, boolean> = {};
const paths: string[] = [];
const paths: Record<string, string> = {};
const glob = await fn.globpath(
denops,
runtimepath,
search + file + ".ts",
search + "/*.ts",
1,
1,
);

for (const path of glob) {
// Skip already added name.
if (parse(path).name in check) {
const parsed = parse(path);
const key = `${basename(parsed.dir)}/${parsed.name}`;
if (key in paths) {
continue;
}

paths.push(path);
check[parse(path).name] = true;
paths[key] = path;
}

return paths;
Expand Down

0 comments on commit 2c13eff

Please sign in to comment.