Skip to content

Commit

Permalink
perf: browser abstraction (#68) (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Jef LeCompte <jeffreylec@gmail.com>
  • Loading branch information
jef committed Sep 19, 2020
1 parent 14b1e4c commit ebbdfe3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Expand Up @@ -3,18 +3,22 @@ import {Stores} from './store/model';
import {Logger} from './logger';
import {sendNotification} from './notification';
import {lookup} from './store';
import puppeteer from 'puppeteer';

/**
* Starts the bot.
*/
async function main() {
const results = [];
const browser = await puppeteer.launch();

for (const store of Stores) {
Logger.debug(store.links);
results.push(lookup(store));
results.push(lookup(browser, store));
}

await Promise.all(results);
await browser.close();

Logger.info('↗ trying stores again');
setTimeout(main, Config.rateLimitTimeout);
Expand Down
8 changes: 4 additions & 4 deletions src/store/lookup.ts
Expand Up @@ -24,16 +24,16 @@ function filterBrand(brand: string) {
* a `Store`. It's important that we ignore `no-await-in-loop` here
* because we don't want to get rate limited within the same store.
*
* @param browser Current browser in use.
* @param store Vendor of graphics cards.
*/
export async function lookup(store: Store) {
export async function lookup(browser: puppeteer.Browser, store: Store) {
/* eslint-disable no-await-in-loop */
for (const link of store.links) {
if (!filterBrand(link.brand)) {
continue;
}

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

Expand Down Expand Up @@ -79,7 +79,7 @@ export async function lookup(store: Store) {
sendNotification(givenUrl);
}

await browser.close();
await page.close();
}
/* eslint-enable no-await-in-loop */
}

0 comments on commit ebbdfe3

Please sign in to comment.