Skip to content

Instantly share code, notes, and snippets.

@TaylorAckley
Created August 20, 2021 16:01
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 TaylorAckley/d767af82640c0812427645f3ae2147eb to your computer and use it in GitHub Desktop.
Save TaylorAckley/d767af82640c0812427645f3ae2147eb to your computer and use it in GitHub Desktop.
Search with Curry
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