Skip to content

Commit

Permalink
Stores and Resolves Promises Once Data is Fetched
Browse files Browse the repository at this point in the history
  • Loading branch information
Step7750 committed Jan 31, 2017
1 parent e87eea3 commit 916fb24
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions float.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ let floatQueue = [];
let floatData = {};
let floatTimer;
let steamListingInfo = {};
let listingInfoPromises = [];

// retrieve g_rgListingInfo from page script
window.addEventListener('message', (e) => {
if (e.data.type == 'listingInfo') {
steamListingInfo = e.data.listingInfo;

// resolve listingInfoPromises
for (let promise of listingInfoPromises) promise(steamListingInfo);

listingInfoPromises = [];
}
});

const retrieveListingInfoFromPage = function(listingId) {
if (listingId != null && (listingId in steamListingInfo)) {
return Promise.resolve(steamListingInfo[listingId]);
return Promise.resolve(steamListingInfo);
}

// Reset, since this is a new page
steamListingInfo = {};

window.postMessage({
type: 'requestListingInfo'
}, '*');

return new Promise((resolve, reject) => {
setTimeout(() => {
if (Object.keys(steamListingInfo).length === 0) reject();
else resolve((listingId != null) ? steamListingInfo[listingId] : steamListingInfo);
}, 10);
listingInfoPromises.push(resolve);
});
};

Expand Down Expand Up @@ -120,10 +120,6 @@ const getAllFloats = function() {

floatQueue.push({ listingId: id, inspectLink: inspectLink });
}
},
() => {
// Try again
getAllFloats();
});
};

Expand Down Expand Up @@ -161,15 +157,16 @@ const getFloatButtonClicked = function(e) {
let id = row.id.replace('listing_', '');

retrieveListingInfoFromPage(id)
.then((listingData) => {
.then((steamListingData) => {
let listingData = steamListingData[id];

if (!listingData) return;

let inspectLink = listingData.asset.market_actions[0].link
.replace('%listingid%', id)
.replace('%assetid%', listingData.asset.id);

floatQueue.push({ listingId: id, inspectLink: inspectLink });
},
() => {
setFloatText(id, 'Failed to obtain listing info, please try again');
});
};

Expand Down

0 comments on commit 916fb24

Please sign in to comment.