@@ -12,6 +12,7 @@ import com.intellij.lang.Language
1212import com.intellij.openapi.actionSystem.AnActionEvent
1313import com.intellij.openapi.actionSystem.DataContext
1414import com.intellij.openapi.application.EDT
15+ import com.intellij.openapi.options.advanced.AdvancedSettings
1516import com.intellij.openapi.project.DumbService
1617import com.intellij.openapi.project.Project
1718import com.intellij.openapi.util.Disposer
@@ -190,25 +191,43 @@ class SeTabVm(
190191
191192private const val ESSENTIALS_WAITING_TIMEOUT : Long = 2000
192193private const val ESSENTIALS_THROTTLE_DELAY : Long = 100
194+ private const val ESSENTIALS_ENOUGH_COUNT : Int = 15
193195
194196private fun Flow<SeResultEvent>.throttleUntilEssentialsArrive (essentialProviderIds : Set <SeProviderId >): Flow <ThrottledItems <SeResultEvent >> {
195- val nonArrivedEssentialProviders = essentialProviderIds.toMutableSet ()
197+ val essentialProvidersCounts = essentialProviderIds.associateWith { 0 }.toMutableMap ()
196198
197199 SeLog .log(SeLog .THROTTLING ) { " Will start throttle with essential providers: $essentialProviderIds " }
198200 return throttledWithAccumulation(ESSENTIALS_WAITING_TIMEOUT , { it !is SeResultEndEvent }) { event, size ->
199- val idToRemove = when (event) {
200- is SeResultAddedEvent -> event.itemData.providerId
201- is SeResultReplacedEvent -> event.newItemData.providerId
202- is SeResultEndEvent -> event.providerId
203- }
201+ val providerId = event.providerId()
204202
205- if (nonArrivedEssentialProviders.remove(idToRemove)) {
206- SeLog .log(SeLog .THROTTLING ) { " Arrived: $idToRemove " }
203+ when (event) {
204+ is SeResultEndEvent -> {
205+ if (essentialProvidersCounts.remove(providerId) != null ) {
206+ SeLog .log(SeLog .THROTTLING ) { " Ended: $providerId " }
207+ }
208+ }
209+ else -> {
210+ essentialProvidersCounts[providerId]?.let {
211+ SeLog .log(SeLog .THROTTLING ) { " Arrived: $providerId ($it )" }
212+ essentialProvidersCounts[providerId] = it + 1
213+ }
214+ }
207215 }
208216
209- return @throttledWithAccumulation if (nonArrivedEssentialProviders.isEmpty()) ESSENTIALS_THROTTLE_DELAY else null
217+ return @throttledWithAccumulation (
218+ if (essentialProvidersCounts.isEmpty()) 0
219+ else if (essentialProvidersCounts.values.all { it >= ESSENTIALS_ENOUGH_COUNT }) 0
220+ else if (essentialProvidersCounts.values.all { it > 0 }) ESSENTIALS_THROTTLE_DELAY
221+ else null
222+ )
210223 }
211224}
212225
226+ private fun SeResultEvent.providerId () = when (this ) {
227+ is SeResultAddedEvent -> itemData.providerId
228+ is SeResultReplacedEvent -> newItemData.providerId
229+ is SeResultEndEvent -> providerId
230+ }
231+
213232@ApiStatus.Internal
214233class SeSearchContext (val searchId : String , val tabId : String , val searchPattern : String , val resultsFlow : Flow <ThrottledItems <SeResultEvent >>)
0 commit comments