Don't keep track of elements or Komponents

This commit is contained in:
2019-05-24 16:51:59 +02:00
parent f3a6783853
commit d7290a4769
4 changed files with 24 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
group 'nl.astraeus' group 'nl.astraeus'
version '0.1.3-SNAPSHOT' version '0.1.4-SNAPSHOT'
apply plugin: 'kotlin2js' apply plugin: 'kotlin2js'
apply plugin: 'kotlin-dce-js' apply plugin: 'kotlin-dce-js'

View File

@@ -3,7 +3,7 @@
<component name="CheckStyle-IDEA"> <component name="CheckStyle-IDEA">
<option name="configuration"> <option name="configuration">
<map> <map>
<entry key="checkstyle-version" value="8.4" /> <entry key="checkstyle-version" value="8.5" />
<entry key="copy-libs" value="false" /> <entry key="copy-libs" value="false" />
<entry key="location-0" value="BUNDLED:(bundled):Sun Checks" /> <entry key="location-0" value="BUNDLED:(bundled):Sun Checks" />
<entry key="location-1" value="BUNDLED:(bundled):Google Checks" /> <entry key="location-1" value="BUNDLED:(bundled):Google Checks" />

View File

@@ -132,9 +132,6 @@ class KompElement(
if (komp == null) { if (komp == null) {
throw IllegalStateException("komponent == null in type Komponent!") throw IllegalStateException("komponent == null in type Komponent!")
} else { } else {
komp.element?.also {
Komponent.remove(it)
}
val kompElement = komp.create() val kompElement = komp.create()
val element = kompElement.create() val element = kompElement.create()
@@ -142,8 +139,6 @@ class KompElement(
komp.kompElement = kompElement komp.kompElement = kompElement
komp.element = element komp.element = element
Komponent.define(element, komp)
element element
} }
} }

View File

@@ -3,9 +3,6 @@ package nl.astraeus.komp
import kotlinx.html.HtmlBlockTag import kotlinx.html.HtmlBlockTag
import org.w3c.dom.HTMLElement import org.w3c.dom.HTMLElement
import org.w3c.dom.Node import org.w3c.dom.Node
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
fun HtmlBlockTag.include(component: Komponent) { fun HtmlBlockTag.include(component: Komponent) {
val consumer = this.consumer val consumer = this.consumer
@@ -27,8 +24,6 @@ abstract class Komponent {
open fun create(): KompElement { open fun create(): KompElement {
val result = render(KompConsumer()) val result = render(KompConsumer())
//result.komponent = this
return result return result
} }
@@ -36,14 +31,14 @@ abstract class Komponent {
open fun refresh() { open fun refresh() {
if (!rendered) { if (!rendered) {
refresh(element) refresh(this)
} else { } else {
update() update()
} }
} }
open fun update() { open fun update() {
refresh(element) refresh(this)
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
@@ -66,8 +61,6 @@ abstract class Komponent {
companion object { companion object {
private val elements: MutableMap<Node, Komponent> = HashMap()
var logRenderEvent = false var logRenderEvent = false
var logReplaceEvent = false var logReplaceEvent = false
var logEquals = false var logEquals = false
@@ -84,13 +77,9 @@ abstract class Komponent {
parent.replaceChild(newElement, oldElement) parent.replaceChild(newElement, oldElement)
elements.remove(oldElement)
newKomponent.komponent?.also { newKomponent.komponent?.also {
it.kompElement = newKomponent it.kompElement = newKomponent
it.element = newElement it.element = newElement
elements[newElement] = it
} }
return newElement return newElement
@@ -104,23 +93,18 @@ abstract class Komponent {
} }
parent.removeChild(element) parent.removeChild(element)
elements.remove(element)
} }
fun appendElement(element: Node, kompElement: KompElement) { fun appendElement(element: Node, kompElement: KompElement) {
val newElement = kompElement.create() val newElement = kompElement.create()
if (Komponent.logReplaceEvent) { if (logReplaceEvent) {
console.log("Append", newElement) console.log("Append", newElement)
} }
element.appendChild(newElement) element.appendChild(newElement)
} }
fun define(element: Node, component: Komponent) {
elements[element] = component
}
fun create(parent: HTMLElement, component: Komponent, insertAsFirst: Boolean = false) { fun create(parent: HTMLElement, component: Komponent, insertAsFirst: Boolean = false) {
component.kompElement = component.create() component.kompElement = component.create()
val element = component.kompElement?.create() val element = component.kompElement?.create()
@@ -132,61 +116,33 @@ abstract class Komponent {
} }
component.element = element component.element = element
elements[element] = component
} }
} }
fun remove(element: Node) { fun refresh(komponent: Komponent) {
elements.remove(element) komponent.element?.let { element ->
}
@JsName("remove")
fun remove(component: Komponent) {
for ((key, value) in elements) {
if (value == component) {
elements.remove(key)
}
}
}
fun refresh(component: Komponent) {
refresh(component.element)
}
fun refresh(element: Node?) {
if (element != null) {
elements[element]?.let {
if (logRenderEvent) { if (logRenderEvent) {
console.log("Rendering", it) console.log("Rendering", komponent)
} }
//val parent = element.parentElement //val parent = element.parentElement
val newElement = it.create() val newElement = komponent.create()
val kompElement = komponent.kompElement
if (updateStrategy == UpdateStrategy.REPLACE) { val replacedElement = if (updateStrategy == UpdateStrategy.REPLACE) {
//val replacedElement = replaceNode(newElement, element) //val replacedElement = replaceNode(newElement, element)
val replacedElement = replaceNode(newElement, element) replaceNode(newElement, element)
it.element = replacedElement } else if (kompElement != null) {
elements[replacedElement] = it
} else {
val kompElement = it.kompElement
val replacedElement = if (kompElement != null) {
DomDiffer.replaceDiff(kompElement, newElement, element) DomDiffer.replaceDiff(kompElement, newElement, element)
} else { } else {
newElement.create() newElement.create()
} }
it.kompElement = newElement komponent.kompElement = newElement
it.element = replacedElement komponent.element = replacedElement
komponent.rendered = true
elements[replacedElement] = it
}
elements.remove(element)
it.rendered = true
}
} }
} }
} }