shared(nearby): parallelize fetchNearbyData with structured concurrency (async) while keeping DI and API unchanged

This commit is contained in:
2025-09-14 22:08:43 +02:00
parent 41270dacac
commit 4c87ef9490

View File

@@ -1,16 +1,14 @@
package app.klottr.nearby
import kotlinx.coroutines.async // För parallella jobb
import kotlinx.coroutines.awaitAll // För att vänta in flera Deferred
import kotlinx.coroutines.coroutineScope // För strukturerad samtidighet
import app.klottr.net.NoteDTO // Datamodeller (finns i :shared)
import app.klottr.net.WikipediaPage //
import app.klottr.net.DigitaltMuseumItem //
import app.klottr.net.FornsokItem //
import app.klottr.net.NoteDTO
import app.klottr.net.WikipediaPage
import app.klottr.net.DigitaltMuseumItem
import app.klottr.net.FornsokItem
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
/** Resultatcontainer för “Närliggande” (alla lager). */
data class NearbyData( //
data class NearbyData(
val klotter: List<NoteDTO>,
val wiki: List<WikipediaPage>,
val dm: List<DigitaltMuseumItem>,
@@ -19,50 +17,42 @@ data class NearbyData( //
/**
* Plattformsneutral fetcher:
* - Tar in fetch-funktioner som parametrar (Dependency Injection) i stället för att importera klienter.
* - Inga beroenden till :app eller plattforms-specifika källträd.
* - DI: tar in fetch-funktioner som parametrar i stället för att importera klienter.
* - Kör parallellt med structured concurrency (coroutineScope + async).
*/
suspend fun fetchNearbyData( // Samma namn – enklare när vi kopplar in från :app
lat: Double, //
lon: Double, //
showKlotter: Boolean, //
showWiki: Boolean, //
showDm: Boolean, //
showFornsok: Boolean, //
wikiRadius: Int, //
dmRadius: Int, //
fornsokRadius: Int, //
suspend fun fetchNearbyData(
lat: Double,
lon: Double,
showKlotter: Boolean,
showWiki: Boolean,
showDm: Boolean,
showFornsok: Boolean,
wikiRadius: Int,
dmRadius: Int,
fornsokRadius: Int,
// ---- injicerade fetchers (plattformen tillhandahåller dessa) ----
klotterFetcher: suspend (lat: Double, lon: Double) -> List<NoteDTO>, //
wikiFetcher: suspend (lat: Double, lon: Double, radius: Int) -> List<WikipediaPage>, //
dmFetcher: suspend (lat: Double, lon: Double, radius: Int) -> List<DigitaltMuseumItem>, //
fornFetcher: suspend (lat: Double, lon: Double, radius: Int) -> List<FornsokItem> //
): NearbyData {
return coroutineScope { // Kör allt inom strukturerad samtidighet
// Starta varje källa parallellt, men respektera show*-flaggorna.
val klDeferred = async { //
klotterFetcher: suspend (lat: Double, lon: Double) -> List<NoteDTO>,
wikiFetcher: suspend (lat: Double, lon: Double, radius: Int) -> List<WikipediaPage>,
dmFetcher: suspend (lat: Double, lon: Double, radius: Int) -> List<DigitaltMuseumItem>,
fornFetcher: suspend (lat: Double, lon: Double, radius: Int) -> List<FornsokItem>
): NearbyData = coroutineScope {
val klDef = async {
if (showKlotter) runCatching { klotterFetcher(lat, lon) }.getOrElse { emptyList() } else emptyList()
}
val wiDeferred = async { //
val wiDef = async {
if (showWiki) runCatching { wikiFetcher(lat, lon, wikiRadius) }.getOrElse { emptyList() } else emptyList()
}
val dmDeferred = async { //
val dmDef = async {
if (showDm) runCatching { dmFetcher(lat, lon, dmRadius) }.getOrElse { emptyList() } else emptyList()
}
val foDeferred = async { //
val foDef = async {
if (showFornsok) runCatching { fornFetcher(lat, lon, fornsokRadius) }.getOrElse { emptyList() } else emptyList()
}
// Vänta in alla parallella jobb.
awaitAll(klDeferred, wiDeferred, dmDeferred, foDeferred) //
// Plocka ut resultaten.
val kl = klDeferred.getCompleted() //
val wi = wiDeferred.getCompleted() //
val dm = dmDeferred.getCompleted() //
val fo = foDeferred.getCompleted() //
NearbyData(klotter = kl, wiki = wi, dm = dm, forn = fo) // [OFÖR]* Samma returtyp, men nu parallellt
}
NearbyData(
klotter = klDef.await(),
wiki = wiDef.await(),
dm = dmDef.await(),
forn = foDef.await()
)
}