TableturfBattleApp/TableturfBattleClient/src/StageDatabase.ts
Andrio Celos d4e8d86571 Rework sync procedure and add a loading indicator
The server will now send a sync message when the client connects via WebSocket. This removes the need to send another GET request to sync game data.
2022-10-25 12:08:18 +11:00

32 lines
1019 B
TypeScript

const stageDatabase = {
stages: null as Stage[] | null,
loadAsync() {
return new Promise<Stage[]>((resolve, reject) => {
if (stageDatabase.stages != null) {
resolve(stageDatabase.stages);
return;
}
const stageListRequest = new XMLHttpRequest();
stageListRequest.open('GET', `${config.apiBaseUrl}/stages`);
stageListRequest.addEventListener('load', e => {
const stages = [ ];
if (stageListRequest.status == 200) {
const s = stageListRequest.responseText;
const response = JSON.parse(s) as object[];
for (const o of response) {
stages.push(Stage.fromJson(o));
}
stageDatabase.stages = stages;
resolve(stages);
} else {
reject(new Error(`Error downloading stage database: response was ${stageListRequest.status}`));
}
});
stageListRequest.addEventListener('error', e => {
reject(new Error('Error downloading stage database: no further information.'))
});
stageListRequest.send();
});
}
}