Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions apps/rxjs/11-high-order-operator-bug/src/app/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inject, Injectable } from '@angular/core';
import { merge, Observable, of } from 'rxjs';
import { catchError, forkJoin, map, Observable, of, tap } from 'rxjs';
import { LocalDBService, TopicType } from './localDB.service';

@Injectable({ providedIn: 'root' })
Expand All @@ -10,10 +10,38 @@ export class AppService {

deleteOldTopics(type: TopicType): Observable<boolean> {
const infoByType = this.dbService.searchByType(type);
return infoByType.length > 0
? infoByType
.map((t) => this.dbService.deleteOneTopic(t.id))
.reduce((acc, curr) => merge(acc, curr), of(true))
: of(true);

if (!infoByType?.length) {
return of(true);
}

const requests$ = infoByType.map((t) =>
this.dbService.deleteOneTopic(t.id).pipe(
tap((result) => {
if (result) {
console.log(`${type} with id ${t.id} deleted successfully.`);
} else {
console.error(`Failed to delete ${type} with id ${t.id}.`);
}
}),
catchError((error) => {
console.error(
`There was an error while deleting ${type} with id ${t.id}.`,
error,
);
return of(false);
}),
),
);

return forkJoin(requests$).pipe(
tap((results) =>
console.log(
`All deletion requests for ${type} completed with results:`,
results,
),
),
map((results) => results.every((r) => r)),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/member-ordering */
import { randomError } from '@angular-challenges/shared/utils';
import { computed, Injectable, signal } from '@angular/core';
import { of } from 'rxjs';
import { Observable, of, throwError } from 'rxjs';

export type TopicType = 'food' | 'book' | 'sport';

Expand Down Expand Up @@ -42,12 +42,12 @@ export class LocalDBService {
this.state.set({ infos: this.state().infos.filter((i) => i.id !== id) });
};

deleteOneTopic = (id: number) =>
deleteOneTopic = (id: number): Observable<boolean> =>
randomError({
success: () => {
this.deleteOne(id);
return of(true);
},
error: () => of(false),
error: () => throwError(() => new Error('Random error occurred')),
});
}
Loading