Skip to content

Commit

Permalink
feat: add tiny dedupe
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou committed Oct 19, 2023
1 parent e71e895 commit 4518e57
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/pw/mihou/nexus/features/react/React.kt
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class React internal constructor(private val api: DiscordApi, private val render
val oldValue = _value.get()
_value.set(value)

subscribers.forEach { Nexus.launcher.launch { it(oldValue, value) } }
this.react(oldValue, value)
}

/**
Expand All @@ -231,7 +231,7 @@ class React internal constructor(private val api: DiscordApi, private val render
_value.getAndUpdate(updater)

val value = _value.get()
subscribers.forEach { Nexus.launcher.launch { it(oldValue, value) } }
this.react(oldValue, value)
}

/**
Expand All @@ -254,6 +254,17 @@ class React internal constructor(private val api: DiscordApi, private val render
return { subscribers.remove(subscription) }
}

/**
* Reacts to the change and executes all the subscriptions that were subscribed at the
* time of execution.
*
* @param oldValue the old value.
* @param value the current value.
*/
internal fun react(oldValue: T, value: T) {
subscribers.forEach { Nexus.launcher.launch { it(oldValue, value) } }
}

override fun toString(): String {
return _value.get().toString()
}
Expand Down

0 comments on commit 4518e57

Please sign in to comment.