Last active
July 11, 2018 20:19
-
-
Save amrfarid140/af81b31de846d574bd21d0a97a06a9ff to your computer and use it in GitHub Desktop.
Use case to manage viewing Facebook repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable, of } from "rxjs"; | |
import { flatMap } from "rxjs/operators"; | |
import { ApiService } from "../data/ApiService"; | |
import { Dao } from "../data/Dao"; | |
import { IRepositroy } from "../data/models/Repository"; | |
export class ViewRepositoriesUseCase { | |
private readonly apiService: ApiService; | |
private readonly dataDao: Dao; | |
constructor(apiSerivce: ApiService, dataDao: Dao) { | |
this.apiService = apiSerivce; | |
this.dataDao = dataDao; | |
} | |
public observeItems(): Observable<IRepositroy[]> { | |
return this.dataDao.getItems().pipe( | |
flatMap(items => { | |
if (items.length === 0) { | |
return this.fetchMore(); | |
} | |
return of(items); | |
}) | |
); | |
} | |
public fetchMore(): Observable<IRepositroy[]> { | |
return this.apiService.getRepos().pipe( | |
flatMap(items => { | |
return this.dataDao.addItems(items); | |
}) | |
); | |
} | |
public async clearItems() { | |
await this.dataDao.clearTable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment