Skip to content

Commit 9722053

Browse files
committed
fix(programs): filter inflight for displayed programs
1 parent 252b1f2 commit 9722053

1 file changed

Lines changed: 35 additions & 18 deletions

File tree

projects/social_platform/src/app/api/program/facades/program-shell-info.service.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ import { HttpParams } from "@angular/common/http";
1919
import { ParticipatingProgramUseCase } from "../use-cases/participating-program.use-case";
2020
import { Result, ok } from "@domain/shared/result.type";
2121
import { ApiPagination } from "@domain/other/api-pagination.model";
22-
import { GetProgramsUseCase } from "../use-cases/get-programs.use-case";
2322

2423
const PROGRAM_CACHE_KEY = "programs";
25-
const PROGRAM_ACTUAL_CACHE_KEY = "programs:actual";
2624
const CACHE_VERSION = 1;
2725
const CACHE_TTL = 24 * 60 * 60 * 1000; // 24h
2826

@@ -31,7 +29,6 @@ const CACHE_TTL = 24 * 60 * 60 * 1000; // 24h
3129
providedIn: "root",
3230
})
3331
export class ProgramShellInfoService {
34-
private readonly getProgramsUseCase = inject(GetProgramsUseCase);
3532
private readonly participatingProgramUseCase = inject(ParticipatingProgramUseCase);
3633

3734
private readonly programs$ = signal<AsyncState<Program[], unknown>>(initial());
@@ -50,12 +47,15 @@ export class ProgramShellInfoService {
5047
.slice(0, 3);
5148
});
5249

53-
private programsInflight: Observable<Result<ApiPagination<Program>, unknown>> | null = null;
50+
private allProgramsInflight: Observable<Result<ApiPagination<Program>, unknown>> | null = null;
51+
private filteredInflight: Observable<Result<ApiPagination<Program>, unknown>> | null = null;
5452

5553
ensureProgramsLoaded(filter?: HttpParams): Observable<Result<ApiPagination<Program>, unknown>> {
56-
if (filter && filter.keys().length > 0) {
57-
if (this.programsInflight) return this.programsInflight;
58-
return this.fetchPrograms(filter);
54+
const isFiltered = !!filter && filter.keys().length > 0;
55+
56+
if (isFiltered) {
57+
if (this.filteredInflight) return this.filteredInflight;
58+
return this.fetchFilteredPrograms(filter);
5959
}
6060

6161
if (isSuccess(this.programs$())) {
@@ -74,8 +74,7 @@ export class ProgramShellInfoService {
7474

7575
if (cached) {
7676
this.programs$.set(success(cached));
77-
78-
this.fetchPrograms();
77+
this.fetchAllPrograms();
7978
return of(
8079
ok<ApiPagination<Program>>({
8180
count: cached.length,
@@ -86,27 +85,28 @@ export class ProgramShellInfoService {
8685
);
8786
}
8887

89-
return this.fetchPrograms();
88+
return this.fetchAllPrograms();
9089
}
9190

9291
refreshPrograms(): void {
93-
this.programsInflight = null;
94-
this.fetchPrograms();
92+
this.allProgramsInflight = null;
93+
this.fetchAllPrograms();
9594
}
9695

9796
invalidatePrograms(): void {
98-
this.programsInflight = null;
97+
this.allProgramsInflight = null;
98+
this.filteredInflight = null;
9999
this.programs$.set(initial());
100100
}
101101

102-
private fetchPrograms(filter?: HttpParams): Observable<Result<ApiPagination<Program>, unknown>> {
103-
if (this.programsInflight) return this.programsInflight;
102+
private fetchAllPrograms(): Observable<Result<ApiPagination<Program>, unknown>> {
103+
if (this.allProgramsInflight) return this.allProgramsInflight;
104104

105105
const state = this.programs$();
106106
const prev = isSuccess(state) ? state.data : undefined;
107107
this.programs$.set(loading(prev));
108108

109-
const source$ = this.participatingProgramUseCase.execute(filter);
109+
const source$ = this.participatingProgramUseCase.execute();
110110

111111
const request$ = source$.pipe(
112112
tap(result => {
@@ -116,16 +116,33 @@ export class ProgramShellInfoService {
116116
writeCache(PROGRAM_CACHE_KEY, CACHE_VERSION, result.value.results);
117117
} catch {}
118118
}),
119-
finalize(() => (this.programsInflight = null)),
119+
finalize(() => (this.allProgramsInflight = null)),
120120
shareReplay({ bufferSize: 1, refCount: true }),
121121
);
122122

123-
this.programsInflight = request$;
123+
this.allProgramsInflight = request$;
124124

125125
request$.subscribe({
126126
error: error => this.programs$.set(failure(error, prev)),
127127
});
128128

129129
return request$;
130130
}
131+
132+
private fetchFilteredPrograms(
133+
filter: HttpParams,
134+
): Observable<Result<ApiPagination<Program>, unknown>> {
135+
const source$ = this.participatingProgramUseCase.execute(filter);
136+
137+
const request$ = source$.pipe(
138+
finalize(() => (this.filteredInflight = null)),
139+
shareReplay({ bufferSize: 1, refCount: true }),
140+
);
141+
142+
this.filteredInflight = request$;
143+
144+
request$.subscribe();
145+
146+
return request$;
147+
}
131148
}

0 commit comments

Comments
 (0)