From f1ef2e5ec776ad8a1fb7737eb0ed4f79218afd61 Mon Sep 17 00:00:00 2001 From: Johannes Eder Date: Fri, 18 Jun 2021 13:51:02 +0200 Subject: [PATCH] feat(util_registry): impelmented destroyAll(scope) method in the utilRegistry --- .../src/lib/event-manager/event-manager.js | 2 + .../services/util-registry/util-registry.js | 45 ++++++++++++++----- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/frontend/src/lib/event-manager/event-manager.js b/frontend/src/lib/event-manager/event-manager.js index 810cfab55..e8121131f 100644 --- a/frontend/src/lib/event-manager/event-manager.js +++ b/frontend/src/lib/event-manager/event-manager.js @@ -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 }); diff --git a/frontend/src/services/util-registry/util-registry.js b/frontend/src/services/util-registry/util-registry.js index 65e9326cc..dd4a38af6 100644 --- a/frontend/src/services/util-registry/util-registry.js +++ b/frontend/src/services/util-registry/util-registry.js @@ -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); } }