feat(util_registry): impelmented destroyAll(scope) method in the utilRegistry

This commit is contained in:
Johannes Eder 2021-06-18 13:51:02 +02:00 committed by Sarah Vaupel
parent d1b9952690
commit f1ef2e5ec7
2 changed files with 36 additions and 11 deletions

View File

@ -31,6 +31,8 @@ export class EventManager {
this._registeredListeners = [];
}
//Todo: Uncomment debug log!
//_debugLog() {}
_debugLog(fName, ...args) {
console.log(`[DEBUGLOG] EventManager.${fName}`, { args: args, instance: this });

View File

@ -4,8 +4,8 @@ const DEBUG_MODE = /localhost/.test(window.location.href) ? 1 : 0;
export class UtilRegistry {
_registeredUtils = new Array();
_activeUtilInstances = new Array();
_registeredUtilClasses = new Array(); //{utilClass}
_activeUtilInstancesWrapped = new Array(); //{utilClass, scope, element, instance}
_appInstance;
/**
@ -33,7 +33,7 @@ export class UtilRegistry {
console.log('registering util "' + util.name + '"');
console.log({ util });
}
this._registeredUtils.push(util);
this._registeredUtilClasses.push(util);
}
deregister(name, destroy) {
@ -44,7 +44,7 @@ export class UtilRegistry {
this._destroyUtilInstances(name);
}
this._registeredUtils.splice(utilIndex, 1);
this._registeredUtilClasses.splice(utilIndex, 1);
}
}
@ -54,7 +54,7 @@ export class UtilRegistry {
initAll(scope = document.body) {
let startedInstances = new Array();
const setupInstances = this._registeredUtils.map((util) => this.setup(util, scope)).flat();
const setupInstances = this._registeredUtilClasses.map((util) => this.setup(util, scope)).flat();
const orderedInstances = setupInstances.filter(_isStartOrdered);
@ -97,6 +97,17 @@ export class UtilRegistry {
return startedInstances;
}
destroyAll(scope = document.body) {
let utilsInScope = this._getUtilInstancesWithinScope(scope);
utilsInScope.forEach((util) => {
//if(DEBUG_MODE > 2) {
console.log('Destroying Util: ', {util});
//}
util.destroy();
});
}
setup(util, scope = document.body) {
if (DEBUG_MODE > 2) {
console.log('setting up util', { util });
@ -130,12 +141,12 @@ export class UtilRegistry {
});
}
this._activeUtilInstances.push(...instances);
this._activeUtilInstancesWrapped.push(...instances);
return instances;
}
find(name) {
return this._registeredUtils.find((util) => util.name === name);
return this._registeredUtilClasses.find((util) => util.name === name);
}
_findUtilElements(util, scope) {
@ -146,11 +157,23 @@ export class UtilRegistry {
}
_findUtilIndex(name) {
return this._registeredUtils.findIndex((util) => util.name === name);
return this._registeredUtilClasses.findIndex((util) => util.name === name);
}
_getUtilInstancesWithinScope(scope) {
let utilInstances = [];
for (let activeUtilInstance of this._activeUtilInstancesWrapped) {
let util = activeUtilInstance.util;
if(this._findUtilElements(util, scope).length > 0) {
utilInstances.push(activeUtilInstance.instance);
}
}
return utilInstances;
}
_destroyUtilInstances(name) {
this._activeUtilInstances
this._activeUtilInstancesWrapped
.map((util, index) => ({
util: util,
index: index,
@ -159,11 +182,11 @@ export class UtilRegistry {
.forEach((activeUtil) => {
// destroy util instance
activeUtil.util.destroy();
delete this._activeUtilInstances[activeUtil.index];
delete this._activeUtilInstancesWrapped[activeUtil.index];
});
// get rid of now empty array slots
this._activeUtilInstances = this._activeUtilInstances.filter((util) => !!util);
this._activeUtilInstancesWrapped = this._activeUtilInstancesWrapped.filter((util) => !!util);
}
}