Skip to content
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
8 changes: 8 additions & 0 deletions backend/src/core/PluginLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Plugin } from "./types";

export class PluginLoader {
static async load(path: string): Promise<Plugin> {
const mod = await import(path);
return mod.default;
}
}
37 changes: 37 additions & 0 deletions backend/src/core/PluginManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Plugin, PluginContext } from "./types";
import { PluginRegistry } from "./PluginRegistry";

export class PluginManager {
private registry = new PluginRegistry();
private context: PluginContext;

constructor(context: PluginContext = {}) {
this.context = context;
}

register(plugin: Plugin) {
this.registry.register(plugin);
}

async initAll() {
for (const plugin of this.registry.getAll()) {
await plugin.init?.(this.context);
}
}

async startAll() {
for (const plugin of this.registry.getAll()) {
await plugin.start?.();
}
}

async stopAll() {
for (const plugin of this.registry.getAll()) {
await plugin.stop?.();
}
}

getPlugin(name: string) {
return this.registry.get(name);
}
}
5 changes: 5 additions & 0 deletions backend/src/core/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum PluginLifecycle {
INIT = "init",
START = "start",
STOP = "stop",
}
20 changes: 20 additions & 0 deletions backend/src/core/pluginRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Plugin } from "./types";

export class PluginRegistry {
private plugins: Map<string, Plugin> = new Map();

register(plugin: Plugin) {
if (this.plugins.has(plugin.name)) {
throw new Error(`Plugin ${plugin.name} already registered`);
}
this.plugins.set(plugin.name, plugin);
}

get(name: string): Plugin | undefined {
return this.plugins.get(name);
}

getAll(): Plugin[] {
return Array.from(this.plugins.values());
}
}
16 changes: 16 additions & 0 deletions backend/src/core/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type PluginContext = {
config?: Record<string, any>;
logger?: {
log: (...args: any[]) => void;
error: (...args: any[]) => void;
};
};

export interface Plugin {
name: string;
version: string;

init?(context: PluginContext): Promise<void> | void;
start?(): Promise<void> | void;
stop?(): Promise<void> | void;
}
Loading