Created
August 20, 2021 16:01
-
-
Save TaylorAckley/d767af82640c0812427645f3ae2147eb to your computer and use it in GitHub Desktop.
Search with Curry
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
export class GithubSearchCurryService extends BaseGithubSearch { | |
searchClient!: (term: string) => Observable<any>; | |
constructor(http: HttpClient) { | |
super(http) | |
} | |
/** | |
* Execute a search - calling this consecutively will page through results | |
*/ | |
search(term: string) { | |
if (!this.searchClient) { | |
this.searchClient = this.searchClientFactory(); | |
} | |
return this.searchClient(term); | |
} | |
/** | |
* Usage: | |
* ``` | |
* const search = this.searchClient(term); | |
* return search(); // returns an observable | |
* ``` | |
*/ | |
private searchClientFactory(): () => Observable<any> { | |
const PAGE_SIZE = 30; // default | |
let since = 0; // will hold the last userid for pagination | |
let term: string; | |
let page = 1; | |
return (_term?: string) => { | |
if(_term) term = _term; | |
return this.fetch<GitHubSearchResponse>(this.withSearchUri(term, since, page)) | |
.pipe(tap({ | |
next: (response: any) => { | |
since = response.items[response.items.length - 1]?.id ?? 0; | |
page++; | |
}})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment