diff options
author | mat <github@matdoes.dev> | 2022-02-10 19:30:36 -0600 |
---|---|---|
committer | mat <github@matdoes.dev> | 2022-02-10 19:30:36 -0600 |
commit | 7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6 (patch) | |
tree | 33cf680ac831e43e05cb3b40647a04c370b32d32 /src/database.ts | |
parent | 3effd71de72aa86bfcd61e26f053ca4b7cc46416 (diff) | |
download | skyblock-api-7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6.tar.gz skyblock-api-7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6.tar.bz2 skyblock-api-7d87d0c3e058e1f6a8c129b2808a5e6ade3b47d6.zip |
fix leaderboards not loading sometimes, hopefully
Diffstat (limited to 'src/database.ts')
-rw-r--r-- | src/database.ts | 75 |
1 files changed, 44 insertions, 31 deletions
diff --git a/src/database.ts b/src/database.ts index 571ec5e..e195eed 100644 --- a/src/database.ts +++ b/src/database.ts @@ -313,21 +313,28 @@ async function fetchMemberLeaderboardRaw(name: string): Promise<memberRawLeaderb sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1 fetchingRawLeaderboardNames.add(name) - const leaderboardRaw: memberRawLeaderboardItem[] = (await memberLeaderboardsCollection - .find(query) - .sort(sortQuery) - .limit(leaderboardMax) - .toArray()) - .map((i: DatabaseMemberLeaderboardItem): memberRawLeaderboardItem => { - return { - profile: i.profile, - uuid: i.uuid, - value: i.stats[name] - } - }) - fetchingRawLeaderboardNames.delete(name) - cachedRawLeaderboards.set(name, leaderboardRaw) - return leaderboardRaw + + try { + const leaderboardRaw: memberRawLeaderboardItem[] = (await memberLeaderboardsCollection + .find(query) + .sort(sortQuery) + .limit(leaderboardMax) + .toArray()) + .map((i: DatabaseMemberLeaderboardItem): memberRawLeaderboardItem => { + return { + profile: i.profile, + uuid: i.uuid, + value: i.stats[name] + } + }) + fetchingRawLeaderboardNames.delete(name) + cachedRawLeaderboards.set(name, leaderboardRaw) + return leaderboardRaw + } catch (e) { + // if it fails while fetching, remove it from fetchingRawLeaderboardNames + fetchingRawLeaderboardNames.delete(name) + throw e + } } async function fetchProfileLeaderboardRaw(name: string): Promise<profileRawLeaderboardItem[]> { @@ -351,22 +358,28 @@ async function fetchProfileLeaderboardRaw(name: string): Promise<profileRawLeade sortQuery[`stats.${name}`] = isLeaderboardReversed(name) ? 1 : -1 fetchingRawLeaderboardNames.add(name) - const leaderboardRaw: profileRawLeaderboardItem[] = (await profileLeaderboardsCollection - .find(query) - .sort(sortQuery) - .limit(leaderboardMax) - .toArray()) - .map((i: DatabaseProfileLeaderboardItem): profileRawLeaderboardItem => { - return { - players: i.players, - uuid: i.uuid, - value: i.stats[name] - } - }) - fetchingRawLeaderboardNames.delete(name) - - cachedRawLeaderboards.set(name, leaderboardRaw) - return leaderboardRaw + try { + const leaderboardRaw: profileRawLeaderboardItem[] = (await profileLeaderboardsCollection + .find(query) + .sort(sortQuery) + .limit(leaderboardMax) + .toArray()) + .map((i: DatabaseProfileLeaderboardItem): profileRawLeaderboardItem => { + return { + players: i.players, + uuid: i.uuid, + value: i.stats[name] + } + }) + fetchingRawLeaderboardNames.delete(name) + + cachedRawLeaderboards.set(name, leaderboardRaw) + return leaderboardRaw + } catch (e) { + // if it fails while fetching, remove it from fetchingRawLeaderboardNames + fetchingRawLeaderboardNames.delete(name) + throw e + } } interface MemberLeaderboard { |