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