mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-25 06:42:51 +02:00
Prevent race conditions in Pokemon game data fetching
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { removeDuplicates, firstUpperCase } = require('../../util/Util');
|
const { removeDuplicates, firstUpperCase, delay } = require('../../util/Util');
|
||||||
const missingno = require('../../assets/json/missingno');
|
const missingno = require('../../assets/json/missingno');
|
||||||
const versions = require('../../assets/json/pokedex-location');
|
const versions = require('../../assets/json/pokedex-location');
|
||||||
|
|
||||||
@@ -58,6 +58,7 @@ module.exports = class Pokemon {
|
|||||||
this.moveSet = data.missingno ? data.moveSet : [];
|
this.moveSet = data.missingno ? data.moveSet : [];
|
||||||
this.moveSetVersion = data.missingno ? data.moveSetVersion : null;
|
this.moveSetVersion = data.missingno ? data.moveSetVersion : null;
|
||||||
this.gameDataCached = data.missingno || false;
|
this.gameDataCached = data.missingno || false;
|
||||||
|
this.gameDataFetching = data.missingno || false;
|
||||||
this.missingno = data.missingno || false;
|
this.missingno = data.missingno || false;
|
||||||
this.cry = data.id > store.pokemonCountWithCry
|
this.cry = data.id > store.pokemonCountWithCry
|
||||||
? null
|
? null
|
||||||
@@ -169,11 +170,17 @@ module.exports = class Pokemon {
|
|||||||
|
|
||||||
async fetchGameData() {
|
async fetchGameData() {
|
||||||
if (this.gameDataCached) return this;
|
if (this.gameDataCached) return this;
|
||||||
|
if (this.gameDataFetching) {
|
||||||
|
await delay(1000);
|
||||||
|
return this.fetchGameData();
|
||||||
|
}
|
||||||
|
this.gameDataFetching = true;
|
||||||
await this.fetchDefaultVariety();
|
await this.fetchDefaultVariety();
|
||||||
await this.fetchMoves(this.rawMoveSet);
|
await this.fetchMoves(this.rawMoveSet);
|
||||||
await this.fetchHeldItemNames();
|
await this.fetchHeldItemNames();
|
||||||
await this.fetchOtherVarieties();
|
await this.fetchOtherVarieties();
|
||||||
await this.fetchChain();
|
await this.fetchChain();
|
||||||
|
this.gameDataFetching = false;
|
||||||
this.gameDataCached = true;
|
this.gameDataCached = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user