From b84ef99e2287016987939a9dc98d206db2fb8d38 Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Sat, 4 Dec 2021 14:56:57 +0100 Subject: [PATCH] Optimized and improved code that would be buggy once we support multiple private servers. --- .../stores/HuntOptimizerStore.kt | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/web/src/main/kotlin/world/phantasmal/web/huntOptimizer/stores/HuntOptimizerStore.kt b/web/src/main/kotlin/world/phantasmal/web/huntOptimizer/stores/HuntOptimizerStore.kt index 34af549c..c05fe1b1 100644 --- a/web/src/main/kotlin/world/phantasmal/web/huntOptimizer/stores/HuntOptimizerStore.kt +++ b/web/src/main/kotlin/world/phantasmal/web/huntOptimizer/stores/HuntOptimizerStore.kt @@ -7,6 +7,7 @@ import mu.KotlinLogging import world.phantasmal.core.JsObject import world.phantasmal.core.component1 import world.phantasmal.core.component2 +import world.phantasmal.core.disposable.Disposable import world.phantasmal.core.unsafe.UnsafeMap import world.phantasmal.core.unsafe.UnsafeSet import world.phantasmal.observable.cell.Cell @@ -51,6 +52,7 @@ class HuntOptimizerStore( private val _huntableItems = mutableListCell() private val _wantedItems = mutableListCell { arrayOf(it.amount) } private val _optimizationResult = mutableCell(OptimizationResultModel(emptyList(), emptyList())) + private var wantedItemsPersistenceObserver: Disposable? = null val huntableItems: ListCell by lazy { observe(uiStore.server) { server -> @@ -73,20 +75,19 @@ class HuntOptimizerStore( _wantedItems } - val optimizationResult: Cell = _optimizationResult - - init { - observe(wantedItems) { - scope.launch(Dispatchers.Default) { - wantedItemPersister.persistWantedItems(it, uiStore.server.value) - } - } - + val optimizationResult: Cell by lazy { observe(wantedItems, huntMethodStore.methods) { wantedItems, huntMethods -> scope.launch(Dispatchers.Default) { _optimizationResult.value = optimize(wantedItems, huntMethods) } } + + _optimizationResult + } + + override fun dispose() { + wantedItemsPersistenceObserver?.dispose() + super.dispose() } fun addWantedItem(itemType: ItemType) { @@ -104,7 +105,22 @@ class HuntOptimizerStore( val wantedItems = wantedItemPersister.loadWantedItems(server) withContext(Dispatchers.Main) { + // Clear the previous wanted items observer, because we don't want it to persist + // changes using the previous server as key. This way we also avoid an unnecessary + // persist call right after the loading and replacing the wanted items. + wantedItemsPersistenceObserver?.dispose() + wantedItemsPersistenceObserver = null + _wantedItems.replaceAll(wantedItems) + + // Wanted items are loaded, start observing them and persist whenever they change. + wantedItemsPersistenceObserver = _wantedItems.observe { + val items = it.value + + scope.launch(Dispatchers.Main) { + wantedItemPersister.persistWantedItems(items, server) + } + } } } }