mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
change app plugins to be a map to prevent duplicates and provide easier access
This commit is contained in:
@@ -101,7 +101,7 @@ describe("App tests", async () => {
|
|||||||
"onFirstBoot",
|
"onFirstBoot",
|
||||||
"onBuilt",
|
"onBuilt",
|
||||||
]);
|
]);
|
||||||
expect(app.plugins).toHaveLength(1);
|
expect(app.plugins.size).toBe(1);
|
||||||
expect(app.plugins.map((p) => p.name)).toEqual(["test"]);
|
expect(Array.from(app.plugins.keys())).toEqual(["test"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -62,8 +62,8 @@ export type AppOptions = {
|
|||||||
manager?: Omit<ModuleManagerOptions, "initial" | "onUpdated" | "seed">;
|
manager?: Omit<ModuleManagerOptions, "initial" | "onUpdated" | "seed">;
|
||||||
asyncEventsMode?: "sync" | "async" | "none";
|
asyncEventsMode?: "sync" | "async" | "none";
|
||||||
};
|
};
|
||||||
export type CreateAppConfig<C extends Connection = Connection> = {
|
export type CreateAppConfig = {
|
||||||
connection?: C | { url: string };
|
connection?: Connection | { url: string };
|
||||||
initialConfig?: InitialModuleConfigs;
|
initialConfig?: InitialModuleConfigs;
|
||||||
options?: AppOptions;
|
options?: AppOptions;
|
||||||
};
|
};
|
||||||
@@ -71,23 +71,26 @@ export type CreateAppConfig<C extends Connection = Connection> = {
|
|||||||
export type AppConfig = InitialModuleConfigs;
|
export type AppConfig = InitialModuleConfigs;
|
||||||
export type LocalApiOptions = Request | ApiOptions;
|
export type LocalApiOptions = Request | ApiOptions;
|
||||||
|
|
||||||
export class App<C extends Connection = Connection> {
|
export class App {
|
||||||
static readonly Events = AppEvents;
|
static readonly Events = AppEvents;
|
||||||
|
|
||||||
modules: ModuleManager;
|
modules: ModuleManager;
|
||||||
adminController?: AdminController;
|
adminController?: AdminController;
|
||||||
_id: string = crypto.randomUUID();
|
_id: string = crypto.randomUUID();
|
||||||
plugins: AppPluginConfig[];
|
plugins: Map<string, AppPluginConfig> = new Map();
|
||||||
|
|
||||||
private trigger_first_boot = false;
|
private trigger_first_boot = false;
|
||||||
private _building: boolean = false;
|
private _building: boolean = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public connection: C,
|
public connection: Connection,
|
||||||
_initialConfig?: InitialModuleConfigs,
|
_initialConfig?: InitialModuleConfigs,
|
||||||
private options?: AppOptions,
|
private options?: AppOptions,
|
||||||
) {
|
) {
|
||||||
this.plugins = (options?.plugins ?? []).map((plugin) => plugin(this));
|
for (const plugin of options?.plugins ?? []) {
|
||||||
|
const config = plugin(this);
|
||||||
|
this.plugins.set(config.name, config);
|
||||||
|
}
|
||||||
this.runPlugins("onBoot");
|
this.runPlugins("onBoot");
|
||||||
this.modules = new ModuleManager(connection, {
|
this.modules = new ModuleManager(connection, {
|
||||||
...(options?.manager ?? {}),
|
...(options?.manager ?? {}),
|
||||||
@@ -109,22 +112,22 @@ export class App<C extends Connection = Connection> {
|
|||||||
...args: any[]
|
...args: any[]
|
||||||
): Promise<{ name: string; result: any }[]> {
|
): Promise<{ name: string; result: any }[]> {
|
||||||
const results: { name: string; result: any }[] = [];
|
const results: { name: string; result: any }[] = [];
|
||||||
for (const plugin of this.plugins) {
|
for (const [name, config] of this.plugins) {
|
||||||
try {
|
try {
|
||||||
if (key in plugin && plugin[key]) {
|
if (key in config && config[key]) {
|
||||||
const fn = plugin[key];
|
const fn = config[key];
|
||||||
if (fn && typeof fn === "function") {
|
if (fn && typeof fn === "function") {
|
||||||
$console.debug(`[Plugin:${plugin.name}] ${key}`);
|
$console.debug(`[Plugin:${name}] ${key}`);
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
const result = await fn(...args);
|
const result = await fn(...args);
|
||||||
results.push({
|
results.push({
|
||||||
name: plugin.name,
|
name,
|
||||||
result,
|
result,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
$console.warn(`[Plugin:${plugin.name}] error running "${key}"`, String(e));
|
$console.warn(`[Plugin:${name}] error running "${key}"`, String(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return results as any;
|
return results as any;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
type IGenericSqlite,
|
type IGenericSqlite,
|
||||||
} from "data/connection/sqlite/GenericSqliteConnection";
|
} from "data/connection/sqlite/GenericSqliteConnection";
|
||||||
|
|
||||||
|
export type BunSqliteConnection = GenericSqliteConnection<Database>;
|
||||||
export type BunSqliteConnectionConfig = {
|
export type BunSqliteConnectionConfig = {
|
||||||
database: Database;
|
database: Database;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -322,7 +322,7 @@ export class SystemController extends Controller {
|
|||||||
local: datetimeStringLocal(),
|
local: datetimeStringLocal(),
|
||||||
utc: datetimeStringUTC(),
|
utc: datetimeStringUTC(),
|
||||||
},
|
},
|
||||||
plugins: this.app.plugins.map((p) => p.name),
|
plugins: Array.from(this.app.plugins.keys()),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user