2019-11-15 13:26:34 +01:00
|
|
|
import log from './../log.js';
|
2023-02-07 20:31:55 +01:00
|
|
|
import Services from '../types/Services';
|
2019-11-15 13:26:34 +01:00
|
|
|
|
2019-11-13 21:02:14 +01:00
|
|
|
export default class ServicesManager {
|
2023-02-07 20:31:55 +01:00
|
|
|
public services: Services = {};
|
|
|
|
|
|
2020-09-15 03:18:46 +02:00
|
|
|
constructor(commandsManager) {
|
|
|
|
|
this._commandsManager = commandsManager;
|
2019-11-13 21:02:14 +01:00
|
|
|
this.services = {};
|
2019-11-15 13:26:34 +01:00
|
|
|
this.registeredServiceNames = [];
|
2019-11-13 21:02:14 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 13:26:34 +01:00
|
|
|
/**
|
2019-12-11 04:28:23 +01:00
|
|
|
* Registers a new service.
|
2019-11-15 13:26:34 +01:00
|
|
|
*
|
|
|
|
|
* @param {Object} service
|
2019-12-11 04:28:23 +01:00
|
|
|
* @param {Object} configuration
|
2019-11-15 13:26:34 +01:00
|
|
|
*/
|
2019-12-11 04:28:23 +01:00
|
|
|
registerService(service, configuration = {}) {
|
2019-11-15 13:26:34 +01:00
|
|
|
if (!service) {
|
|
|
|
|
log.warn(
|
|
|
|
|
'Attempting to register a null/undefined service. Exiting early.'
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 04:28:23 +01:00
|
|
|
if (!service.name) {
|
2019-11-15 13:26:34 +01:00
|
|
|
log.warn(`Service name not set. Exiting early.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 04:28:23 +01:00
|
|
|
if (this.registeredServiceNames.includes(service.name)) {
|
2019-11-15 13:26:34 +01:00
|
|
|
log.warn(
|
2019-12-11 04:28:23 +01:00
|
|
|
`Service name ${service.name} has already been registered. Exiting before duplicating services.`
|
2019-11-15 13:26:34 +01:00
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 04:28:23 +01:00
|
|
|
if (service.create) {
|
2019-12-16 20:07:53 +01:00
|
|
|
this.services[service.name] = service.create({
|
|
|
|
|
configuration,
|
2020-09-15 03:18:46 +02:00
|
|
|
commandsManager: this._commandsManager,
|
2022-09-21 05:40:26 +02:00
|
|
|
servicesManager: this,
|
2019-12-16 20:07:53 +01:00
|
|
|
});
|
2023-02-07 20:31:55 +01:00
|
|
|
if (service.altName) {
|
|
|
|
|
console.log('Registering old name', service.altName);
|
|
|
|
|
this.services[service.altName] = this.services[service.name];
|
|
|
|
|
}
|
2019-12-11 04:28:23 +01:00
|
|
|
} else {
|
|
|
|
|
log.warn(`Service create factory function not defined. Exiting early.`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-11-15 13:26:34 +01:00
|
|
|
|
2019-12-11 04:28:23 +01:00
|
|
|
/* Track service registration */
|
|
|
|
|
this.registeredServiceNames.push(service.name);
|
2019-11-15 13:26:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-12-11 04:28:23 +01:00
|
|
|
* An array of services, or an array of arrays that contains service
|
|
|
|
|
* configuration pairs.
|
2019-11-15 13:26:34 +01:00
|
|
|
*
|
|
|
|
|
* @param {Object[]} services - Array of services
|
|
|
|
|
*/
|
|
|
|
|
registerServices(services) {
|
2019-12-11 04:28:23 +01:00
|
|
|
services.forEach(service => {
|
|
|
|
|
const hasConfiguration = Array.isArray(service);
|
|
|
|
|
|
|
|
|
|
if (hasConfiguration) {
|
|
|
|
|
const [ohifService, configuration] = service;
|
|
|
|
|
this.registerService(ohifService, configuration);
|
|
|
|
|
} else {
|
|
|
|
|
this.registerService(service);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-11-13 21:02:14 +01:00
|
|
|
}
|
|
|
|
|
}
|