package at.hannibal2.skyhanni.utils import io.github.notenoughupdates.moulconfig.observer.Observer import io.github.notenoughupdates.moulconfig.observer.Property object ConditionalUtils { fun T.transformIf(condition: T.() -> Boolean, transformation: T.() -> T) = if (condition()) transformation(this) else this fun T.conditionalTransform(condition: Boolean, ifTrue: T.() -> Any, ifFalse: T.() -> Any) = if (condition) ifTrue(this) else ifFalse(this) // MoulConfig is in Java, I don't want to downgrade this logic fun onChange(vararg properties: Property, observer: Observer) { for (property in properties) { property.whenChanged { a, b -> observer.observeChange(a, b) } } } fun onToggle(vararg properties: Property, observer: Runnable) { onChange(*properties) { _, _ -> observer.run() } } fun Property.onToggle(observer: Runnable) { whenChanged { _, _ -> observer.run() } } fun Property.afterChange(observer: T.() -> Unit) { whenChanged { _, new -> observer(new) } } }