Use async/await for actions

This commit is contained in:
James Lyne 2021-05-18 18:22:34 +01:00
parent 8f0b1a5476
commit a6ae811660

View File

@ -77,11 +77,12 @@ export interface Actions {
}
export const actions: ActionTree<State, State> & Actions = {
[ActionTypes.LOAD_CONFIGURATION]({commit, state}): Promise<DynmapConfigurationResponse> {
async [ActionTypes.LOAD_CONFIGURATION]({commit, state}): Promise<DynmapConfigurationResponse> {
//Clear any existing has to avoid triggering a second config load, after this load changes the hash
commit(MutationTypes.CLEAR_CONFIGURATION_HASH, undefined);
return API.getConfiguration().then(config => {
const config = await API.getConfiguration();
commit(MutationTypes.SET_CONFIGURATION, config.config);
commit(MutationTypes.SET_MESSAGES, config.messages);
commit(MutationTypes.SET_WORLDS, config.worlds);
@ -141,14 +142,15 @@ export const actions: ActionTree<State, State> & Actions = {
}
return config;
});
},
[ActionTypes.GET_UPDATE]({commit, dispatch, state}) {
async [ActionTypes.GET_UPDATE]({commit, dispatch, state}) {
if(!state.currentWorld) {
return Promise.reject("No current world");
}
return API.getUpdate(state.updateRequestId, state.currentWorld.name, state.updateTimestamp.valueOf()).then(update => {
const update = await API.getUpdate(state.updateRequestId, state.currentWorld.name, state.updateTimestamp.valueOf())
commit(MutationTypes.SET_WORLD_STATE, update.worldState);
commit(MutationTypes.SET_UPDATE_TIMESTAMP, new Date(update.timestamp));
commit(MutationTypes.INCREMENT_REQUEST_ID, undefined);
@ -157,11 +159,10 @@ export const actions: ActionTree<State, State> & Actions = {
commit(MutationTypes.ADD_CHAT, update.updates.chat);
commit(MutationTypes.SET_CONFIGURATION_HASH, update.configHash);
return dispatch(ActionTypes.SET_PLAYERS, update.players).then(() => {
await dispatch(ActionTypes.SET_PLAYERS, update.players);
return update;
});
});
},
[ActionTypes.SET_PLAYERS]({commit, state}, players: Set<DynmapPlayer>) {
const keep: Set<string> = new Set();
@ -187,79 +188,79 @@ export const actions: ActionTree<State, State> & Actions = {
requestAnimationFrame(() => processQueue(players, resolve));
});
},
[ActionTypes.GET_MARKER_SETS]({commit, state}) {
async [ActionTypes.GET_MARKER_SETS]({commit, state}) {
if(!state.currentWorld) {
return Promise.reject("No current world");
throw new Error("No current world");
}
return API.getMarkerSets(state.currentWorld.name).then(markerSets => {
const markerSets = await API.getMarkerSets(state.currentWorld.name)
commit(MutationTypes.SET_MARKER_SETS, markerSets);
return markerSets;
});
},
[ActionTypes.POP_MARKER_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapMarkerUpdate[]> {
async [ActionTypes.POP_MARKER_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapMarkerUpdate[]> {
if(!state.markerSets.has(markerSet)) {
console.warn(`POP_MARKER_UPDATES: Marker set ${markerSet} doesn't exist`);
return Promise.resolve([]);
return [];
}
const updates = state.pendingSetUpdates.get(markerSet)!.markerUpdates.slice(0, amount);
commit(MutationTypes.POP_MARKER_UPDATES, {markerSet, amount});
return Promise.resolve(updates);
return updates;
},
[ActionTypes.POP_AREA_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapAreaUpdate[]> {
async [ActionTypes.POP_AREA_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapAreaUpdate[]> {
if(!state.markerSets.has(markerSet)) {
console.warn(`POP_AREA_UPDATES: Marker set ${markerSet} doesn't exist`);
return Promise.resolve([]);
return [];
}
const updates = state.pendingSetUpdates.get(markerSet)!.areaUpdates.slice(0, amount);
commit(MutationTypes.POP_AREA_UPDATES, {markerSet, amount});
return Promise.resolve(updates);
return updates;
},
[ActionTypes.POP_CIRCLE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapCircleUpdate[]> {
async [ActionTypes.POP_CIRCLE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapCircleUpdate[]> {
if(!state.markerSets.has(markerSet)) {
console.warn(`POP_CIRCLE_UPDATES: Marker set ${markerSet} doesn't exist`);
return Promise.resolve([]);
return [];
}
const updates = state.pendingSetUpdates.get(markerSet)!.circleUpdates.slice(0, amount);
commit(MutationTypes.POP_CIRCLE_UPDATES, {markerSet, amount});
return Promise.resolve(updates);
return updates;
},
[ActionTypes.POP_LINE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapLineUpdate[]> {
async [ActionTypes.POP_LINE_UPDATES]({commit, state}, {markerSet, amount}: {markerSet: string, amount: number}): Promise<DynmapLineUpdate[]> {
if(!state.markerSets.has(markerSet)) {
console.warn(`POP_LINE_UPDATES: Marker set ${markerSet} doesn't exist`);
return Promise.resolve([]);
return [];
}
const updates = state.pendingSetUpdates.get(markerSet)!.lineUpdates.slice(0, amount);
commit(MutationTypes.POP_LINE_UPDATES, {markerSet, amount});
return Promise.resolve(updates);
return updates;
},
[ActionTypes.POP_TILE_UPDATES]({commit, state}, amount: number): Promise<Array<DynmapTileUpdate>> {
async [ActionTypes.POP_TILE_UPDATES]({commit, state}, amount: number): Promise<Array<DynmapTileUpdate>> {
const updates = state.pendingTileUpdates.slice(0, amount);
commit(MutationTypes.POP_TILE_UPDATES, amount);
return Promise.resolve(updates);
return updates;
},
[ActionTypes.SEND_CHAT_MESSAGE]({commit, state}, message: string): Promise<void> {
return API.sendChatMessage(message);
async [ActionTypes.SEND_CHAT_MESSAGE]({commit, state}, message: string): Promise<void> {
await API.sendChatMessage(message);
},
}