lavva.exalushome
Version:
Library implementing communication and abstraction layers for ExalusHome system
127 lines • 6.37 kB
JavaScript
import { TypedEvent } from './TypedEvent';
import { LoggerService } from './Services/Logging/LoggerService';
export class DependencyContainer {
constructor() {
this._services = {};
this._serviceRegistrationEvent = new TypedEvent();
}
set DebugCalls(value) {
DependencyContainer._debugCalls = value;
localStorage.setItem("_exalus_DependencyContainer_debugCalls", value.toString());
}
static get Instance() {
if (DependencyContainer._instance != null)
return DependencyContainer._instance;
const debugCallsValue = localStorage.getItem("_exalus_DependencyContainer_debugCalls");
DependencyContainer._debugCalls = debugCallsValue === true.toString();
DependencyContainer._instance = new DependencyContainer();
this.Log = new LoggerService();
this.Log.Warning("Initializing DependencyContainer");
DependencyContainer._instance.RegisterService(this.Log);
this.IsInitialized = true;
window["services"] = this;
return DependencyContainer._instance;
}
RegisterService(service) {
var _a;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Registering new service: ${service.GetServiceName()}`);
if (DependencyContainer._debugCalls) {
try {
throw new Error(`Registering new service: ${service.GetServiceName()}`);
}
catch (ex) {
console.error(ex);
}
}
this._services[service.GetServiceName()] = service;
this._serviceRegistrationEvent.Invoke(service);
}
GetService(serviceName) {
var _a;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Getting service [${serviceName}] by GetService<T>()`);
if (DependencyContainer._debugCalls) {
try {
throw new Error(`Getting service: ${serviceName}`);
}
catch (ex) {
console.error(ex);
}
}
return this._services[serviceName];
}
GetServiceAsync(serviceName, token) {
var _a, _b;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Waiting for service availability [${serviceName}] by GetServiceAsync<T>()`);
if (DependencyContainer._debugCalls) {
try {
throw new Error(`Waiting for service availability: ${serviceName}`);
}
catch (ex) {
console.error(ex);
}
}
if (this._services.hasOwnProperty(serviceName)) {
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug("DependencyContainer", `Got service [${serviceName}] by GetServiceAsync<T>()`);
return Promise.resolve(this._services[serviceName]);
}
return new Promise((resolve, reject) => {
var sub = (service) => {
var _a;
if ((service === null || service === void 0 ? void 0 : service.GetServiceName()) === serviceName) {
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Got service [${serviceName}] by GetServiceAsync<T>()`);
this._serviceRegistrationEvent.Unsubscribe(sub);
resolve(service);
}
};
var onCancel = () => {
var _a;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Cancelled service [${serviceName}] retrieval by GetServiceAsync<T>()`);
this._serviceRegistrationEvent.Unsubscribe(sub);
token === null || token === void 0 ? void 0 : token.CancellationEvent.Unsubscribe(onCancel);
reject(new Error("Operation cancelled!"));
};
token === null || token === void 0 ? void 0 : token.CancellationEvent.Subscribe(onCancel);
this._serviceRegistrationEvent.Subscribe(sub);
});
}
GetServiceWithTimeoutAsync(serviceName, timeout) {
var _a, _b;
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Waiting for service[${serviceName}] availability by GetServiceWithTimeoutAsync<T>()`);
if (DependencyContainer._debugCalls) {
try {
throw new Error(`Waiting for service availability: ${serviceName}`);
}
catch (ex) {
console.error(ex);
}
}
if (this._services.hasOwnProperty(serviceName)) {
(_b = DependencyContainer.Log) === null || _b === void 0 ? void 0 : _b.Debug("DependencyContainer", `Got service [${serviceName}] by GetServiceWithTimeoutAsync<T>()`);
return Promise.resolve(this._services[serviceName]);
}
return new Promise((resolve, reject) => {
let timeoutId = 0;
let sub = (service) => {
var _a;
if ((service === null || service === void 0 ? void 0 : service.GetClassName()) === service) {
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Debug("DependencyContainer", `Got service [${service}] by GetServiceWithTimeoutAsync<T>()`);
window.clearTimeout(timeoutId);
this._serviceRegistrationEvent.Unsubscribe(sub);
resolve(service);
}
};
let onTimeout = () => {
var _a;
window.clearTimeout(timeoutId);
this._serviceRegistrationEvent.Unsubscribe(sub);
(_a = DependencyContainer.Log) === null || _a === void 0 ? void 0 : _a.Error("DependencyContainer", `Failed to get service [${serviceName}] in time by GetServiceWithTimeoutAsync<T>()`);
reject(new Error("Operation cancelled!"));
};
timeoutId = window.setTimeout(onTimeout, timeout);
this._serviceRegistrationEvent.Subscribe(sub);
});
}
}
DependencyContainer.IsInitialized = false;
DependencyContainer._debugCalls = false;
//# sourceMappingURL=DependencyContainer.js.map