diffusion
Version:
Diffusion JavaScript client
53 lines (52 loc) • 1.8 kB
JavaScript
;
/**
* @module Client
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceRegistryImpl = void 0;
var errors_1 = require("./../../errors/errors");
/**
* Mutable registry of service implementations, bound to a service definitions.
*/
var ServiceRegistryImpl = /** @class */ (function () {
function ServiceRegistryImpl() {
/**
* The map of services indexed by their service definition
*/
this.services = new Map();
/**
* Listeners, to be notified whenever a new service implementation is added
*/
this.listeners = [];
}
/**
* Add a new service implementation to the registry. Only one service
* may be bound to a given definition.
*
* When a new service is added, any listeners will be notified.
*
* @param definition the service definition
* @param service the service implementation
* @throws a {@link RuntimeError} if another service is already bound to the same definition.
*/
ServiceRegistryImpl.prototype.add = function (definition, service) {
if (this.services.has(definition)) {
throw new errors_1.RuntimeError("Service already exists for " + definition);
}
this.services.set(definition, service);
this.listeners.forEach(function (listener) {
listener(definition, service);
});
};
/**
* @inheritdoc
*/
ServiceRegistryImpl.prototype.addListener = function (listener) {
this.listeners.push(listener);
this.services.forEach(function (service, definition) {
listener(definition, service);
});
};
return ServiceRegistryImpl;
}());
exports.ServiceRegistryImpl = ServiceRegistryImpl;