mirror of
https://github.com/samanhappy/mcphub.git
synced 2026-01-01 04:08:52 -05:00
feat: implement lazy loading for data service and enhance service registration (#233)
This commit is contained in:
@@ -8,10 +8,10 @@ interface Service<T> {
|
||||
const registry = new Map<string, Service<any>>();
|
||||
const instances = new Map<string, unknown>();
|
||||
|
||||
export function registerService<T>(key: string, entry: Service<T>) {
|
||||
export async function registerService<T>(key: string, entry: Service<T>) {
|
||||
// Try to load override immediately during registration
|
||||
const overridePath = './' + key + 'x.js';
|
||||
import(overridePath)
|
||||
await import(overridePath)
|
||||
.then((mod) => {
|
||||
const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'];
|
||||
if (typeof override === 'function') {
|
||||
|
||||
@@ -1,10 +1,31 @@
|
||||
import { registerService, getService } from './registry.js';
|
||||
import { DataService, DataServiceImpl } from './dataService.js';
|
||||
|
||||
registerService('dataService', {
|
||||
defaultImpl: DataServiceImpl,
|
||||
});
|
||||
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
|
||||
}
|
||||
|
||||
export function getDataService(): DataService {
|
||||
return getService<DataService>('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;
|
||||
}
|
||||
|
||||
export { initializeServices };
|
||||
|
||||
Reference in New Issue
Block a user