Skip to content

Instantly share code, notes, and snippets.

@amrfarid140
Last active July 11, 2018 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amrfarid140/af81b31de846d574bd21d0a97a06a9ff to your computer and use it in GitHub Desktop.
Save amrfarid140/af81b31de846d574bd21d0a97a06a9ff to your computer and use it in GitHub Desktop.
Use case to manage viewing Facebook repositories
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