Skip to content

Commit

Permalink
Fix defaulting provider not propagating upwards
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Jul 31, 2024
1 parent 4415be7 commit c8e3386
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@ import xyz.xenondevs.commons.provider.immutable.provider
* Creates a new [MutableProvider] that defaults to [value] if the value of [this][MutableProvider] is null.
* The default value is propagated upwards when the value of the returned provider is loaded.
*/
fun <T : Any> MutableProvider<T?>.defaultsTo(value: T): MutableProvider<T> {
val provider = MutableDefaultValueProvider(this, value)
addChild(provider)
return provider
}
fun <T : Any> MutableProvider<T?>.defaultsTo(value: T): MutableProvider<T> =
MutableDefaultValueProvider(this, value)

/**
* Creates a new [MutableProvider] that defaults to the value obtained through [provider] if the value of [this][MutableProvider] is null.
* The default value is propagated upwards when the value of the returned provider is loaded.
* Once the value has been propagated upwards, changes to the value of [provider] will be ignored.
*/
fun <T : Any> MutableProvider<T?>.defaultsTo(provider: Provider<T>): MutableProvider<T> {
val result = MutableDefaultProviderProvider(this, provider)
addChild(result)
return result
}
fun <T : Any> MutableProvider<T?>.defaultsTo(provider: Provider<T>): MutableProvider<T> =
MutableDefaultProviderProvider(this, provider)

/**
* Creates a new [MutableProvider] that defaults to the value obtained through the [lazyValue] lambda if the value of [this][MutableProvider] is null.
Expand All @@ -47,6 +41,11 @@ private abstract class MutableDefaultingProvider<T : Any>(
return value
}

override fun set(value: T, ignoredChildren: Set<Provider<*>>) {
super.set(value, ignoredChildren)
provider.set(value, setOf(this))
}

protected abstract fun applyDefaultValue(): T

}
Expand All @@ -56,6 +55,10 @@ private class MutableDefaultValueProvider<T : Any>(
private val defaultValue: T
) : MutableDefaultingProvider<T>(provider) {

init {
provider.addChild(this)
}

override fun applyDefaultValue(): T {
provider.set(defaultValue, setOf(this))
return defaultValue
Expand All @@ -68,6 +71,10 @@ private class MutableDefaultProviderProvider<T : Any>(
private val defaultProvider: Provider<T>
) : MutableDefaultingProvider<T>(provider) {

init {
provider.addChild(this)
}

override fun applyDefaultValue(): T {
val defaultValue = defaultProvider.get()
provider.set(defaultValue, setOf(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class DefaultingProviderTests {
assertEquals(null, provider.get())
assertEquals(1, defaultingProvider.get())
assertEquals(1, provider.get())

// expected write propagation
defaultingProvider.set(2)
assertEquals(2, defaultingProvider.get())
assertEquals(2, provider.get())
}

@Test
Expand All @@ -27,6 +32,11 @@ class DefaultingProviderTests {
assertEquals(null, provider.get())
assertEquals(1, defaultingProvider.get())
assertEquals(1, provider.get())

// expected write propagation
defaultingProvider.set(2)
assertEquals(2, defaultingProvider.get())
assertEquals(2, provider.get())
}

@Test
Expand All @@ -48,6 +58,11 @@ class DefaultingProviderTests {
assertEquals(2, fallback.get())
assertEquals(1, defaultingProvider.get())
assertEquals(1, provider.get())

// expected write propagation
defaultingProvider.set(3)
assertEquals(3, defaultingProvider.get())
assertEquals(3, provider.get())
}

}

0 comments on commit c8e3386

Please sign in to comment.