Skip to content

Commit

Permalink
provide a better way to configure render container element (related to
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Jun 28, 2020
1 parent 0fe32f9 commit c2f49c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/main/kotlin/kweb/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import kotlin.reflect.KClass
open class Element(override val browser: WebBrowser, val creator: ElementCreator<*>?, open var jsExpression: String, val tag: String? = null, val id: String?) :
EventGenerator<Element> {
constructor(element: Element) : this(element.browser, element.creator, jsExpression = element.jsExpression, tag = element.tag, id = element.id)
/*********
********* Low level methods
*********/

/**
* Execute some JavaScript in the browser. This is the
Expand Down Expand Up @@ -63,6 +60,8 @@ open class Element(override val browser: WebBrowser, val creator: ElementCreator
fun <P : KwebPlugin> plugin(plugin: KClass<P>) = browser.plugin(plugin)


val children: List<Element> = ArrayList()

/**
* Obtain an [ElementReader] that can be used to read various properties of this element.
*/
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/kweb/state/render.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kweb.state

import kweb.*
import kweb.html.style.StyleReceiver
import kweb.shoebox.KeyValue
import kweb.shoebox.OrderedViewSet
import kweb.shoebox.Shoebox
Expand All @@ -15,10 +16,10 @@ import java.util.concurrent.atomic.AtomicReference

private val logger = KotlinLogging.logger {}

fun <T : Any?> ElementCreator<*>.render(kval: KVal<T>, containerElementTag : String = "span", block: ElementCreator<Element>.(T) -> Unit) {
fun <T : Any?> ElementCreator<*>.render(value: KVal<T>, container : ElementCreator<*>.() -> Element = { span() }, block: ElementCreator<Element>.(T) -> Unit) {

// NOTE: Per https://github.com/kwebio/kweb-core/issues/151 eventually render() won't rely on a container element.
val containerElement : Element = element(containerElementTag)
val containerElement : Element = container(this)

val previousElementCreator: AtomicReference<ElementCreator<Element>?> = AtomicReference(null)

Expand All @@ -30,15 +31,15 @@ fun <T : Any?> ElementCreator<*>.render(kval: KVal<T>, containerElementTag : Str
containerElement.new {
previousElementCreator.getAndSet(this)?.cleanup()
renderState.set(RENDERING_NO_PENDING_CHANGE)
block(kval.value)
StyleReceiver.DisplayValues.block(value.value)
if (renderState.get() == RENDERING_NO_PENDING_CHANGE) {
renderState.set(NOT_RENDERING)
}
}
} while (renderState.get() != NOT_RENDERING)
}

val listenerHandle = kval.addListener { _, _ ->
val listenerHandle = value.addListener { _, _ ->
when (renderState.get()) {
NOT_RENDERING -> {
eraseAndRender()
Expand All @@ -55,7 +56,7 @@ fun <T : Any?> ElementCreator<*>.render(kval: KVal<T>, containerElementTag : Str
containerElement.new {
previousElementCreator.getAndSet(this)?.cleanup()
renderState.set(RENDERING_NO_PENDING_CHANGE)
block(kval.value)
StyleReceiver.DisplayValues.block(value.value)
if (renderState.get() == RENDERING_WITH_PENDING_CHANGE) {
eraseAndRender()
} else {
Expand All @@ -70,7 +71,7 @@ fun <T : Any?> ElementCreator<*>.render(kval: KVal<T>, containerElementTag : Str

this.onCleanup(true) {
previousElementCreator.getAndSet(null)?.cleanup()
kval.removeListener(listenerHandle)
value.removeListener(listenerHandle)
}
}

Expand Down

0 comments on commit c2f49c7

Please sign in to comment.