Skip to content

Commit

Permalink
feat: add support for first and render subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Oct 16, 2023
1 parent b8bc0da commit c66f83c
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/main/java/pw/mihou/nexus/features/command/react/React.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import kotlin.reflect.KProperty
typealias Subscription<T> = (oldValue: T, newValue: T) -> Unit
typealias Unsubscribe = () -> Unit

typealias RenderSubscription = () -> Unit

class React internal constructor(private val api: DiscordApi) {
private var message: NexusMessage = NexusMessage()
private var message: NexusMessage? = null
private var unsubscribe: Unsubscribe = {}
private var component: (Component.() -> Unit)? = null

Expand All @@ -28,15 +30,32 @@ class React internal constructor(private val api: DiscordApi) {

internal var resultingMessage: Message? = null

internal var firstRenderSubscribers = mutableListOf<RenderSubscription>()
internal var renderSubscribers = mutableListOf<RenderSubscription>()

companion object {
var debounceMillis = 250L
}

fun view() = message
fun view() = message ?: NexusMessage()

fun onRender(subscription: RenderSubscription) {
renderSubscribers.add(subscription)
}

fun onInitialRender(subscription: RenderSubscription) {
firstRenderSubscribers.add(subscription)
}

fun render(component: Component.() -> Unit) {
val element = apply(component)

if (message == null) {
firstRenderSubscribers.forEach { it() }
}

renderSubscribers.forEach { it() }

val (unsubscribe, message) = element.render(api)
this.message = message
this.unsubscribe = unsubscribe
Expand Down

0 comments on commit c66f83c

Please sign in to comment.