restructure and less wasted time while rendering

This commit is contained in:
jusax23 2022-12-07 21:08:53 +01:00
parent 15a4048730
commit bf9908ded1

47
main.js
View file

@ -154,8 +154,7 @@ export class Stear{
*/
static addAnimation(steps, name = "stearAnimation_" + counter++) {
Stear.addGlobalStyleText(`@keyframes ${name} {
${
Object.entries(steps).map(([k,d])=>
${Object.entries(steps).map(([k, d]) =>
` ${k} {
${Object.entries(d).map(d => " " + toCssAttr(d[0]) + ": " + d[1] + ";").join("\n")}
}`).join("\n")
@ -503,6 +502,8 @@ export class class_ {
#find;
#doBuild;
#dynamicState = 0;
/**
* Generate a new Stear render Node.
*
@ -543,6 +544,7 @@ export class class_ {
set childs(childs) {
if (!this.#doBuild) return;
this.#childs = Array.isArray(childs) ? childs : [childs];
this.#dynamicState = 0;
}
/**
@ -564,6 +566,20 @@ export class class_ {
}
}
async #syncBuild(args) {
for (let i = 0; i < this.#childs.length; i++) {
let elem = this.#childs[i];
if (Array.isArray(elem)) {
for (let j = 0; j < elem.length; j++) {
if (elem[j] instanceof class_) await elem[j].build(args);
}
} else {
if (elem instanceof class_) await elem.build(args);
}
}
}
/**
* Build Stear Structure
*
@ -571,11 +587,15 @@ export class class_ {
*/
async build(args) {
if (!this.#doBuild) return;
if (this.#dynamicState < 0) return void await this.#syncBuild(args);
this.#build = [];
for (let i = 0; i < this.#childs.length; i++) {
let elem = this.#childs[i];
if (typeof elem == "function") elem = (await elem(...args))??[];
if (typeof elem == "function") {
elem = (await elem(...args)) ?? [];
this.#dynamicState = 2;
}
if (Array.isArray(elem)) {
for (let j = 0; j < elem.length; j++) {
@ -587,6 +607,17 @@ export class class_ {
this.#build.push(elem);
}
}
if (this.#dynamicState <= 0) this.#dynamicState = -1;
}
#syncRender() {
let out = [];
for (let i = 0; i < this.#build.length; i++) {
const elem = this.#build[i];
if (typeof elem != "string" && !(elem instanceof LanguagePoolString)) {
out[i] = elem.render;
}
}
}
/**
@ -596,6 +627,10 @@ export class class_ {
*/
get render() {
if (!this.#doBuild) return this.#elem;
if (this.#dynamicState < -1) {
this.#syncRender();
return this.#elem;
}
let out = [];
for (let i = 0; i < this.#build.length; i++) {
const elem = this.#build[i];
@ -606,6 +641,7 @@ export class class_ {
}
}
this.#elem.replaceChildren(...out);
if (this.#dynamicState < 0) this.#dynamicState = -2;
return this.#elem;
}
@ -616,6 +652,11 @@ export class class_ {
return this.#elem;
}
async rerender(args = []) {
await this.build(args);
this.render;
}
/**
* Returns find Object
*