Skip to content

Commit ebbdfe3

Browse files
authored
perf: browser abstraction (jef#68) (jef#81)
Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
1 parent 14b1e4c commit ebbdfe3

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ import {Stores} from './store/model';
33
import {Logger} from './logger';
44
import {sendNotification} from './notification';
55
import {lookup} from './store';
6+
import puppeteer from 'puppeteer';
67

78
/**
89
* Starts the bot.
910
*/
1011
async function main() {
1112
const results = [];
13+
const browser = await puppeteer.launch();
14+
1215
for (const store of Stores) {
1316
Logger.debug(store.links);
14-
results.push(lookup(store));
17+
results.push(lookup(browser, store));
1518
}
1619

1720
await Promise.all(results);
21+
await browser.close();
1822

1923
Logger.info('↗ trying stores again');
2024
setTimeout(main, Config.rateLimitTimeout);

src/store/lookup.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ function filterBrand(brand: string) {
2424
* a `Store`. It's important that we ignore `no-await-in-loop` here
2525
* because we don't want to get rate limited within the same store.
2626
*
27+
* @param browser Current browser in use.
2728
* @param store Vendor of graphics cards.
2829
*/
29-
export async function lookup(store: Store) {
30+
export async function lookup(browser: puppeteer.Browser, store: Store) {
3031
/* eslint-disable no-await-in-loop */
3132
for (const link of store.links) {
3233
if (!filterBrand(link.brand)) {
3334
continue;
3435
}
3536

36-
const browser = await puppeteer.launch();
3737
const page = await browser.newPage();
3838
page.setDefaultNavigationTimeout(Config.page.navigationTimeout);
3939
await page.setUserAgent(Config.page.userAgent);
@@ -48,7 +48,7 @@ export async function lookup(store: Store) {
4848
await page.goto(link.url, {waitUntil: 'networkidle0'});
4949
} catch {
5050
Logger.error(`✖ [${store.name}] ${graphicsCard} skipping; timed out`);
51-
await browser.close();
51+
await page.close();
5252
continue;
5353
}
5454

@@ -79,7 +79,7 @@ export async function lookup(store: Store) {
7979
sendNotification(givenUrl);
8080
}
8181

82-
await browser.close();
82+
await page.close();
8383
}
8484
/* eslint-enable no-await-in-loop */
8585
}

0 commit comments

Comments
 (0)