Skip to content

Commit

Permalink
Remove PackageSearch ApiClient coroutine scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
lamba92 committed Dec 8, 2023
1 parent fdd1ef6 commit bff0144
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ import com.jetbrains.packagesearch.plugin.utils.ApiSearchEntry
import com.jetbrains.packagesearch.plugin.utils.KtorDebugLogger
import com.jetbrains.packagesearch.plugin.utils.PackageSearchApiPackageCache
import com.jetbrains.packagesearch.plugin.utils.PackageSearchProjectService
import com.jetbrains.packagesearch.plugin.utils.timer
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logging
import java.util.concurrent.CompletableFuture
import kotlin.io.path.absolutePathString
import kotlin.io.path.div
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.future.future
Expand Down Expand Up @@ -87,10 +83,12 @@ class PackageSearchApplicationCachesService(private val coroutineScope: Coroutin
logger = KtorDebugLogger()
filter { it.attributes.getOrNull(PackageSearchApiClient.Attributes.Cache) == true }
}
},
scope = coroutineScope
}
)

val isOnlineFlow = devApiClient.isOnlineFlow()
.stateIn(coroutineScope, SharingStarted.WhileSubscribed(), true)

val apiPackageCache = PackageSearchApiPackageCache(
apiPackageCache = packagesRepository,
searchCache = searchesRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class PackageSearchProjectService(

IntelliJApplication.PackageSearchApplicationCachesService
.apiPackageCache
.isOnlineFlow
.isOnlineFlow()
.filter { it }
.onEach { restart() }
.launchIn(coroutineScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class PackageListViewModel(

private val isOnline
get() = IntelliJApplication.PackageSearchApplicationCachesService
.apiPackageCache
.isOnlineFlow

val isCompactFlow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ internal class TreeViewModel(
internal val treeState = TreeState(SelectableLazyListState(lazyListState))

val isOnline
get() = IntelliJApplication.PackageSearchApplicationCachesService
.apiPackageCache
.isOnlineFlow
get() = IntelliJApplication.PackageSearchApplicationCachesService.isOnlineFlow

fun expandAll() {
treeState.openNodes = tree.value.walkBreadthFirst().map { it.id }.toSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ class PackageSearchApiPackageCache(
.toList()
.associateBy { it.id }
val missingIds = ids - localDatabaseResults.keys
if (missingIds.isNotEmpty() && isOnlineFlow.value) {
val networkResults = apiCall(missingIds)
if (missingIds.isNotEmpty()) {
val networkResults = runCatching { apiCall(missingIds) }
.getOrDefault(emptyMap())
// TODO cache also miss in network to avoid pointless empty query
if (networkResults.isNotEmpty()) {
val packageEntries = networkResults.values.map { it.asCacheEntry() }
Expand Down

0 comments on commit bff0144

Please sign in to comment.