fix: Create typed services and allow lower case access to agree with naming conventions (#3141)

* fix: Add typed services

* fix: Typed services - PR changes

* remaining of the cineService and toolbarService renaming

---------

Co-authored-by: Alireza <ar.sedghi@gmail.com>
This commit is contained in:
Bill WallaceandAlireza authored and GitHub committed 2023-02-07 14:31:55 -05:00
1 parent 0e1a58da79
commit 7f799b8e3d
55 files changed
+302 -233

No files matched your search

@@ -0,0 +1,76 @@
import log from './../log.js';
import Services from '../types/Services';
export default class ServicesManager {
public services: Services = {};
constructor(commandsManager) {
this._commandsManager = commandsManager;
this.services = {};
this.registeredServiceNames = [];
}
/**
* Registers a new service.
*
* @param {Object} service
* @param {Object} configuration
*/
registerService(service, configuration = {}) {
if (!service) {
log.warn(
'Attempting to register a null/undefined service. Exiting early.'
);
return;
}
if (!service.name) {
log.warn(`Service name not set. Exiting early.`);
return;
}
if (this.registeredServiceNames.includes(service.name)) {
log.warn(
`Service name ${service.name} has already been registered. Exiting before duplicating services.`
);
return;
}
if (service.create) {
this.services[service.name] = service.create({
configuration,
commandsManager: this._commandsManager,
servicesManager: this,
});
if (service.altName) {
console.log('Registering old name', service.altName);
this.services[service.altName] = this.services[service.name];
}
} else {
log.warn(`Service create factory function not defined. Exiting early.`);
return;
}
/* Track service registration */
this.registeredServiceNames.push(service.name);
}
/**
* An array of services, or an array of arrays that contains service
* configuration pairs.
*
* @param {Object[]} services - Array of services
*/
registerServices(services) {
services.forEach(service => {
const hasConfiguration = Array.isArray(service);
if (hasConfiguration) {
const [ohifService, configuration] = service;
this.registerService(ohifService, configuration);
} else {
this.registerService(service);
}
});
}
}