Skip to content

Commit

Permalink
feat: add support for delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Oct 16, 2023
1 parent b66c37e commit 279895c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main/java/pw/mihou/nexus/features/command/react/React.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import java.io.InputStream
import java.time.Instant
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.locks.ReentrantLock
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

typealias Subscription<T> = (oldValue: T, newValue: T) -> Unit
typealias Unsubscribe = () -> Unit

class React(private val api: DiscordApi) {
class React internal constructor(private val api: DiscordApi) {
private var message: NexusMessage = NexusMessage()
private var unsubscribe: Unsubscribe = {}
private var component: (Component.() -> Unit)? = null
Expand Down Expand Up @@ -91,6 +93,12 @@ class React(private val api: DiscordApi) {
class Writable<T>(value: T) {
private val subscribers: MutableList<Subscription<T>> = mutableListOf()
private val _value: AtomicReference<T> = AtomicReference(value)
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
return _value.get()
}
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
set(value)
}
fun set(value: T) {
val oldValue = _value.get()
_value.set(value)
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/commands/ReactiveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ReactiveTest: NexusHandler {
val description = "A test regarding React"
override fun onEvent(event: NexusCommandEvent) {
event.R {
val clicks = writable(0)
var clicks by writable(0)
render {
Embed {
Title("R.Embeds")
Expand All @@ -21,7 +21,7 @@ class ReactiveTest: NexusHandler {
}
Button(label = "Click to be DM'd") { event ->
event.interaction.user.sendMessage("Hi")
clicks.getAndUpdate { it + 1 }
clicks += 1
}
}
}
Expand Down

0 comments on commit 279895c

Please sign in to comment.