From d9839f21ea670a20ffa946b3c80d1a1f37fa90f2 Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Mon, 3 Feb 2025 18:30:30 -0800 Subject: [PATCH] Add some async to the queries --- index.js | 78 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index bbd020f..5762c92 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,7 @@ const headers = { 'TE': 'trailers' }; -const skuIds = ["6614151","6616096","6617487","6614119","6614120","6616095","6616092","6616096","6614122","6616093","6616090","6616091","6615930","6615929","6615931"] +const skuIds = ["6614151","6616096","6617487","6614119","6614120","6616095","6616092","6614122","6616093","6616090","6616091","6615930","6615929","6615931"] const queries = skuIds.map(skuId => ({ skuId, @@ -151,42 +151,52 @@ async function fetchGraphQL(zip, store, query) { async function pollStore(zip, store) { console.log(`Starting poll for zip ${zip} and store ${store}...`); + const batchSize = 5; while (!found) { - for (const { skuId, query } of queries) { - const data = await fetchGraphQL(zip, store, query); - if ( - data && - data.data && - data.data.productBySkuId && - data.data.productBySkuId.fulfillmentOptions && - data.data.productBySkuId.fulfillmentOptions.buttonStates && - data.data.productBySkuId.fulfillmentOptions.buttonStates.length > 0 - ) { - const buttonState = data.data.productBySkuId.fulfillmentOptions.buttonStates[0].buttonState; - console.log(`skuId: ${skuId} Store: ${store} Ship to: ${zip} -> buttonState: ${buttonState}`); - if (buttonState !== "SOLD_OUT") { - found = true; - const productUrl = getProductUrl(skuId) - console.log(productUrl) - winningResult = { skuId, zip, store, buttonState, data }; - const openPromise = open(productUrl); - const addToCartPromise = addToCart(productUrl); - await open('./alarm.wav'); - await addToCartPromise - return winningResult; + const queryBatches = []; + for (let i = 0; i < queries.length; i += batchSize) { + queryBatches.push(queries.slice(i, i + batchSize)); + } + + for (const batch of queryBatches) { + const fetchPromises = batch.map(({ skuId, query }) => fetchGraphQL(zip, store, query).then(data => ({ skuId, data }))); + const results = await Promise.all(fetchPromises); + + for (const { skuId, data } of results) { + if ( + data && + data.data && + data.data.productBySkuId && + data.data.productBySkuId.fulfillmentOptions && + data.data.productBySkuId.fulfillmentOptions.buttonStates && + data.data.productBySkuId.fulfillmentOptions.buttonStates.length > 0 + ) { + const buttonState = data.data.productBySkuId.fulfillmentOptions.buttonStates[0].buttonState; + console.log(`skuId: ${skuId} Store: ${store} Ship to: ${zip} -> buttonState: ${buttonState}`); + if (buttonState !== "SOLD_OUT") { + found = true; + const productUrl = getProductUrl(skuId); + console.log(productUrl); + winningResult = { skuId, zip, store, buttonState, data }; + const openPromise = open(productUrl); + const addToCartPromise = addToCart(productUrl); + await open('./alarm.wav'); + await addToCartPromise; + return winningResult; + } + } else { + console.log(`Incomplete data for zip ${zip}, store ${store}.`); + } + lines++; + if (lines > 30) { + lines = 0; + process.stdout.write('\x1B[3J\x1B[2J\x1B[H'); + } + await new Promise(resolve => setTimeout(resolve, 500)); } - } else { - console.log(`Incomplete data for zip ${zip}, store ${store}.`); - } - lines++ - if(lines > 30) { - lines = 0; - process.stdout.write('\x1B[3J\x1B[2J\x1B[H'); - } - await new Promise(resolve => setTimeout(resolve, 500)); } - } - return null; +} +return null; } async function runAllPollers() {