mirror of
https://github.com/samanhappy/mcphub.git
synced 2025-12-31 20:00:00 -05:00
Refactor service registration and revert lazy loading implementation (#234)
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { createRequire } from 'module';
|
||||
import { join } from 'path';
|
||||
|
||||
type Class<T> = new (...args: any[]) => T;
|
||||
|
||||
interface Service<T> {
|
||||
@@ -8,17 +11,19 @@ interface Service<T> {
|
||||
const registry = new Map<string, Service<any>>();
|
||||
const instances = new Map<string, unknown>();
|
||||
|
||||
export async function registerService<T>(key: string, entry: Service<T>) {
|
||||
export function registerService<T>(key: string, entry: Service<T>) {
|
||||
// Try to load override immediately during registration
|
||||
const overridePath = './' + key + 'x.js';
|
||||
await 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
|
||||
const overridePath = join(process.cwd(), 'src', 'services', key + 'x.ts');
|
||||
try {
|
||||
const require = createRequire(process.cwd());
|
||||
const mod = require(overridePath);
|
||||
const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'];
|
||||
if (typeof override === 'function') {
|
||||
entry.override = override;
|
||||
}
|
||||
} catch (error) {
|
||||
// Silently ignore if override doesn't exist
|
||||
}
|
||||
|
||||
registry.set(key, entry);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,10 @@
|
||||
import { registerService, getService } from './registry.js';
|
||||
import { DataService, DataServiceImpl } from './dataService.js';
|
||||
|
||||
let initialized = false;
|
||||
let tempDataService: DataService | null = null;
|
||||
|
||||
async function initializeServices() {
|
||||
if (initialized) return;
|
||||
|
||||
await registerService('dataService', {
|
||||
defaultImpl: DataServiceImpl,
|
||||
});
|
||||
|
||||
initialized = true;
|
||||
tempDataService = null; // Clear temp service once real one is ready
|
||||
}
|
||||
registerService('dataService', {
|
||||
defaultImpl: DataServiceImpl,
|
||||
});
|
||||
|
||||
export function getDataService(): DataService {
|
||||
if (initialized) {
|
||||
return getService<DataService>('dataService');
|
||||
}
|
||||
|
||||
// Return temporary service for cases where services haven't been initialized yet
|
||||
// This allows module loading to work even before server initialization
|
||||
if (!tempDataService) {
|
||||
tempDataService = new DataServiceImpl();
|
||||
}
|
||||
return tempDataService;
|
||||
return getService<DataService>('dataService');
|
||||
}
|
||||
|
||||
export { initializeServices };
|
||||
|
||||
Reference in New Issue
Block a user