From 752ca8435babbca953bb7aaa71c39c120fe28e26 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 17 Apr 2025 00:20:09 +0200 Subject: [PATCH] REFA: changed from classical object to map approach --- src/context/extStore.js | 17 ++++-- src/context/scriptAndStyleContext.js | 81 ++++++++++++++-------------- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/context/extStore.js b/src/context/extStore.js index efcde8d..e9badad 100644 --- a/src/context/extStore.js +++ b/src/context/extStore.js @@ -522,12 +522,16 @@ class SStoreDefinition { /** * Resolves an overwrite case for a map/object. * @param {string} key - * @param {Object} container + * @param {Map|Object} container * @param {OverwriteBehaviour} overwriteBehaviour * @returns {string} the key to be used */ function resolveOverwrite(key, container, overwriteBehaviour) { - let occurances = Object.keys(container) + let dealAsMap = container instanceof Map; + let occurances = (dealAsMap + ? container.keys + : Object.keys(container) + ) .filter(e => e.includes(key)) .length; @@ -536,8 +540,13 @@ function resolveOverwrite(key, container, overwriteBehaviour) { break; case OverwriteBehaviour.RENAME_OLD: nameForOld = `${key}${occurances}`; - container[nameForOld] = container[key]; - delete container[key]; + if (dealAsMap) { + container.set(nameForOld, container.get(key)); + container.delete(key); + } else { + container[nameForOld] = container[key]; + delete container[key]; + } break; case OverwriteBehaviour.RENAME: default: diff --git a/src/context/scriptAndStyleContext.js b/src/context/scriptAndStyleContext.js index 4f301ec..80b6839 100644 --- a/src/context/scriptAndStyleContext.js +++ b/src/context/scriptAndStyleContext.js @@ -10,17 +10,19 @@ */ class ScriptAndStyleContext { /** - * @property {map>} functions + * @property {Map>} #css + * @type {Map>} #css */ #css; /** - * @property {map} functions + * @property {Map} #functions + * @type {Map} #functions */ #functions; constructor() { - this.#functions = {}; - this.#css = {}; + this.#functions = new Map(); + this.#css = new Map(); } /** @@ -29,7 +31,7 @@ class ScriptAndStyleContext { * @returns a name for a function (e.g. for storing) */ getFunctionName(nameAddition = "") { - return `func${this.#functions.length}${nameAddition}`; + return `func${this.#functions.size}${nameAddition}`; } /** @@ -54,7 +56,7 @@ class ScriptAndStyleContext { ].find(e => e !== ''); /* deal with name already present */ - let functionNames = Object.keys(this.#functions); + let functionNames = this.#functions.keys; if (functionNames.includes(registrationName)) { registrationName = resolveOverwrite(registrationName, this.#functions, overwriteBehaviour); } @@ -62,7 +64,7 @@ class ScriptAndStyleContext { /* clear function text */ let clearedFuncText = clearFunctionDeclarationText(fun); - this.#functions[registrationName] = new FunctionStoreBuffer(clearedFuncText); + this.#functions.set(registrationName, new FunctionStoreBuffer(clearedFuncText)); return registrationName; } @@ -79,7 +81,7 @@ class ScriptAndStyleContext { */ registerRepeatingFunction(fun, interval, underTheName = '', overwriteBehaviour = OverwriteBehaviour.RENAME, args = []) { let registrationName = this.registerPageFunction(fun, underTheName, overwriteBehaviour); - let fsb = this.#functions[registrationName]; + let fsb = this.#functions.get(registrationName); fsb.repeats = true; fsb.interval = interval; fsb.args = args; @@ -98,7 +100,7 @@ class ScriptAndStyleContext { */ executeAfterLoad(fun, delay = 1000, underTheName = '', overwriteBehaviour = OverwriteBehaviour.RENAME, interval = -1, args = []) { let registrationName = this.registerPageFunction(fun, underTheName, overwriteBehaviour); - let fsb = this.#functions[registrationName]; + let fsb = this.#functions.get(registrationName); fsb.execAfterStart = true; fsb.delay = delay; @@ -118,7 +120,7 @@ class ScriptAndStyleContext { * @returns a name for a styling (e.g. for storing) */ getStyleName(nameAddition = "") { - return `styling${this.#css.length}${nameAddition}`; + return `styling${this.#css.size}${nameAddition}`; } @@ -132,12 +134,12 @@ class ScriptAndStyleContext { * @param {map|Modifier} styleRuleMap The Styling rules/values */ registerStyling(elementIdentifier, styleRuleMap) { - if(styleRuleMap instanceof Modifier){ + if (styleRuleMap instanceof Modifier) { styleRuleMap = styleRuleMap._modifications; } - if (!Object.keys(this.#css).includes(elementIdentifier)) { - this.#css[elementIdentifier] = styleRuleMap + if (!this.#css.has(elementIdentifier)) { + this.#css.set(elementIdentifier, styleRuleMap); } return elementIdentifier; } @@ -156,13 +158,12 @@ class ScriptAndStyleContext { /* generate style tag and fill it with stored stylings */ let styleTag = document.createElement('style'); - Object.entries(this.#css) - .forEach((tuple) => { - styleTag.innerText += `${tuple[0]} {${Object.entries(tuple[1]) - .map(style => style[0] + ": " + style[1] + "; ") - .join(" ") - }} `; - }); + for (const tuple of this.#css.entries()) { + styleTag.innerText += `${tuple[0]} {${Object.entries(tuple[1]) + .map(style => style[0] + ": " + style[1] + "; ") + .join(" ") + }} `; + } head.appendChild(styleTag); @@ -177,28 +178,26 @@ class ScriptAndStyleContext { containersTag.innerText += 'const repeated = {}; '; head.appendChild(containersTag); - if (this.#functions.length > 0) { + if (this.#functions.size > 0) { let funcTag = document.createElement('script'); - Object.entries(this.#functions) - .forEach(tuple => { - let regName = tuple[0]; - let fsb = tuple[1]; - funcTag.innerText += getScriptTagInjectionText(fsb.func, regName); - - if (fsb.repeats && !fsb.execAfterStart) { - repeated[regName] = setInterval(regName, fsb.interval, fsb.args); - } - - if (!fsb.repeats && fsb.execAfterStart) { - delayed[regName] = setTimeout(regName, fsb.interval, fsb.args); - } - - if (fsb.repeats && fsb.execAfterStart) { - repeated[regName] = setInterval(regName, fsb.interval, fsb.args); - delayed[regName] = setTimeout(repeated[regName], fsb.delay, fsb.args); - } - - }); + for (const tuple of this.#functions.entries) { + let regName = tuple[0]; + let fsb = tuple[1]; + funcTag.innerText += getScriptTagInjectionText(fsb.func, regName); + + if (fsb.repeats && !fsb.execAfterStart) { + repeated[regName] = setInterval(regName, fsb.interval, fsb.args); + } + + if (!fsb.repeats && fsb.execAfterStart) { + delayed[regName] = setTimeout(regName, fsb.interval, fsb.args); + } + + if (fsb.repeats && fsb.execAfterStart) { + repeated[regName] = setInterval(regName, fsb.interval, fsb.args); + delayed[regName] = setTimeout(repeated[regName], fsb.delay, fsb.args); + } + } head.appendChild(funcTag); } }