@@ -2,7 +2,7 @@ import { Component, Inject, Input, OnDestroy, OnInit } from '@angular/core';
22import { Router } from '@angular/router' ;
33
44import { BehaviorSubject , Observable } from 'rxjs' ;
5- import { map , tap } from 'rxjs/operators' ;
5+ import { map , filter } from 'rxjs/operators' ;
66
77import { SearchService } from '../../../core/shared/search/search.service' ;
88import { RemoteData } from '../../../core/data/remote-data' ;
@@ -62,9 +62,16 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
6262 searchLink : string ;
6363
6464 /**
65- * Filters for which visibility has been computed
65+ * Keeps track of the filters computed for each configuration during the current rendering cycle
66+ * This array stores objects with configuration identifier and number of computed filters
6667 */
67- filtersWithComputedVisibility = 0 ;
68+ private currentFiltersComputed = [ ] ;
69+
70+ /**
71+ * Stores the final count of computed filters for each configuration
72+ * Used to determine when all filters for a configuration have been processed
73+ */
74+ private finalFiltersComputed = [ ] ;
6875
6976 subs = [ ] ;
7077 defaultFilterCount : number ;
@@ -90,7 +97,6 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
9097 ngOnInit ( ) : void {
9198 this . router . events . subscribe ( ( ) => {
9299 this . clearParams = this . searchConfigService . getCurrentFrontendFilters ( ) . pipe (
93- tap ( ( ) => this . filtersWithComputedVisibility = 0 ) ,
94100 map ( ( filters ) => {
95101 Object . keys ( filters ) . forEach ( ( f ) => filters [ f ] = null ) ;
96102 return filters ;
@@ -127,7 +133,122 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
127133
128134 countFiltersWithComputedVisibility ( computed : boolean ) {
129135 if ( computed ) {
130- this . filtersWithComputedVisibility += 1 ;
136+ this . filters . pipe (
137+ // Get filter data and check if we need to increment the counter
138+ map ( filtersData => {
139+ if ( filtersData && filtersData . hasSucceeded && filtersData . payload ) {
140+ const totalFilters = filtersData . payload . length ;
141+ const currentComputed = this . getCurrentFiltersComputed ( this . currentConfiguration ) ;
142+
143+ // If we've already computed all filters for this configuration
144+ if ( currentComputed >= totalFilters ) {
145+ // Register in finalFiltersComputed if not already registered
146+ if ( ! this . findConfigInFinalFilters ( this . currentConfiguration ) ) {
147+ this . updateFinalFiltersComputed ( this . currentConfiguration , totalFilters ) ;
148+ }
149+ return { shouldIncrement : false } ;
150+ }
151+
152+ // We haven't reached the total yet, proceed with increment
153+ return {
154+ shouldIncrement : true ,
155+ totalFilters
156+ } ;
157+ }
158+ return { shouldIncrement : false } ;
159+ } ) ,
160+ // Only continue if we need to increment the counter
161+ filter ( result => result . shouldIncrement ) ,
162+ // Increment the counter for the current configuration
163+ map ( result => {
164+ const filterConfig = this . findConfigInCurrentFilters ( this . currentConfiguration ) ;
165+
166+ if ( filterConfig ) {
167+ // Update existing counter
168+ filterConfig . filtersComputed += 1 ;
169+ } else {
170+ // Create new counter entry
171+ this . currentFiltersComputed . push ( {
172+ configuration : this . currentConfiguration ,
173+ filtersComputed : 1
174+ } ) ;
175+ }
176+
177+ // Pass along the total and updated count
178+ return {
179+ totalFilters : result . totalFilters ,
180+ currentComputed : this . getCurrentFiltersComputed ( this . currentConfiguration )
181+ } ;
182+ } ) ,
183+ // Check if we've reached the total after incrementing
184+ map ( result => {
185+ if ( result . currentComputed === result . totalFilters ) {
186+ // If we've reached the total, update final filters count
187+ this . updateFinalFiltersComputed ( this . currentConfiguration , result . currentComputed ) ;
188+ }
189+ return result ;
190+ } )
191+ ) . subscribe ( ) . unsubscribe ( ) ; // Execute the pipeline and immediately unsubscribe
131192 }
132193 }
194+
195+ /**
196+ * Finds a configuration entry in the currentFiltersComputed array
197+ * @param configuration The configuration identifier to search for
198+ * @returns The filter configuration object if found, otherwise undefined
199+ */
200+ private findConfigInCurrentFilters ( configuration : string ) {
201+ return this . currentFiltersComputed . find (
202+ ( configFilter ) => configFilter . configuration === configuration
203+ ) ;
204+ }
205+
206+ /**
207+ * Finds a configuration entry in the finalFiltersComputed array
208+ * @param configuration The configuration identifier to search for
209+ * @returns The filter configuration object if found, otherwise undefined
210+ */
211+ private findConfigInFinalFilters ( configuration : string ) {
212+ return this . finalFiltersComputed . find (
213+ ( configFilter ) => configFilter . configuration === configuration
214+ ) ;
215+ }
216+
217+ /**
218+ * Updates or adds a new entry in the finalFiltersComputed array
219+ * @param configuration The configuration identifier to update
220+ * @param count The number of computed filters to set for this configuration
221+ */
222+ private updateFinalFiltersComputed ( configuration : string , count : number ) {
223+ const filterConfig = this . findConfigInFinalFilters ( configuration ) ;
224+
225+ if ( filterConfig ) {
226+ filterConfig . filtersComputed = count ;
227+ } else {
228+ this . finalFiltersComputed . push ( {
229+ configuration,
230+ filtersComputed : count
231+ } ) ;
232+ }
233+ }
234+
235+ /**
236+ * Gets the current number of computed filters for a specific configuration
237+ * @param configuration The configuration identifier to get the count for
238+ * @returns The number of computed filters, or 0 if none found
239+ */
240+ private getCurrentFiltersComputed ( configuration : string ) {
241+ const configFilter = this . findConfigInCurrentFilters ( configuration ) ;
242+ return configFilter ?. filtersComputed || 0 ;
243+ }
244+
245+ /**
246+ * Gets the final number of computed filters for a specific configuration
247+ * @param configuration The configuration identifier to get the count for
248+ * @returns The number of computed filters in the final state, or 0 if none found
249+ */
250+ getFinalFiltersComputed ( configuration : string ) : number {
251+ const configFilter = this . findConfigInFinalFilters ( configuration ) ;
252+ return configFilter ?. filtersComputed || 0 ;
253+ }
133254}
0 commit comments