hub
is a lightweight service locator library for Node.js. It allows you to register, retrieve, and manage services, making dependency injection easier and decoupling your modules. This library supports asynchronous service initialization, de-initialization, and lazy-loading of services using promises.
Install via npm:
npm install hub
To initialize the service locator in your application, create a standalone instance of hub
in the main entry point of your application:
const hub = require('hub').standalone();
In all other files across your application, you can simply include hub
using:
const hub = require('hub');
This ensures that all parts of your application share the same hub
instance.
You can register a service with the register()
method. A service constructor should return a promise that resolves to the service object, and an optional destructor can be provided for clean-up. Here's an example using async/await
:
const hub = require('hub');
// Register a service
hub.register('logger', async () => {
return {
log: (message) => console.log(message),
};
});
Once a service is registered, you can retrieve it using the get()
method. The service will be lazily initialized the first time it's accessed:
const logger = await hub.get('logger');
logger.log('Service Locator Initialized!');
To properly clean up resources, you can destroy a service by calling destroy()
. This will execute the optional destructor provided during registration:
await hub.destroy('logger');
console.log('Logger service destroyed');
If needed, services can be unregistered to remove them from the service locator. This will not destroy an already initiated service:
hub.unregister('logger');
You can check if a service is registered or initiated:
console.log(hub.isRegistered('logger')); // true/false
console.log(hub.isInitiated('logger')); // true/false
Registers a service with a given name.
-
name:
String
- The name of the service. -
constructor:
Function
- A function that returns a promise resolving to the service instance. -
destructor (optional):
Function
- A function that returns a promise for cleaning up the service.
Returns true
if successful.
Retrieves the service instance by its name. If standalone
is true, it will not cache the service and will always invoke the constructor.
-
name:
String | Array
- Name of the service (or array of names). -
standalone:
Boolean
(optional) - If true, the service will not be cached.
Returns a Promise
that resolves to the service instance.
Destroys a service and calls the registered destructor (if provided).
-
name:
String | Array
- Name of the service or array of services. - service (optional): The service instance to destroy.
Returns a Promise
.
Unregisters a service by its name.
-
name:
String
- Name of the service.
Throws an error if the service is not registered. Returns true
if successful.
Checks if a service is registered.
-
name:
String
- Name of the service.
Returns true
if the service is registered, otherwise false
.
Retrieves a list of all registered services.
Returns an array of service names.
Checks if a service has been initiated.
-
name:
String
- Name of the service.
Returns true
if the service is initiated, otherwise false
.
Retrieves a list of all initiated services.
Returns an array of initiated service names.
Returns a new instance of the Hub that is not cached by the require
system. Useful for creating isolated service locators.