11import { inject , Injectable } from '@angular/core' ;
2- import { merge , Observable , of } from 'rxjs' ;
3- import { LocalDBService , TopicType } from './localDB.service' ;
2+ import { forkJoin , map , Observable , of } from 'rxjs' ;
3+ import { Info , LocalDBService , TopicType } from './localDB.service' ;
44
55@Injectable ( { providedIn : 'root' } )
66export class AppService {
@@ -9,11 +9,16 @@ export class AppService {
99 getAllInfo = this . dbService . infos ;
1010
1111 deleteOldTopics ( type : TopicType ) : Observable < boolean > {
12+ const deleteTopics = ( infoByType : Info [ ] ) => {
13+ const results$ = infoByType . map ( ( t ) =>
14+ this . dbService . deleteOneTopic ( t . id ) ,
15+ ) ;
16+ return forkJoin ( results$ ) . pipe (
17+ map ( ( results ) => results . every ( ( value ) => value === true ) ) ,
18+ ) ;
19+ } ;
20+
1221 const infoByType = this . dbService . searchByType ( type ) ;
13- return infoByType . length > 0
14- ? infoByType
15- . map ( ( t ) => this . dbService . deleteOneTopic ( t . id ) )
16- . reduce ( ( acc , curr ) => merge ( acc , curr ) , of ( true ) )
17- : of ( true ) ;
22+ return infoByType . length > 0 ? deleteTopics ( infoByType ) : of ( true ) ;
1823 }
1924}
0 commit comments