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 f85b8ce commit e190792
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
16 changes: 7 additions & 9 deletions denops/dpp/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ export { ensure, is } from "https://deno.land/x/[email protected]/mod.ts";
export {
assertEquals,
equal,
} from "https://deno.land/std@0.202.0/assert/mod.ts";
} from "https://deno.land/std@0.204.0/assert/mod.ts";
export {
basename,
dirname,
extname,
parse,
SEP as pathsep,
toFileUrl,
} from "https://deno.land/std@0.202.0/path/mod.ts";
} from "https://deno.land/std@0.204.0/path/mod.ts";
export {
deadline,
DeadlineError,
} from "https://deno.land/std@0.202.0/async/mod.ts";
} from "https://deno.land/std@0.204.0/async/mod.ts";
export { TimeoutError } from "https://deno.land/x/[email protected]/response_waiter.ts";
export { Lock } from "https://deno.land/x/[email protected]/mod.ts";
export {
basename,
dirname,
SEP as pathsep,
} from "https://deno.land/[email protected]/path/mod.ts";
export { deferred } from "https://deno.land/[email protected]/async/deferred.ts";
export { deferred } from "https://deno.land/[email protected]/async/deferred.ts";
38 changes: 22 additions & 16 deletions denops/dpp/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ExtName,
ProtocolName,
} from "./types.ts";
import { Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";
import { basename, Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";

type Mod = {
// deno-lint-ignore no-explicit-any
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: DppExtType,
name: string,
) {
const paths = await globpath(
denops,
`denops/@dpp-${type}s/`,
name,
);
const runtimepath = await op.runtimepath.getGlobal(denops);
if (runtimepath !== this.prevRuntimepath) {
this.cachedPaths = await globpath(
denops,
"denops/@dpp-*s",
);
this.prevRuntimepath = runtimepath;
}

const key = `@dpp-${type}s/${name}`;

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

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

async registerPath(type: DppExtType, path: string) {
Expand Down Expand Up @@ -114,28 +121,27 @@ class Extension {
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 e190792

Please sign in to comment.