UNPKG

@hashgraph/hedera-local

Version:

Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).

74 lines 2.64 kB
"use strict"; // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceLocator = void 0; /** * ServiceLocator is a class that manages services in the application. * It provides methods to register, unregister, and get services. * It implements the Singleton pattern, meaning there can only be one instance of this class in the application. */ class ServiceLocator { constructor() { /** * A map of services registered with the service locator. * The key is the service name and the value is the service instance. * * @private */ this.services = new Map(); } /** * Gets the current instance of the ServiceLocator. * If the current instance does not exist, it creates a new one. * * @public * @returns {ServiceLocator} The current instance of the ServiceLocator. */ static get Current() { if (!ServiceLocator.currentInstance) { this.currentInstance = new ServiceLocator(); } return this.currentInstance; } /** * Gets a service registered with the service locator. * * @param {string} serviceName - The name of the service to get. * @returns {T} The service instance. * @throws {Error} If the service is not registered with the service locator. */ get(serviceName) { if (!this.services.has(serviceName)) { throw new Error(`${serviceName} not registered with ${this.constructor.name}`); } return this.services.get(serviceName); } /** * Registers a service with the service locator. * * @param {T} service - The service instance to register. * @throws {Error} If the service is already registered with the service locator. */ register(service) { const key = service.constructor.name; if (this.services.has(key)) { throw new Error(`${key} not registered with ${this.constructor.name}`); } this.services.set(key, service); } /** * Unregisters a service from the service locator. * * @public * @param {string} serviceName - The name of the service to unregister. */ unregister(serviceName) { if (!this.services.has(serviceName)) { console.error(`Attempted to unregister service of type ${serviceName} which is not registered with ${this.constructor.name}.`); return; } this.services.delete(serviceName); } } exports.ServiceLocator = ServiceLocator; //# sourceMappingURL=ServiceLocator.js.map