-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServicesMediator.ts
40 lines (32 loc) · 1.19 KB
/
ServicesMediator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {InitializationSingleTon} from '@/Service/lib/shared.ts';
import {VariantService} from '@/Service/lib/Invariant/Adapter.ts';
export class ServiceMediator {
private services: Set<InitializationSingleTon<any>> = new Set();
constructor() {}
public registerServiceForInitialization(service: InitializationSingleTon<any>) {
const serviceName = service.constructor.name;
// 중복된 서비스가 있는지 확인
for (const registeredService of this.services) {
const isDuplicatedService = registeredService.constructor.name === serviceName;
VariantService.invariant(!isDuplicatedService, {
type: 'warning',
message: `Service ${serviceName} is already registered.`,
});
}
console.log(`${serviceName}service registered`);
this.services.add(service);
}
public async initializeServices(callback?: () => Promise<void>) {
try {
await Promise.all(
Array.from(this.services).map(service => {
return service.initialize();
}),
);
await callback?.();
} catch (e: unknown) {
console.warn(`There are some problem on initializing Services, ${e}`);
}
}
}
export default new ServiceMediator();