Refactors Initinialization.

Now we should take care to initialize in the right order.
This commit is contained in:
2023-09-22 17:46:20 +02:00
parent 8450226211
commit fdf10196a0
5 changed files with 44 additions and 38 deletions

View File

@@ -45,38 +45,34 @@ export function getServiceComponent(type: string): any {
}
export async function initServices() {
const services = import.meta.glob('/src/lib/services/**/+service.ts');
const modules = import.meta.glob('/src/lib/services/**/+service.ts');
for (const [modulePath, load] of Object.entries(services)) {
try {
//eslint-disable-next-line @typescript-eslint/no-explicit-any
const { poll, config } = (await load()) as any;
if (poll == undefined && config == undefined) {
throw new Error(`${modulePath} does not export 'poll' or 'config'`);
return Promise.allSettled(
Object.entries(modules).map(async ([path, load]) => {
try {
const { poll, config } = (await load()) as any;
registerService(path.slice(18, -12), { poll, config });
} catch (error) {
console.warn(`could not load ${path}: ${error}`);
}
const typeName = modulePath.slice(18, -12);
registerService(typeName, { poll, config });
} 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');
const modules = import.meta.glob('/src/lib/services/**/+content.svelte');
for (const [componentPath, load] of Object.entries(services)) {
try {
//eslint-disable-next-line @typescript-eslint/no-explicit-any
const module = (await load()) as any;
if (module == undefined) {
throw new Error(`${componentPath} does not load`);
return Promise.allSettled(
Object.entries(modules).map(async ([path, load]) => {
try {
const module = (await load()) as any;
if (module == undefined) {
throw new Error('load returned and undefined value');
}
registerComponent(path.slice(18, -16), module.default);
} catch (error) {
console.warn(`could not load ${path}: ${error}`);
}
const typeName = componentPath.slice(18, -16);
registerComponent(typeName, module.default);
} catch (err) {
console.error(`Could not load component definition from '${componentPath}': ${err}`);
}
}
})
);
}