Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add the ability to register multiple listeners for a property #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/Vigilance.api
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public final class gg/essential/vigilance/data/PropertyData {
public static final field Companion Lgg/essential/vigilance/data/PropertyData$Companion;
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributes;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
public fun <init> (Lgg/essential/vigilance/data/PropertyAttributesExt;Lgg/essential/vigilance/data/PropertyValue;Lgg/essential/vigilance/Vigilant;)V
public final fun addAction (Lkotlin/jvm/functions/Function1;)V
public final fun component1 ()Lgg/essential/vigilance/data/PropertyAttributes;
public final fun component2 ()Lgg/essential/vigilance/data/PropertyValue;
public final fun component3 ()Lgg/essential/vigilance/Vigilant;
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/gg/essential/vigilance/Vigilant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ abstract class Vigilant @JvmOverloads constructor(
propertyCollector
.getProperties()
.firstOrNull { it.value is FieldBackedPropertyValue && it.value.field == field }!!
.action = { obj -> listener.accept(obj as T) }
.addAction { obj -> listener.accept(obj as T) }
}

@Suppress("UNCHECKED_CAST")
fun <T> registerListener(propertyName: String, listener: Consumer<T>) {
propertyCollector.getProperties()
.firstOrNull { it.value is FieldBackedPropertyValue && it.value.field.name == propertyName }!!
.action = { obj -> listener.accept(obj as T) }
.addAction { obj -> listener.accept(obj as T) }
}

@Deprecated(
Expand Down Expand Up @@ -452,7 +452,7 @@ abstract class Vigilant @JvmOverloads constructor(
)

if (action != null) {
data.action = { action(it as T) }
data.addAction { action(it as T) }
}

properties.add(data)
Expand Down
21 changes: 19 additions & 2 deletions src/main/kotlin/gg/essential/vigilance/data/PropertyData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ data class PropertyData(@Deprecated("Replace with attributesExt", ReplaceWith("a

var attributesExt: PropertyAttributesExt = PropertyAttributesExt(attributes)
private set

@Deprecated("Replace with addAction", ReplaceWith("addAction"))
var action: ((Any?) -> Unit)? = null
set(value) {
if (value != null) {
actions.add(value)
} else {
actions.remove(field)
}

field = value
}

private val actions = mutableListOf<(Any?) -> Unit>()
var dependsOn: PropertyData? = null
var hasDependants: Boolean = false

Expand All @@ -40,16 +53,20 @@ data class PropertyData(@Deprecated("Replace with attributesExt", ReplaceWith("a
}

if (attributesExt.triggerActionOnInitialization || this.value.initialized)
action?.invoke(value)
actions.forEach { it.invoke(value) }

this.value.initialized = true
this.value.setValue(value, instance)

instance.markDirty()
}

fun addAction(action: (Any?) -> Unit) {
actions.add(action)
}

fun setCallbackConsumer(callback: Consumer<Any?>) {
this.action = callback::accept
this.actions.add(callback::accept)
}

companion object {
Expand Down