diff options
Diffstat (limited to 'src/hypixelCached.ts')
-rw-r--r-- | src/hypixelCached.ts | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts index c347a1a..3dc89a3 100644 --- a/src/hypixelCached.ts +++ b/src/hypixelCached.ts @@ -143,18 +143,32 @@ export async function usernameFromUser(user: string): Promise<string> { return username } +let fetchingPlayers: Set<string> = new Set() export async function fetchPlayer(user: string): Promise<CleanPlayer> { const playerUuid = await uuidFromUser(user) + if (playerCache.has(playerUuid)) return playerCache.get(playerUuid) + // if it's already in the process of fetching, check every 100ms until it's not fetching the player anymore and fetch it again, since it'll be cached now + if (fetchingPlayers.has(playerUuid)) { + while (fetchingPlayers.has(playerUuid)) { + await new Promise(resolve => setTimeout(resolve, 100)) + } + return await fetchPlayer(user) + } + + fetchingPlayers.add(playerUuid) + const cleanPlayer: CleanPlayer = await hypixel.sendCleanApiRequest({ path: 'player', args: { uuid: playerUuid } }) + fetchingPlayers.delete(playerUuid) + if (!cleanPlayer) return // clone in case it gets modified somehow later |