mirror of
https://github.com/samanhappy/mcphub.git
synced 2025-12-31 20:00:00 -05:00
feat: enable immediate loading of service overrides during registration (#232)
This commit is contained in:
@@ -2,12 +2,24 @@ type Class<T> = new (...args: any[]) => T;
|
||||
|
||||
interface Service<T> {
|
||||
defaultImpl: Class<T>;
|
||||
override?: Class<T>;
|
||||
}
|
||||
|
||||
const registry = new Map<string, Service<any>>();
|
||||
const instances = new Map<string, unknown>();
|
||||
|
||||
export function registerService<T>(key: string, entry: Service<T>) {
|
||||
// Try to load override immediately during registration
|
||||
const overridePath = './' + key + 'x.js';
|
||||
import(overridePath)
|
||||
.then((mod) => {
|
||||
const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'];
|
||||
if (typeof override === 'function') {
|
||||
entry.override = override;
|
||||
}
|
||||
})
|
||||
.catch(() => {}); // Silently ignore if override doesn't exist
|
||||
|
||||
registry.set(key, entry);
|
||||
}
|
||||
|
||||
@@ -19,17 +31,8 @@ export function getService<T>(key: string): T {
|
||||
const entry = registry.get(key);
|
||||
if (!entry) throw new Error(`Service not registered for key: ${key.toString()}`);
|
||||
|
||||
let Impl = entry.defaultImpl;
|
||||
|
||||
const overridePath = './' + key + 'x.js';
|
||||
import(overridePath)
|
||||
.then((mod) => {
|
||||
const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'] ?? Impl.name;
|
||||
if (typeof override === 'function') {
|
||||
Impl = override;
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
// Use override if available, otherwise use default
|
||||
const Impl = entry.override || entry.defaultImpl;
|
||||
|
||||
const instance = new Impl();
|
||||
instances.set(key, instance);
|
||||
|
||||
Reference in New Issue
Block a user