Files
Checkmate/server/src/service/system/serviceRegistry.js
T

63 lines
1.3 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "ServiceRegistry";
2025-07-28 15:25:33 -07:00
2025-04-20 11:29:53 -07:00
class ServiceRegistry {
static SERVICE_NAME = SERVICE_NAME;
2025-07-28 15:25:33 -07:00
2025-07-28 15:03:28 -07:00
constructor({ logger }) {
2025-04-20 11:29:53 -07:00
this.services = {};
2025-07-28 15:03:28 -07:00
this.logger = logger;
2025-04-20 11:29:53 -07:00
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return ServiceRegistry.SERVICE_NAME;
}
2025-07-28 15:25:33 -07:00
// Instance methods
2025-04-20 11:29:53 -07:00
register(name, service) {
2025-07-28 15:03:28 -07:00
this.logger.info({
2025-04-20 11:29:53 -07:00
message: `Registering service ${name}`,
service: SERVICE_NAME,
method: "register",
});
this.services[name] = service;
}
get(name) {
if (!this.services[name]) {
2025-07-28 15:03:28 -07:00
this.logger.error({
2025-04-20 11:29:53 -07:00
message: `Service ${name} is not registered`,
service: SERVICE_NAME,
method: "get",
});
throw new Error(`Service ${name} is not registered`);
}
return this.services[name];
}
listServices() {
return Object.keys(this.services);
}
2025-07-28 15:25:33 -07:00
static get(name) {
if (!ServiceRegistry.instance) {
throw new Error("ServiceRegistry not initialized");
}
return ServiceRegistry.instance.get(name);
}
static register(name, service) {
if (!ServiceRegistry.instance) {
throw new Error("ServiceRegistry not initialized");
}
return ServiceRegistry.instance.register(name, service);
}
static listServices() {
if (!ServiceRegistry.instance) {
throw new Error("ServiceRegistry not initialized");
}
return ServiceRegistry.instance.listServices();
}
2025-04-20 11:29:53 -07:00
}
2025-07-28 15:03:28 -07:00
export default ServiceRegistry;