Browse Source

REFA: Moved functions to their logical position

master
chris 1 month ago
parent
commit
170d00d852
  1. 76
      src/context/extStore.js
  2. 78
      src/context/scriptAndStyleContext.js

76
src/context/extStore.js

@ -64,6 +64,82 @@ const OverwriteBehaviour = Object.freeze({
MOVE_ELEMENT_SPECIFIC: "MOVE_ELEMENT_SPECIFIC"
});
/**
* Is supposed to shrink all empty strings to length 1
* @param {Function} func
* @returns {string}
*/
function clearFunctionDeclarationText(func) {
function shrinkEmptyStrings(text) {
for (let i = 1; i < 10; i++) {
text = text.replaceAll(" ".slice(i), ' ');
}
return text;
}
return shrinkEmptyStrings(
func.toString()
.replaceAll('\n', ' ')
.replaceAll('\r\n', ' ')
.replaceAll('\n\r', ' ')
);
}
/**
*
* @param {Function} func
* @param {string} registrationName
* @returns {string}
*/
function getScriptTagInjectionText(func, registrationName) {
let funcHasName = func.name && func.name.trim() !== '';
if (func.startWith('function')) {
let label = `function ${registrationName}`;
let isNameInFuncText = func.startWith(label);
if (funcHasName && isNameInFuncText) {
return func;
} else {
return [label, '(', func.split('(', 1)[1]].join('')
}
} else {
return `const ${registrationName} = ${func}; `;
}
}
/**
* Stores a function until generate is called.
* Then the additional informations of the store wil be applied
* and the funcitons added to the page.
*/
class FunctionStoreBuffer {
/**
* Stores a function until generate is called.
* Then the additional informations of the store wil be applied
* and the funcitons added to the page.
* @param {Function} func the function that will be stored
* @param {Array<any>} args additional arguments that will be given to the function
* @param {boolean} repeats weither the funciton is supposed to execute repeatedly
* @param {number} interval the time in milliseconds between executions
* @param {boolean} execAfterStart weither the function is supposed to be executed after pageload
* @param {number} delay the time in milliseconds the execution will be delayed
*/
constructor(
func,
args = [],
repeats = false,
interval = -1,
execAfterStart = false,
delay = -1
) {
this.func = func;
this.args = args;
this.execAfterStart = execAfterStart;
this.delay = delay;
this.repeats = repeats;
this.interval = interval;
}
}
/**
* Extracted this super class to differentiate between
* internal and external store.

78
src/context/scriptAndStyleContext.js

@ -4,82 +4,6 @@
* @copyright by its creator Christian Martin
*/
/**
* Is supposed to shrink all empty strings to length 1
* @param {Function} func
* @returns {string}
*/
function clearFunctionDeclarationText(func) {
function shrinkEmptyStrings(text) {
for (let i = 1; i < 10; i++) {
text = text.replaceAll(" ".slice(i), ' ');
}
return text;
}
return shrinkEmptyStrings(
func.toString()
.replaceAll('\n', ' ')
.replaceAll('\r\n', ' ')
.replaceAll('\n\r', ' ')
);
}
/**
*
* @param {Function} func
* @param {string} registrationName
* @returns {string}
*/
function getScriptTagInjectionText(func, registrationName) {
let funcHasName = func.name && func.name.trim() !== '';
if (func.startWith('function')) {
let label = `function ${registrationName}`;
let isNameInFuncText = func.startWith(label);
if (funcHasName && isNameInFuncText) {
return func;
} else {
return [label, '(', func.split('(', 1)[1]].join('')
}
} else {
return `const ${registrationName} = ${func}; `;
}
}
/**
* Stores a function until generate is called.
* Then the additional informations of the store wil be applied
* and the funcitons added to the page.
*/
class FunctionStoreBuffer {
/**
* Stores a function until generate is called.
* Then the additional informations of the store wil be applied
* and the funcitons added to the page.
* @param {Function} func the function that will be stored
* @param {Array<any>} args additional arguments that will be given to the function
* @param {boolean} repeats weither the funciton is supposed to execute repeatedly
* @param {number} interval the time in milliseconds between executions
* @param {boolean} execAfterStart weither the function is supposed to be executed after pageload
* @param {number} delay the time in milliseconds the execution will be delayed
*/
constructor(
func,
args = [],
repeats = false,
interval = -1,
execAfterStart = false,
delay = -1
) {
this.func = func;
this.args = args;
this.execAfterStart = execAfterStart;
this.delay = delay;
this.repeats = repeats;
this.interval = interval;
}
}
/**
* @abstract
* Class adds function and style storing properties to the context (PageBuilder).
@ -274,4 +198,4 @@ class ScriptAndStyleContext {
head.appendChild(funcTag);
}
}
}
}

Loading…
Cancel
Save