More async/await

This commit is contained in:
James Lyne 2021-05-18 18:24:35 +01:00
parent a3aa04c2dd
commit 509089e5b7
4 changed files with 126 additions and 117 deletions

View File

@ -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 = () => {

View File

@ -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,14 +690,9 @@ 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();
}).then((response): DynmapConfigurationResponse => {
if (response.error === 'login-required') { if (response.error === 'login-required') {
throw new Error("Login required"); throw new Error("Login required");
} else if (response.error) { } else if (response.error) {
@ -688,21 +706,14 @@ export default {
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) {
throw new Error('Network response was not ok');
}
return response.json();
}).then((response): DynmapUpdateResponse => {
const players: Set<DynmapPlayer> = new Set(); const players: Set<DynmapPlayer> = new Set();
(response.players || []).forEach((player: any) => { (response.players || []).forEach((player: any) => {
@ -754,19 +765,12 @@ export default {
players, players,
updates: buildUpdates(response.updates || []), 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) {
throw new Error('Network response was not ok');
}
return response.json();
}).then((response): Map<string, DynmapMarkerSet> => {
const sets: Map<string, DynmapMarkerSet> = new Map(); const sets: Map<string, DynmapMarkerSet> = new Map();
response.sets = response.sets || {}; response.sets = response.sets || {};
@ -792,7 +796,6 @@ export default {
} }
return sets; return sets;
});
}, },
sendChatMessage(message: string) { sendChatMessage(message: string) {

View File

@ -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 => {

View File

@ -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) {}
} }
}; };