aboutsummaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-05-28 16:12:24 -0500
committerGitHub <noreply@github.com>2021-05-28 16:12:24 -0500
commit98c6c5bbb7959db1a69eb8bc0567f8d5d93ebb14 (patch)
treeece574521b8dfd6d3e6ab9847568c37c67ac0f07 /build
parent58c3467bf5f61d48d6071e0d99052a0a964e061e (diff)
downloadskyblock-api-98c6c5bbb7959db1a69eb8bc0567f8d5d93ebb14.tar.gz
skyblock-api-98c6c5bbb7959db1a69eb8bc0567f8d5d93ebb14.tar.bz2
skyblock-api-98c6c5bbb7959db1a69eb8bc0567f8d5d93ebb14.zip
Don't fetch leaderboards multiple times when they're already being fetched (#47)
Diffstat (limited to 'build')
-rw-r--r--build/database.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/build/database.js b/build/database.js
index 9cc625d..e4d7a9b 100644
--- a/build/database.js
+++ b/build/database.js
@@ -191,37 +191,59 @@ function isLeaderboardReversed(name) {
}
return false;
}
+/** A set of names of the raw leaderboards that are currently being fetched. This is used to make sure two leaderboads aren't fetched at the same time */
+const fetchingRawLeaderboardNames = new Set();
async function fetchMemberLeaderboardRaw(name) {
if (!client)
throw Error('Client isn\'t initialized yet');
if (exports.cachedRawLeaderboards.has(name))
return exports.cachedRawLeaderboards.get(name);
+ // if it's currently being fetched, check every 100ms until it's in cachedRawLeaderboards
+ if (fetchingRawLeaderboardNames.has(name)) {
+ while (true) {
+ await util_1.sleep(100);
+ if (exports.cachedRawLeaderboards.has(name))
+ return exports.cachedRawLeaderboards.get(name);
+ }
+ }
// typescript forces us to make a new variable and set it this way because it gives an error otherwise
const query = {};
query[`stats.${name}`] = { '$exists': true, '$ne': NaN };
const sortQuery = {};
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1;
+ fetchingRawLeaderboardNames.add(name);
const leaderboardRaw = await memberLeaderboardsCollection
.find(query)
.sort(sortQuery)
.limit(leaderboardMax)
.toArray();
+ fetchingRawLeaderboardNames.delete(name);
exports.cachedRawLeaderboards.set(name, leaderboardRaw);
return leaderboardRaw;
}
async function fetchProfileLeaderboardRaw(name) {
if (exports.cachedRawLeaderboards.has(name))
return exports.cachedRawLeaderboards.get(name);
+ // if it's currently being fetched, check every 100ms until it's in cachedRawLeaderboards
+ if (fetchingRawLeaderboardNames.has(name)) {
+ while (true) {
+ await util_1.sleep(100);
+ if (exports.cachedRawLeaderboards.has(name))
+ return exports.cachedRawLeaderboards.get(name);
+ }
+ }
// typescript forces us to make a new variable and set it this way because it gives an error otherwise
const query = {};
query[`stats.${name}`] = { '$exists': true, '$ne': NaN };
const sortQuery = {};
sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1;
+ fetchingRawLeaderboardNames.add(name);
const leaderboardRaw = await profileLeaderboardsCollection
.find(query)
.sort(sortQuery)
.limit(leaderboardMax)
.toArray();
+ fetchingRawLeaderboardNames.delete(name);
exports.cachedRawLeaderboards.set(name, leaderboardRaw);
return leaderboardRaw;
}