@@ -15,7 +15,7 @@ import {
1515 BehaviorSubject ,
1616 Observable ,
1717} from 'rxjs' ;
18- import { map } from 'rxjs/operators' ;
18+ import { map , filter } from 'rxjs/operators' ;
1919
2020import {
2121 APP_CONFIG ,
@@ -82,9 +82,16 @@ export class SearchFiltersComponent implements OnInit {
8282 searchLink : string ;
8383
8484 /**
85- * Filters for which visibility has been computed
85+ * Keeps track of the filters computed for each configuration during the current rendering cycle
86+ * This array stores objects with configuration identifier and number of computed filters
8687 */
87- filtersWithComputedVisibility = 0 ;
88+ private currentFiltersComputed = [ ] ;
89+
90+ /**
91+ * Stores the final count of computed filters for each configuration
92+ * Used to determine when all filters for a configuration have been processed
93+ */
94+ private finalFiltersComputed = [ ] ;
8895
8996 subs = [ ] ;
9097 filterLabel = 'search' ;
@@ -136,7 +143,122 @@ export class SearchFiltersComponent implements OnInit {
136143
137144 countFiltersWithComputedVisibility ( computed : boolean ) {
138145 if ( computed ) {
139- this . filtersWithComputedVisibility += 1 ;
146+ this . filters . pipe (
147+ // Get filter data and check if we need to increment the counter
148+ map ( filtersData => {
149+ if ( filtersData && filtersData . hasSucceeded && filtersData . payload ) {
150+ const totalFilters = filtersData . payload . length ;
151+ const currentComputed = this . getCurrentFiltersComputed ( this . currentConfiguration ) ;
152+
153+ // If we've already computed all filters for this configuration
154+ if ( currentComputed >= totalFilters ) {
155+ // Register in finalFiltersComputed if not already registered
156+ if ( ! this . findConfigInFinalFilters ( this . currentConfiguration ) ) {
157+ this . updateFinalFiltersComputed ( this . currentConfiguration , totalFilters ) ;
158+ }
159+ return { shouldIncrement : false } ;
160+ }
161+
162+ // We haven't reached the total yet, proceed with increment
163+ return {
164+ shouldIncrement : true ,
165+ totalFilters
166+ } ;
167+ }
168+ return { shouldIncrement : false } ;
169+ } ) ,
170+ // Only continue if we need to increment the counter
171+ filter ( result => result . shouldIncrement ) ,
172+ // Increment the counter for the current configuration
173+ map ( result => {
174+ const filterConfig = this . findConfigInCurrentFilters ( this . currentConfiguration ) ;
175+
176+ if ( filterConfig ) {
177+ // Update existing counter
178+ filterConfig . filtersComputed += 1 ;
179+ } else {
180+ // Create new counter entry
181+ this . currentFiltersComputed . push ( {
182+ configuration : this . currentConfiguration ,
183+ filtersComputed : 1
184+ } ) ;
185+ }
186+
187+ // Pass along the total and updated count
188+ return {
189+ totalFilters : result . totalFilters ,
190+ currentComputed : this . getCurrentFiltersComputed ( this . currentConfiguration )
191+ } ;
192+ } ) ,
193+ // Check if we've reached the total after incrementing
194+ map ( result => {
195+ if ( result . currentComputed === result . totalFilters ) {
196+ // If we've reached the total, update final filters count
197+ this . updateFinalFiltersComputed ( this . currentConfiguration , result . currentComputed ) ;
198+ }
199+ return result ;
200+ } )
201+ ) . subscribe ( ) . unsubscribe ( ) ; // Execute the pipeline and immediately unsubscribe
140202 }
141203 }
204+
205+ /**
206+ * Finds a configuration entry in the currentFiltersComputed array
207+ * @param configuration The configuration identifier to search for
208+ * @returns The filter configuration object if found, otherwise undefined
209+ */
210+ private findConfigInCurrentFilters ( configuration : string ) {
211+ return this . currentFiltersComputed . find (
212+ ( configFilter ) => configFilter . configuration === configuration
213+ ) ;
214+ }
215+
216+ /**
217+ * Finds a configuration entry in the finalFiltersComputed array
218+ * @param configuration The configuration identifier to search for
219+ * @returns The filter configuration object if found, otherwise undefined
220+ */
221+ private findConfigInFinalFilters ( configuration : string ) {
222+ return this . finalFiltersComputed . find (
223+ ( configFilter ) => configFilter . configuration === configuration
224+ ) ;
225+ }
226+
227+ /**
228+ * Updates or adds a new entry in the finalFiltersComputed array
229+ * @param configuration The configuration identifier to update
230+ * @param count The number of computed filters to set for this configuration
231+ */
232+ private updateFinalFiltersComputed ( configuration : string , count : number ) {
233+ const filterConfig = this . findConfigInFinalFilters ( configuration ) ;
234+
235+ if ( filterConfig ) {
236+ filterConfig . filtersComputed = count ;
237+ } else {
238+ this . finalFiltersComputed . push ( {
239+ configuration,
240+ filtersComputed : count
241+ } ) ;
242+ }
243+ }
244+
245+ /**
246+ * Gets the current 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, or 0 if none found
249+ */
250+ private getCurrentFiltersComputed ( configuration : string ) {
251+ const configFilter = this . findConfigInCurrentFilters ( configuration ) ;
252+ return configFilter ?. filtersComputed || 0 ;
253+ }
254+
255+ /**
256+ * Gets the final number of computed filters for a specific configuration
257+ * @param configuration The configuration identifier to get the count for
258+ * @returns The number of computed filters in the final state, or 0 if none found
259+ */
260+ getFinalFiltersComputed ( configuration : string ) : number {
261+ const configFilter = this . findConfigInFinalFilters ( configuration ) ;
262+ return configFilter ?. filtersComputed || 0 ;
263+ }
142264}
0 commit comments