Skip to content

Commit

Permalink
Merge pull request #575 from KovalevAndrey/2.x-align-customisations-w…
Browse files Browse the repository at this point in the history
…ith-1.x

Align customisation implementation with 1.x implementation
  • Loading branch information
KovalevAndrey authored Aug 31, 2023
2 parents d4f2610 + 17323a8 commit 92a0f06
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 43 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Pending changes

### Fixed

- [#575](https://github.com/bumble-tech/appyx/pull/575) - Make customisations lazy to improve performance

## 2.0.0-alpha03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,49 @@ actual open class NodeCustomisationDirectoryImpl actual constructor(
override val parent: NodeCustomisationDirectory?
) : MutableNodeCustomisationDirectory {

private val map: MutableMap<Any, Any> = hashMapOf()
private val map: MutableMap<Any, Lazy<*>> = hashMapOf()

override fun <T : NodeCustomisation> put(key: KClass<T>, value: T) {
map[key] = value
}

fun <T : Any> put(vararg values: T) {
values.forEach {
map[it::class] = it
}
}

inline operator fun <reified T : Any> T.unaryPlus() {
put(this)
override fun <T : NodeCustomisation> put(key: KClass<T>, valueProvider: () -> T) {
map[key] = lazy(valueProvider)
}

override fun <T : NodeCustomisation> get(key: KClass<T>): T? =
map[key] as? T
getValue(key)

override fun <T : NodeCustomisation> getRecursively(key: KClass<T>): T? =
get(key) ?: parent?.get(key)

override fun <T : Any> putSubDirectory(key: KClass<T>, value: NodeCustomisationDirectory) {
map[key] = value
override fun <T : Any> putSubDirectory(
key: KClass<T>,
valueProvider: () -> NodeCustomisationDirectory
) {
map[key] = lazy(valueProvider)
}

override fun <T : Any> getSubDirectory(key: KClass<T>): NodeCustomisationDirectory? =
map[key] as? NodeCustomisationDirectory
getValue(key)

override fun <T : Any> getSubDirectoryOrSelf(key: KClass<T>): NodeCustomisationDirectory {
val subDir = map.keys.firstOrNull {
it is KClass<*> && it.java.isAssignableFrom(key.java)
}

return map[subDir] as? NodeCustomisationDirectory ?: this
return getValue(subDir) ?: this
}

operator fun KClass<*>.invoke(block: NodeCustomisationDirectoryImpl.() -> Unit) {
if (map.containsKey(this)) {
// TODO warning for accidental override?
}
map[this] = NodeCustomisationDirectoryImpl(this@NodeCustomisationDirectoryImpl).apply(block)
map[this] = lazy {
NodeCustomisationDirectoryImpl(this@NodeCustomisationDirectoryImpl).apply(block)
}
}

/**
* Gets a value from the map, if the value is lazy the lazy value will be initialized.
*/
private fun <T : Any> getValue(key: Any?): T? =
map[key]?.value as? T

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ package com.bumble.appyx.utils.customisations
import kotlin.reflect.KClass

interface MutableNodeCustomisationDirectory : NodeCustomisationDirectory {
fun <T : Any> putSubDirectory(key: KClass<T>, valueProvider: () -> NodeCustomisationDirectory)

fun <T : Any> putSubDirectory(key: KClass<T>, value: NodeCustomisationDirectory)
fun <T : NodeCustomisation> put(key: KClass<T>, valueProvider: () -> T)
}

inline fun <reified T : Any> MutableNodeCustomisationDirectory.putSubDirectory(
noinline valueProvider: () -> NodeCustomisationDirectory
) {
putSubDirectory(T::class, valueProvider)
}

fun <T : NodeCustomisation> put(key: KClass<T>, value: T)
inline fun <reified T : NodeCustomisation> MutableNodeCustomisationDirectory.put(
noinline valueProvider: () -> T
) {
put(T::class, valueProvider)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,49 @@ actual open class NodeCustomisationDirectoryImpl actual constructor(
override val parent: NodeCustomisationDirectory?
) : MutableNodeCustomisationDirectory {

private val map: MutableMap<Any, Any> = hashMapOf()
private val map: MutableMap<Any, Lazy<*>> = hashMapOf()

override fun <T : NodeCustomisation> put(key: KClass<T>, value: T) {
map[key] = value
}

fun <T : Any> put(vararg values: T) {
values.forEach {
map[it::class] = it
}
}

inline operator fun <reified T : Any> T.unaryPlus() {
put(this)
override fun <T : NodeCustomisation> put(key: KClass<T>, valueProvider: () -> T) {
map[key] = lazy(valueProvider)
}

override fun <T : NodeCustomisation> get(key: KClass<T>): T? =
map[key] as? T
getValue(key)

override fun <T : NodeCustomisation> getRecursively(key: KClass<T>): T? =
get(key) ?: parent?.get(key)

override fun <T : Any> putSubDirectory(key: KClass<T>, value: NodeCustomisationDirectory) {
map[key] = value
override fun <T : Any> putSubDirectory(
key: KClass<T>,
valueProvider: () -> NodeCustomisationDirectory
) {
map[key] = lazy(valueProvider)
}

override fun <T : Any> getSubDirectory(key: KClass<T>): NodeCustomisationDirectory? =
map[key] as? NodeCustomisationDirectory
getValue(key)

override fun <T : Any> getSubDirectoryOrSelf(key: KClass<T>): NodeCustomisationDirectory {
val subDir = map.keys.firstOrNull {
it is KClass<*> && it.java.isAssignableFrom(key.java)
}

return map[subDir] as? NodeCustomisationDirectory ?: this
return getValue(subDir) ?: this
}

operator fun KClass<*>.invoke(block: NodeCustomisationDirectoryImpl.() -> Unit) {
if (map.containsKey(this)) {
// TODO warning for accidental override?
}
map[this] = NodeCustomisationDirectoryImpl(this@NodeCustomisationDirectoryImpl).apply(block)
map[this] = lazy {
NodeCustomisationDirectoryImpl(this@NodeCustomisationDirectoryImpl).apply(block)
}
}

/**
* Gets a value from the map, if the value is lazy the lazy value will be initialized.
*/
private fun <T : Any> getValue(key: Any?): T? =
map[key]?.value as? T

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ actual open class NodeCustomisationDirectoryImpl actual constructor(
override val parent: NodeCustomisationDirectory?
) : MutableNodeCustomisationDirectory {

override fun <T : NodeCustomisation> put(key: KClass<T>, value: T) {
override fun <T : NodeCustomisation> put(key: KClass<T>, valueProvider: () -> T) {
// NO-OP
}

Expand All @@ -16,7 +16,7 @@ actual open class NodeCustomisationDirectoryImpl actual constructor(
override fun <T : NodeCustomisation> getRecursively(key: KClass<T>): T? =
null

override fun <T : Any> putSubDirectory(key: KClass<T>, value: NodeCustomisationDirectory) {
override fun <T : Any> putSubDirectory(key: KClass<T>, valueProvider: () -> NodeCustomisationDirectory) {
// NO-OP
}

Expand Down

0 comments on commit 92a0f06

Please sign in to comment.