Skip to content
This repository was archived by the owner on Aug 10, 2024. It is now read-only.

Commit b2db71d

Browse files
committed
deprecate old Component approach rather than removing it
1 parent cb9372d commit b2db71d

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

api/kweb-core.api

+9
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,10 @@ public final class kweb/routing/RouteReceiver {
17201720
public final fun path (Ljava/lang/String;Lkotlin/jvm/functions/Function2;)V
17211721
}
17221722

1723+
public abstract interface class kweb/state/AdvancedComponent {
1724+
public abstract fun render (Lkweb/ElementCreator;)Ljava/lang/Object;
1725+
}
1726+
17231727
public final class kweb/state/CloseReason {
17241728
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
17251729
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
@@ -1734,6 +1738,10 @@ public final class kweb/state/CloseReason {
17341738
public fun toString ()Ljava/lang/String;
17351739
}
17361740

1741+
public abstract interface class kweb/state/Component : kweb/state/AdvancedComponent {
1742+
public abstract fun render (Lkweb/ElementCreator;)V
1743+
}
1744+
17371745
public class kweb/state/KVal : java/lang/AutoCloseable {
17381746
public fun <init> (Ljava/lang/Object;)V
17391747
public final fun addListener (Lkotlin/jvm/functions/Function2;)J
@@ -1853,6 +1861,7 @@ public final class kweb/state/RenderHandle {
18531861

18541862
public final class kweb/state/RenderKt {
18551863
public static final fun closeOnElementCreatorCleanup (Lkweb/ElementCreator;Lkweb/state/KVal;)V
1864+
public static final fun render (Lkweb/ElementCreator;Lkweb/state/AdvancedComponent;)Ljava/lang/Object;
18561865
public static final fun render (Lkweb/ElementCreator;Lkweb/state/KVal;Lkotlin/jvm/functions/Function2;)Lkweb/state/RenderFragment;
18571866
}
18581867

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package kweb.components
2+
3+
import kweb.ElementCreator
4+
5+
/**
6+
* Typealias for [ElementCreator] to simply create and manage components using an
7+
* extension function
8+
*/
9+
typealias Component = ElementCreator<*>

src/main/kotlin/kweb/state/render.kt

+39-3
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,46 @@ fun ElementCreator<*>.closeOnElementCreatorCleanup(kv: KVal<*>) {
125125
}
126126

127127
/**
128-
* Typealias for [ElementCreator] to simply create and manage components using an
129-
* extension function
128+
* Render the value of a [KVar] into DOM elements, and automatically re-render those
129+
* elements whenever the value changes.
130+
*/
131+
@Deprecated("Use kweb.components.Component instead, see: https://docs.kweb.io/book/components.html")
132+
fun <PARENT_ELEMENT_TYPE : Element, RETURN_TYPE> ElementCreator<PARENT_ELEMENT_TYPE>.render(
133+
component: AdvancedComponent<PARENT_ELEMENT_TYPE, RETURN_TYPE>
134+
) : RETURN_TYPE {
135+
return component.render(this)
136+
}
137+
138+
139+
/**
140+
* [AdvancedComponent]s can be rendered into DOM elements by calling [AdvancedComponent.render].
141+
*
142+
* Unlike [Component], [AdvancedComponent]s allows the parent element type to be configured, and a return
143+
* type to be specified.
130144
*/
131-
typealias Component = ElementCreator<*>
145+
@Deprecated("Use kweb.components.Component instead, see: https://docs.kweb.io/book/components.html")
146+
interface AdvancedComponent<in PARENT_ELEMENT_TYPE : Element, out RETURN_TYPE> {
147+
148+
/**
149+
* Render this [Component] into DOM elements, returning an arbitrary
150+
* value of type [RETURN_TYPE].
151+
*/
152+
fun render(elementCreator: ElementCreator<PARENT_ELEMENT_TYPE>) : RETURN_TYPE
153+
}
154+
155+
/**
156+
* [Component]s can be rendered into DOM elements by calling [Component.render].
157+
*
158+
* For more flexibility, see [AdvancedComponent].
159+
*/
160+
@Deprecated("Use kweb.components.Component instead, see: https://docs.kweb.io/book/components.html")
161+
interface Component : AdvancedComponent<Element, Unit> {
162+
163+
/**
164+
* Render this [Component] into DOM elements
165+
*/
166+
override fun render(elementCreator: ElementCreator<Element>)
167+
}
132168

133169
class RenderFragment(val startId: String, val endId: String) {
134170
private val deletionListeners = ArrayList<() -> Unit>()

src/test/kotlin/kweb/docs/components.kt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kweb.docs
22

33
import kweb.*
44
import kweb.InputType.text
5+
import kweb.components.Component
56
import kweb.state.*
67
import kweb.util.json
78

0 commit comments

Comments
 (0)