More async/await
This commit is contained in:
parent
a3aa04c2dd
commit
509089e5b7
@ -67,13 +67,15 @@ export default defineComponent({
|
|||||||
update();
|
update();
|
||||||
},
|
},
|
||||||
|
|
||||||
update = () => {
|
update = async () => {
|
||||||
//TODO: Error notification for repeated failures?
|
//TODO: Error notification for repeated failures?
|
||||||
store.dispatch(ActionTypes.GET_UPDATE, undefined).finally(() => {
|
try {
|
||||||
|
await store.dispatch(ActionTypes.GET_UPDATE, undefined);
|
||||||
|
} finally {
|
||||||
if(updatesEnabled.value) {
|
if(updatesEnabled.value) {
|
||||||
updateTimeout.value = setTimeout(() => update(), updateInterval.value);
|
updateTimeout.value = setTimeout(() => update(), updateInterval.value);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
stopUpdates = () => {
|
stopUpdates = () => {
|
||||||
|
215
src/api.ts
215
src/api.ts
@ -658,6 +658,29 @@ const validateDynmapConfiguration = (config: DynmapUrlConfig): Promise<Map<strin
|
|||||||
return Promise.resolve(result);
|
return Promise.resolve(result);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function fetchJSON(url: string) {
|
||||||
|
let response, json;
|
||||||
|
|
||||||
|
try {
|
||||||
|
response = await fetch(url);
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error(`Network request failed (${e})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Network request failed (${response.statusText || 'Unknown'})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
json = await response.json();
|
||||||
|
} catch(e) {
|
||||||
|
throw new Error('Request returned invalid json');
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
validateConfiguration(): Promise<Map<string, LiveAtlasServerDefinition>> {
|
validateConfiguration(): Promise<Map<string, LiveAtlasServerDefinition>> {
|
||||||
if (typeof window.liveAtlasConfig.servers !== 'undefined') {
|
if (typeof window.liveAtlasConfig.servers !== 'undefined') {
|
||||||
@ -667,132 +690,112 @@ export default {
|
|||||||
return validateDynmapConfiguration(window.config.url ?? null);
|
return validateDynmapConfiguration(window.config.url ?? null);
|
||||||
},
|
},
|
||||||
|
|
||||||
getConfiguration(): Promise<DynmapConfigurationResponse> {
|
async getConfiguration(): Promise<DynmapConfigurationResponse> {
|
||||||
return fetch(useStore().getters.serverConfig.dynmap.configuration).then(response => {
|
const response = await fetchJSON(useStore().getters.serverConfig.dynmap.configuration);
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Network request failed: ' + response.statusText);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
if (response.error === 'login-required') {
|
||||||
}).then((response): DynmapConfigurationResponse => {
|
throw new Error("Login required");
|
||||||
if (response.error === 'login-required') {
|
} else if (response.error) {
|
||||||
throw new Error("Login required");
|
throw new Error(response.error);
|
||||||
} else if (response.error) {
|
}
|
||||||
throw new Error(response.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
config: buildServerConfig(response),
|
config: buildServerConfig(response),
|
||||||
messages: buildMessagesConfig(response),
|
messages: buildMessagesConfig(response),
|
||||||
worlds: buildWorlds(response),
|
worlds: buildWorlds(response),
|
||||||
components: buildComponents(response),
|
components: buildComponents(response),
|
||||||
loggedIn: response.loggedin || false,
|
loggedIn: response.loggedin || false,
|
||||||
}
|
}
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getUpdate(requestId: number, world: string, timestamp: number): Promise<DynmapUpdateResponse> {
|
async getUpdate(requestId: number, world: string, timestamp: number): Promise<DynmapUpdateResponse> {
|
||||||
let url = useStore().getters.serverConfig.dynmap.update;
|
let url = useStore().getters.serverConfig.dynmap.update;
|
||||||
url = url.replace('{world}', world);
|
url = url.replace('{world}', world);
|
||||||
url = url.replace('{timestamp}', timestamp.toString());
|
url = url.replace('{timestamp}', timestamp.toString());
|
||||||
|
|
||||||
return fetch(url).then(response => {
|
const response = await fetchJSON(url);
|
||||||
if (!response.ok) {
|
const players: Set<DynmapPlayer> = new Set();
|
||||||
throw new Error('Network response was not ok');
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
(response.players || []).forEach((player: any) => {
|
||||||
}).then((response): DynmapUpdateResponse => {
|
const world = player.world && player.world !== '-some-other-bogus-world-' ? player.world : undefined;
|
||||||
const players: Set<DynmapPlayer> = new Set();
|
|
||||||
|
|
||||||
(response.players || []).forEach((player: any) => {
|
players.add({
|
||||||
const world = player.world && player.world !== '-some-other-bogus-world-' ? player.world : undefined;
|
account: player.account || "",
|
||||||
|
health: player.health || 0,
|
||||||
players.add({
|
armor: player.armor || 0,
|
||||||
account: player.account || "",
|
name: player.name || "",
|
||||||
health: player.health || 0,
|
sort: player.sort || 0,
|
||||||
armor: player.armor || 0,
|
hidden: !world,
|
||||||
name: player.name || "",
|
location: {
|
||||||
sort: player.sort || 0,
|
//Add 0.5 to position in the middle of a block
|
||||||
hidden: !world,
|
x: !isNaN(player.x) ? player.x + 0.5 : 0,
|
||||||
location: {
|
y: !isNaN(player.y) ? player.y : 0,
|
||||||
//Add 0.5 to position in the middle of a block
|
z: !isNaN(player.z) ? player.z + 0.5 : 0,
|
||||||
x: !isNaN(player.x) ? player.x + 0.5 : 0,
|
world: world,
|
||||||
y: !isNaN(player.y) ? player.y : 0,
|
}
|
||||||
z: !isNaN(player.z) ? player.z + 0.5 : 0,
|
|
||||||
world: world,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Extra fake players for testing
|
|
||||||
// for(let i = 0; i < 150; i++) {
|
|
||||||
// players.add({
|
|
||||||
// account: "VIDEO GAMES " + i,
|
|
||||||
// health: Math.round(Math.random() * 10),
|
|
||||||
// armor: Math.round(Math.random() * 10),
|
|
||||||
// name: "VIDEO GAMES " + i,
|
|
||||||
// sort: 0,
|
|
||||||
// location: {
|
|
||||||
// x: Math.round(Math.random() * 1000) - 500,
|
|
||||||
// y: 64,
|
|
||||||
// z: Math.round(Math.random() * 1000) - 500,
|
|
||||||
// world: "world",
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
return {
|
|
||||||
worldState: {
|
|
||||||
timeOfDay: response.servertime || 0,
|
|
||||||
thundering: response.isThundering || false,
|
|
||||||
raining: response.hasStorm || false,
|
|
||||||
},
|
|
||||||
playerCount: response.count || 0,
|
|
||||||
configHash: response.confighash || 0,
|
|
||||||
timestamp: response.timestamp || 0,
|
|
||||||
players,
|
|
||||||
updates: buildUpdates(response.updates || []),
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Extra fake players for testing
|
||||||
|
// for(let i = 0; i < 150; i++) {
|
||||||
|
// players.add({
|
||||||
|
// account: "VIDEO GAMES " + i,
|
||||||
|
// health: Math.round(Math.random() * 10),
|
||||||
|
// armor: Math.round(Math.random() * 10),
|
||||||
|
// name: "VIDEO GAMES " + i,
|
||||||
|
// sort: 0,
|
||||||
|
// location: {
|
||||||
|
// x: Math.round(Math.random() * 1000) - 500,
|
||||||
|
// y: 64,
|
||||||
|
// z: Math.round(Math.random() * 1000) - 500,
|
||||||
|
// world: "world",
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
return {
|
||||||
|
worldState: {
|
||||||
|
timeOfDay: response.servertime || 0,
|
||||||
|
thundering: response.isThundering || false,
|
||||||
|
raining: response.hasStorm || false,
|
||||||
|
},
|
||||||
|
playerCount: response.count || 0,
|
||||||
|
configHash: response.confighash || 0,
|
||||||
|
timestamp: response.timestamp || 0,
|
||||||
|
players,
|
||||||
|
updates: buildUpdates(response.updates || []),
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getMarkerSets(world: string): Promise<Map<string, DynmapMarkerSet>> {
|
async getMarkerSets(world: string): Promise<Map<string, DynmapMarkerSet>> {
|
||||||
const url = `${useStore().getters.serverConfig.dynmap.markers}_markers_/marker_${world}.json`;
|
const url = `${useStore().getters.serverConfig.dynmap.markers}_markers_/marker_${world}.json`;
|
||||||
|
|
||||||
return fetch(url).then(response => {
|
const response = await fetchJSON(url);
|
||||||
if (!response.ok) {
|
const sets: Map<string, DynmapMarkerSet> = new Map();
|
||||||
throw new Error('Network response was not ok');
|
|
||||||
|
response.sets = response.sets || {};
|
||||||
|
|
||||||
|
for (const key in response.sets) {
|
||||||
|
if (!Object.prototype.hasOwnProperty.call(response.sets, key)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json();
|
const set = response.sets[key],
|
||||||
}).then((response): Map<string, DynmapMarkerSet> => {
|
markers = buildMarkers(set.markers || {}),
|
||||||
const sets: Map<string, DynmapMarkerSet> = new Map();
|
circles = buildCircles(set.circles || {}),
|
||||||
|
areas = buildAreas(set.areas || {}),
|
||||||
|
lines = buildLines(set.lines || {});
|
||||||
|
|
||||||
response.sets = response.sets || {};
|
sets.set(key, {
|
||||||
|
...buildMarkerSet(key, set),
|
||||||
|
markers,
|
||||||
|
circles,
|
||||||
|
areas,
|
||||||
|
lines,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for (const key in response.sets) {
|
return sets;
|
||||||
if (!Object.prototype.hasOwnProperty.call(response.sets, key)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const set = response.sets[key],
|
|
||||||
markers = buildMarkers(set.markers || {}),
|
|
||||||
circles = buildCircles(set.circles || {}),
|
|
||||||
areas = buildAreas(set.areas || {}),
|
|
||||||
lines = buildLines(set.lines || {});
|
|
||||||
|
|
||||||
sets.set(key, {
|
|
||||||
...buildMarkerSet(key, set),
|
|
||||||
markers,
|
|
||||||
circles,
|
|
||||||
areas,
|
|
||||||
lines,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return sets;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
sendChatMessage(message: string) {
|
sendChatMessage(message: string) {
|
||||||
|
@ -68,7 +68,7 @@
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
sendMessage = () => {
|
sendMessage = async () => {
|
||||||
const message = enteredMessage.value.trim().substring(0, messageMaxLength.value);
|
const message = enteredMessage.value.trim().substring(0, messageMaxLength.value);
|
||||||
|
|
||||||
if(!message) {
|
if(!message) {
|
||||||
@ -77,21 +77,22 @@
|
|||||||
|
|
||||||
sendingMessage.value = true;
|
sendingMessage.value = true;
|
||||||
sendingError.value = null;
|
sendingError.value = null;
|
||||||
|
t
|
||||||
store.dispatch(ActionTypes.SEND_CHAT_MESSAGE, message).then(() => {
|
try {
|
||||||
|
await store.dispatch(ActionTypes.SEND_CHAT_MESSAGE, message);
|
||||||
enteredMessage.value = "";
|
enteredMessage.value = "";
|
||||||
sendingError.value = null;
|
sendingError.value = null;
|
||||||
}).catch(e => {
|
} catch(e) {
|
||||||
if(e instanceof ChatError) {
|
if(e instanceof ChatError) {
|
||||||
sendingError.value = e.message;
|
sendingError.value = e.message;
|
||||||
} else {
|
} else {
|
||||||
sendingError.value = `An unexpected error occurred. See console for details.`;
|
sendingError.value = `An unexpected error occurred. See console for details.`;
|
||||||
}
|
}
|
||||||
}).finally(() => {
|
} finally {
|
||||||
sendingMessage.value = false;
|
sendingMessage.value = false;
|
||||||
|
|
||||||
requestAnimationFrame(() => chatInput.value!.focus());
|
requestAnimationFrame(() => chatInput.value!.focus());
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(chatBoxVisible, newValue => {
|
watch(chatBoxVisible, newValue => {
|
||||||
|
@ -62,11 +62,14 @@ export default defineComponent({
|
|||||||
|
|
||||||
unfollow();
|
unfollow();
|
||||||
},
|
},
|
||||||
updatePlayerImage = () => {
|
updatePlayerImage = async () => {
|
||||||
image.value = defaultImage;
|
image.value = defaultImage;
|
||||||
|
|
||||||
if(store.state.components.playerMarkers && store.state.components.playerMarkers.showSkinFaces) {
|
if(store.state.components.playerMarkers && store.state.components.playerMarkers.showSkinFaces) {
|
||||||
getMinecraftHead(props.target, '16').then((result) => image.value = result.src).catch(() => {});
|
try {
|
||||||
|
const result = await getMinecraftHead(props.target, '16');
|
||||||
|
image.value = result.src;
|
||||||
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user