Skip to content

Commit

Permalink
feat: swap requestIterator for [Symbol.asyncIterator]
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed Jul 4, 2024
1 parent a796104 commit ead067b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/storages/request_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ export interface IRequestList {
*
* The function resolves to `null` if there are no more requests to process.
*
* Unlike `fetchNextRequest()`, this function returns an `AsyncGenerator` that can be used in a `for await...of` loop.
* Can be used to iterate over the `RequestList` instance in a `for await .. of` loop.
* Provides an alternative for the repeated use of `fetchNextRequest`.
*/
requestIterator(): AsyncGenerator<Request | null>;
[Symbol.asyncIterator](): AsyncGenerator<Request>;

/**
* Reclaims request to the list if its processing failed.
Expand Down Expand Up @@ -686,7 +687,7 @@ export class RequestList implements IRequestList {
/**
* @inheritDoc
*/
async *requestIterator() {
async *[Symbol.asyncIterator]() {
while (true) {
const req = await this.fetchNextRequest();
if (!req) break;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/storages/sitemap_request_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class SitemapRequestList implements IRequestList {
/**
* @inheritDoc
*/
async *requestIterator() {
async *[Symbol.asyncIterator]() {
while ((!this.isSitemapFullyLoaded() && !this.abortLoading) || !(await this.isEmpty())) {
const request = await this.fetchNextRequest();
if (!request) break;
Expand Down
4 changes: 2 additions & 2 deletions test/core/request_list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('RequestList', () => {
expect(await newList.isEmpty()).toBe(true);
});

test('the `requestIterator` method works as expected', async () => {
test('`RequestList` is `for .. await` iterable', async () => {
const sources = [
'https://example.com/1',
'https://example.com/2',
Expand All @@ -151,7 +151,7 @@ describe('RequestList', () => {
];
const requestList = await RequestList.open(null, sources);

for await (const request of requestList.requestIterator()) {
for await (const request of requestList) {
expect(request?.url).toBe(sources.shift());
}
});
Expand Down
8 changes: 4 additions & 4 deletions test/core/sitemap_request_list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('SitemapRequestList', () => {
test('for..await syntax works with requestIterator', async () => {
const list = await SitemapRequestList.open({ sitemapUrls: [`${url}/sitemap-index.xml`] });

for await (const request of list.requestIterator()) {
for await (const request of list) {
await list.markRequestHandled(request);
}

Expand All @@ -214,7 +214,7 @@ describe('SitemapRequestList', () => {
await sleep(50); // Loads the first sub-sitemap, but not the second
controller.abort();

for await (const request of list.requestIterator()) {
for await (const request of list) {
await list.markRequestHandled(request);
}

Expand All @@ -229,7 +229,7 @@ describe('SitemapRequestList', () => {
timeoutMillis: 50, // Loads the first sub-sitemap, but not the second
});

for await (const request of list.requestIterator()) {
for await (const request of list) {
await list.markRequestHandled(request);
}

Expand All @@ -255,7 +255,7 @@ describe('SitemapRequestList', () => {
}

const newList = await SitemapRequestList.open(options);
for await (const request of newList.requestIterator()) {
for await (const request of newList) {
await newList.markRequestHandled(request);
}

Expand Down

0 comments on commit ead067b

Please sign in to comment.