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

Align customisation implementation with 1.x implementation #575

Merged
Merged
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
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