Skip to content

Commit

Permalink
Refactor loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jul 8, 2023
1 parent 2fc6b97 commit 0292446
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 44 deletions.
5 changes: 3 additions & 2 deletions denops/ddu/base/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export type GetTextResult = {
};

export abstract class BaseColumn<Params extends BaseColumnParams> {
apiVersion = 2;

name = "";
path = "";
isInitialized = false;

apiVersion = 2;

async onInit(_args: OnInitArguments<Params>): Promise<void> {}

abstract getLength({}: GetLengthArguments<Params>): Promise<number>;
Expand Down
5 changes: 2 additions & 3 deletions denops/ddu/base/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ export type FilterArguments<Params extends BaseFilterParams> = {
};

export abstract class BaseFilter<Params extends BaseFilterParams> {
name = "";
isInitialized = false;

apiVersion = 3;

name = "";
path = "";
isInitialized = false;

async onInit(_args: OnInitArguments<Params>): Promise<void> {}

Expand Down
5 changes: 3 additions & 2 deletions denops/ddu/base/kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export type GetPreviewerArguments = {
export abstract class BaseKind<
Params extends BaseKindParams,
> {
apiVersion = 2;

name = "";
path = "";
isInitialized = false;

apiVersion = 2;

actions: Actions<Params> = {};

abstract params(): Params;
Expand Down
5 changes: 3 additions & 2 deletions denops/ddu/base/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ export abstract class BaseSource<
Params extends BaseSourceParams,
UserData extends unknown = unknown,
> {
apiVersion = 3;

name = "";
path = "";
isInitialized = false;

apiVersion = 3;

kind = "base";
prevMtime = new Date();
actions: Actions<Params> = {};
Expand Down
5 changes: 3 additions & 2 deletions denops/ddu/base/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ export type UiActionArguments<Params extends BaseUiParams> = {
export abstract class BaseUi<
Params extends BaseUiParams,
> {
apiVersion = 2;

name = "";
path = "";
isInitialized = false;

apiVersion = 2;

async onInit(_args: OnInitArguments<Params>): Promise<void> {}
async onBeforeAction(_args: OnBeforeActionArguments<Params>): Promise<void> {}
async onAfterAction(_args: OnAfterActionArguments<Params>): Promise<void> {}
Expand Down
8 changes: 4 additions & 4 deletions denops/ddu/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ export { ensure, is } from "https://deno.land/x/[email protected]/mod.ts";
export {
assertEquals,
equal,
} from "https://deno.land/std@0.192.0/testing/asserts.ts";
export { parse, toFileUrl } from "https://deno.land/std@0.192.0/path/mod.ts";
} from "https://deno.land/std@0.193.0/testing/asserts.ts";
export { parse, toFileUrl } from "https://deno.land/std@0.193.0/path/mod.ts";
export {
deadline,
DeadlineError,
} from "https://deno.land/std@0.192.0/async/mod.ts";
} from "https://deno.land/std@0.193.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/std@0.192.0/path/mod.ts";
} from "https://deno.land/std@0.193.0/path/mod.ts";
42 changes: 13 additions & 29 deletions denops/ddu/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import {
} from "./types.ts";
import { Denops, fn, Lock, op, parse, toFileUrl } from "./deps.ts";

type Mod = {
// deno-lint-ignore no-explicit-any
mod: any;
path: string;
};

export class Loader {
private extensions: Record<string, Extension> = {};
private mods: Record<
DduExtType,
Record<string, { mod: unknown; path: string }>
> = {
private mods: Record<DduExtType, Record<string, Mod>> = {
ui: {},
source: {},
filter: {},
Expand Down Expand Up @@ -138,9 +141,7 @@ export class Loader {

const mod = await import(toFileUrl(path).href);

const obj = { mod, path };

this.mods[type][name] = obj;
this.mods[type][name] = { mod, path };

// Check alias
const aliases = this.getAliasNames(type).filter(
Expand All @@ -161,8 +162,7 @@ class Extension {
private kinds: Record<KindName, BaseKind<BaseKindParams>> = {};
private columns: Record<ColumnName, BaseColumn<BaseColumnParams>> = {};

// deno-lint-ignore no-explicit-any
getUi(mod: { mod: any; path: string }, name: string): BaseUi<BaseUiParams> {
getUi(mod: Mod, name: string): BaseUi<BaseUiParams> {
if (!this.uis[name]) {
const obj = new mod.mod.Ui();
obj.name = name;
Expand All @@ -171,11 +171,7 @@ class Extension {
}
return this.uis[name];
}
getSource(
// deno-lint-ignore no-explicit-any
mod: { mod: any; path: string },
name: string,
): BaseSource<BaseSourceParams> {
getSource(mod: Mod, name: string): BaseSource<BaseSourceParams> {
if (!this.sources[name]) {
const obj = new mod.mod.Source();
obj.name = name;
Expand All @@ -184,11 +180,7 @@ class Extension {
}
return this.sources[name];
}
getFilter(
// deno-lint-ignore no-explicit-any
mod: { mod: any; path: string },
name: string,
): BaseFilter<BaseFilterParams> {
getFilter(mod: Mod, name: string): BaseFilter<BaseFilterParams> {
if (!this.filters[name]) {
const obj = new mod.mod.Filter();
obj.name = name;
Expand All @@ -197,11 +189,7 @@ class Extension {
}
return this.filters[name];
}
getKind(
// deno-lint-ignore no-explicit-any
mod: { mod: any; path: string },
name: string,
): BaseKind<BaseKindParams> {
getKind(mod: Mod, name: string): BaseKind<BaseKindParams> {
if (!this.kinds[name]) {
const obj = new mod.mod.Kind();
obj.name = name;
Expand All @@ -210,11 +198,7 @@ class Extension {
}
return this.kinds[name];
}
getColumn(
// deno-lint-ignore no-explicit-any
mod: { mod: any; path: string },
name: string,
): BaseColumn<BaseColumnParams> {
getColumn(mod: Mod, name: string): BaseColumn<BaseColumnParams> {
if (!this.columns[name]) {
const obj = new mod.mod.Column();
obj.name = name;
Expand Down

0 comments on commit 0292446

Please sign in to comment.