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 = async () => {
//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) {
updateTimeout.value = setTimeout(() => update(), updateInterval.value);
}
});
}
},
stopUpdates = () => {

View File

@ -658,6 +658,29 @@ const validateDynmapConfiguration = (config: DynmapUrlConfig): Promise<Map<strin
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 {
validateConfiguration(): Promise<Map<string, LiveAtlasServerDefinition>> {
if (typeof window.liveAtlasConfig.servers !== 'undefined') {
@ -667,14 +690,9 @@ export default {
return validateDynmapConfiguration(window.config.url ?? null);
},
getConfiguration(): Promise<DynmapConfigurationResponse> {
return fetch(useStore().getters.serverConfig.dynmap.configuration).then(response => {
if (!response.ok) {
throw new Error('Network request failed: ' + response.statusText);
}
async getConfiguration(): Promise<DynmapConfigurationResponse> {
const response = await fetchJSON(useStore().getters.serverConfig.dynmap.configuration);
return response.json();
}).then((response): DynmapConfigurationResponse => {
if (response.error === 'login-required') {
throw new Error("Login required");
} else if (response.error) {
@ -688,21 +706,14 @@ export default {
components: buildComponents(response),
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;
url = url.replace('{world}', world);
url = url.replace('{timestamp}', timestamp.toString());
return fetch(url).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
}).then((response): DynmapUpdateResponse => {
const response = await fetchJSON(url);
const players: Set<DynmapPlayer> = new Set();
(response.players || []).forEach((player: any) => {
@ -754,19 +765,12 @@ export default {
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`;
return fetch(url).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
}).then((response): Map<string, DynmapMarkerSet> => {
const response = await fetchJSON(url);
const sets: Map<string, DynmapMarkerSet> = new Map();
response.sets = response.sets || {};
@ -792,7 +796,6 @@ export default {
}
return sets;
});
},
sendChatMessage(message: string) {

View File

@ -68,7 +68,7 @@
}
}),
sendMessage = () => {
sendMessage = async () => {
const message = enteredMessage.value.trim().substring(0, messageMaxLength.value);
if(!message) {
@ -77,21 +77,22 @@
sendingMessage.value = true;
sendingError.value = null;
store.dispatch(ActionTypes.SEND_CHAT_MESSAGE, message).then(() => {
t
try {
await store.dispatch(ActionTypes.SEND_CHAT_MESSAGE, message);
enteredMessage.value = "";
sendingError.value = null;
}).catch(e => {
} catch(e) {
if(e instanceof ChatError) {
sendingError.value = e.message;
} else {
sendingError.value = `An unexpected error occurred. See console for details.`;
}
}).finally(() => {
} finally {
sendingMessage.value = false;
requestAnimationFrame(() => chatInput.value!.focus());
});
}
};
watch(chatBoxVisible, newValue => {

View File

@ -62,11 +62,14 @@ export default defineComponent({
unfollow();
},
updatePlayerImage = () => {
updatePlayerImage = async () => {
image.value = defaultImage;
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) {}
}
};