From 0df9a2639fa262aa95dabcbe1cfb720fca79cb7e Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Fri, 19 Aug 2022 16:18:54 +0200 Subject: [PATCH] yield questions as we request them Signed-off-by: Emma Indal --- .../StackOverflowQuestionsCollatorFactory.ts | 65 ++++++++----------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts index 1bce4fee46..5803ef4b6b 100644 --- a/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts +++ b/plugins/stack-overflow-backend/src/search/StackOverflowQuestionsCollatorFactory.ts @@ -95,35 +95,6 @@ export class StackOverflowQuestionsCollatorFactory return Readable.from(this.execute()); } - async getQuestions() { - let hasMorePages = true; - let page = 1; - const items = []; - - const params = qs.stringify(this.requestParams, { - arrayFormat: 'comma', - addQueryPrefix: true, - }); - - const apiKeyParam = this.apiKey - ? `${params ? '&' : '?'}key=${this.apiKey}` - : ''; - - while (hasMorePages) { - const res = await fetch( - `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, - ); - - const data = await res.json(); - items.push(...data.items); - hasMorePages = data.has_more; - page = page + 1; - } - return { - items, - }; - } - async *execute(): AsyncGenerator { if (!this.baseUrl) { this.logger.debug( @@ -142,16 +113,34 @@ export class StackOverflowQuestionsCollatorFactory this.logger.error(`Caught ${e}`); } - const data = await this.getQuestions(); + const params = qs.stringify(this.requestParams, { + arrayFormat: 'comma', + addQueryPrefix: true, + }); - for (const question of data.items) { - yield { - title: question.title, - location: question.link, - text: question.owner.display_name, - tags: question.tags, - answers: question.answer_count, - }; + const apiKeyParam = this.apiKey + ? `${params ? '&' : '?'}key=${this.apiKey}` + : ''; + + let hasMorePages = true; + let page = 1; + while (hasMorePages) { + const res = await fetch( + `${this.baseUrl}/questions${params}${apiKeyParam}&page=${page}`, + ); + + const data = await res.json(); + for (const question of data.items) { + yield { + title: question.title, + location: question.link, + text: question.owner.display_name, + tags: question.tags, + answers: question.answer_count, + }; + } + hasMorePages = data.has_more; + page = page + 1; } } }