57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import type { ServiceHandler } from './service';
|
|
|
|
const services: Record<string, ServiceHandler> = {};
|
|
|
|
const components: Record<string, any> = {};
|
|
|
|
function registerService(type: string, handler: ServiceHandler) {
|
|
services[type] = handler;
|
|
}
|
|
|
|
function registerComponent(type: string, component: any) {
|
|
components[type] = component;
|
|
}
|
|
|
|
export function getServiceHandler(type: string): ServiceHandler | undefined {
|
|
return services[type];
|
|
}
|
|
|
|
export function getServiceComponent(type: string): any {
|
|
return components[type];
|
|
}
|
|
|
|
export async function initServices() {
|
|
const services = import.meta.glob('/src/lib/services/**/+service.ts');
|
|
|
|
for (const [modulePath, load] of Object.entries(services)) {
|
|
try {
|
|
const { handle } = (await load()) as any;
|
|
if (handle == undefined) {
|
|
throw new Error(`${modulePath} does not export 'handle'`);
|
|
}
|
|
const typeName = modulePath.slice(18, -12);
|
|
registerService(typeName, handle);
|
|
} catch (err) {
|
|
console.error(`Could not load service definition from '${modulePath}': ${err}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function initComponents() {
|
|
const services = import.meta.glob('/src/lib/services/**/+content.svelte');
|
|
|
|
for (const [componentPath, load] of Object.entries(services)) {
|
|
try {
|
|
const module = (await load()) as any;
|
|
|
|
if (module == undefined) {
|
|
throw new Error(`${componentPath} does not load`);
|
|
}
|
|
const typeName = componentPath.slice(18, -16);
|
|
registerComponent(typeName, module.default);
|
|
} catch (err) {
|
|
console.error(`Could not load component definition from '${componentPath}': ${err}`);
|
|
}
|
|
}
|
|
}
|