IJPL-163530 Improve service instance container to allow multiple instances for different classloaders

GitOrigin-RevId: c29c9396fe50abbafa4e499ce7d6cc41209e4148
This commit is contained in:
Jakub Senohrabek
2024-10-08 12:08:49 +02:00
committed by intellij-monorepo-bot
parent 481fab7d13
commit a076bbdfba
2 changed files with 61 additions and 0 deletions

View File

@@ -122,6 +122,15 @@ f:com.intellij.ide.dnd.SmoothAutoScroller
*:com.intellij.ide.environment.EnvironmentService
- a:getEnvironmentValue(com.intellij.ide.environment.EnvironmentKey,java.lang.String,kotlin.coroutines.Continuation):java.lang.Object
- a:getEnvironmentValue(com.intellij.ide.environment.EnvironmentKey,kotlin.coroutines.Continuation):java.lang.Object
*f:com.intellij.ide.ui.LafFlowService
- *sf:Companion:com.intellij.ide.ui.LafFlowService$Companion
- <init>(kotlinx.coroutines.CoroutineScope):V
- f:customLafFlowState(java.lang.Object,kotlinx.coroutines.flow.SharingStarted,kotlin.jvm.functions.Function1):kotlinx.coroutines.flow.StateFlow
- bs:customLafFlowState$default(com.intellij.ide.ui.LafFlowService,java.lang.Object,kotlinx.coroutines.flow.SharingStarted,kotlin.jvm.functions.Function1,I,java.lang.Object):kotlinx.coroutines.flow.StateFlow
- sf:getInstance():com.intellij.ide.ui.LafFlowService
- f:getLafChangedFlow():kotlinx.coroutines.flow.SharedFlow
*f:com.intellij.ide.ui.LafFlowService$Companion
- f:getInstance():com.intellij.ide.ui.LafFlowService
a:com.intellij.ide.ui.LafManager
- *a:getInstalledThemes():kotlin.sequences.Sequence
*:com.intellij.ide.ui.PopupLocator

View File

@@ -0,0 +1,52 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide.ui
import com.intellij.openapi.application.Application
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.util.messages.MessageBus
import com.intellij.util.messages.SimpleMessageBusConnection
import com.intellij.util.messages.Topic
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.ProducerScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.*
import org.jetbrains.annotations.ApiStatus
@ApiStatus.Experimental
@Service(Service.Level.APP)
class LafFlowService(
private val scope: CoroutineScope,
) {
private val lafEventFlow = ApplicationManager.getApplication().lookAndFeelFlow(scope)
@Suppress("MemberVisibilityCanBePrivate")
val lafChangedFlow: SharedFlow<Unit> = lafEventFlow.onStart { emit(Unit) }.shareIn(scope, SharingStarted.Eagerly, replay = 1)
fun <T> customLafFlowState(
default: T,
sharingStarted: SharingStarted = SharingStarted.Eagerly,
mapper: (SharedFlow<Unit>) -> Flow<T>,
): StateFlow<T> {
return mapper(lafChangedFlow).stateIn(scope, sharingStarted, default)
}
companion object {
@JvmStatic
fun getInstance(): LafFlowService = service<LafFlowService>()
}
}
private fun Application.lookAndFeelFlow(scope: CoroutineScope): Flow<Unit> =
messageBus.flow(LafManagerListener.TOPIC, scope) { LafManagerListener { trySend(Unit) } }
private fun <L : Any, K> MessageBus.flow(
topic: Topic<L>,
parentScope: CoroutineScope,
listener: ProducerScope<K>.() -> L,
): Flow<K> = callbackFlow {
val connection: SimpleMessageBusConnection = connect(parentScope)
connection.subscribe(topic, listener())
awaitClose { connection.disconnect() }
}