|
| 1 | +import { Observable } from 'rxjs/Observable' |
| 2 | +import { Subject } from 'rxjs/Subject' |
| 3 | + |
| 4 | +import 'rxjs/add/observable/of' |
| 5 | +import 'rxjs/add/observable/fromPromise' |
| 6 | + |
| 7 | +import 'rxjs/add/operator/do' |
| 8 | +import 'rxjs/add/operator/catch' |
| 9 | +import 'rxjs/add/operator/switchMap' |
| 10 | +import 'rxjs/add/operator/debounceTime' |
| 11 | +import 'rxjs/add/operator/distinctUntilChanged' |
| 12 | +import 'rxjs/add/operator/map' |
| 13 | +import 'rxjs/add/operator/filter' |
| 14 | + |
| 15 | +import R from 'ramda' |
| 16 | +import fetch from 'isomorphic-fetch' |
| 17 | + |
| 18 | +export const fuck = 1 |
| 19 | +const QUERY_REPOS = |
| 20 | + 'https://api.github.com/search/repositories?sort=stars&order=desc&q=' |
| 21 | + |
| 22 | +const getReposPromise = query => { |
| 23 | + const url = `${QUERY_REPOS}${query}` |
| 24 | + return fetch(url).then(res => res.json()) |
| 25 | +} |
| 26 | + |
| 27 | +const getRepos = query => { |
| 28 | + const promise = getReposPromise(query) |
| 29 | + return Observable.fromPromise(promise) |
| 30 | +} |
| 31 | + |
| 32 | +const getGithubRepos = R.curry(getRepos) |
| 33 | + |
| 34 | +const isEmptyValue = R.compose(R.isEmpty, R.trim) |
| 35 | +const isNotEmptyValue = R.compose(R.not, isEmptyValue) |
| 36 | + |
| 37 | +export class SearchService { |
| 38 | + constructor() { |
| 39 | + this.searchTerm = new Subject() |
| 40 | + // this.doSearch = R.curry(this.githubQuery) |
| 41 | + } |
| 42 | + |
| 43 | + search(term) { |
| 44 | + this.searchTerm.next(term) |
| 45 | + } |
| 46 | + |
| 47 | + get() { |
| 48 | + return ( |
| 49 | + this.searchTerm |
| 50 | + .debounceTime(400) |
| 51 | + .filter(isNotEmptyValue) |
| 52 | + // .do(value => console.log('after:', value)) |
| 53 | + .distinctUntilChanged() |
| 54 | + .switchMap(getGithubRepos) |
| 55 | + .catch(error => { |
| 56 | + console.error(error) |
| 57 | + return Observable.of([]) |
| 58 | + }) |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + emptyInput() { |
| 63 | + return this.searchTerm.debounceTime(600).filter(isEmptyValue) |
| 64 | + } |
| 65 | +} |
0 commit comments