Move fetchJSON to abstract MapProvider

This commit is contained in:
James Lyne 2021-07-24 20:13:19 +01:00
parent ad9af04e9e
commit e4e7dfcd5f
2 changed files with 35 additions and 35 deletions

View File

@ -582,40 +582,6 @@ export default class DynmapMapProvider extends MapProvider {
return updates; return updates;
} }
private static async fetchJSON(url: string, signal: AbortSignal) {
let response, json;
try {
response = await fetch(url, {signal});
} catch(e) {
if(e instanceof DOMException && e.name === 'AbortError') {
console.warn(`Request aborted (${url}`);
throw e;
} else {
console.error(e);
}
throw new Error(`Network request failed`);
}
if (!response.ok) {
throw new Error(`Network request failed (${response.statusText || 'Unknown'})`);
}
try {
json = await response.json();
} catch(e) {
if(e instanceof DOMException && e.name === 'AbortError') {
console.warn(`Request aborted (${url}`);
throw e;
} else {
throw new Error('Request returned invalid json');
}
}
return json;
}
private async getMarkerSets(world: string): Promise<Map<string, LiveAtlasMarkerSet>> { private async getMarkerSets(world: string): Promise<Map<string, LiveAtlasMarkerSet>> {
const url = `${useStore().getters.serverConfig.dynmap.markers}_markers_/marker_${world}.json`; const url = `${useStore().getters.serverConfig.dynmap.markers}_markers_/marker_${world}.json`;

View File

@ -16,10 +16,44 @@ export default abstract class MapProvider implements LiveAtlasMapProvider {
}); });
} }
abstract destroy(): void;
abstract loadServerConfiguration(): Promise<void>; abstract loadServerConfiguration(): Promise<void>;
abstract loadWorldConfiguration(world: LiveAtlasWorldDefinition): Promise<void>; abstract loadWorldConfiguration(world: LiveAtlasWorldDefinition): Promise<void>;
abstract sendChatMessage(message: string): void; abstract sendChatMessage(message: string): void;
abstract startUpdates(): void; abstract startUpdates(): void;
abstract stopUpdates(): void; abstract stopUpdates(): void;
abstract destroy(): void;
protected static async fetchJSON(url: string, signal: AbortSignal) {
let response, json;
try {
response = await fetch(url, {signal});
} catch(e) {
if(e instanceof DOMException && e.name === 'AbortError') {
console.warn(`Request aborted (${url}`);
throw e;
} else {
console.error(e);
}
throw new Error(`Network request failed`);
}
if (!response.ok) {
throw new Error(`Network request failed (${response.statusText || 'Unknown'})`);
}
try {
json = await response.json();
} catch(e) {
if(e instanceof DOMException && e.name === 'AbortError') {
console.warn(`Request aborted (${url}`);
throw e;
} else {
throw new Error('Request returned invalid json');
}
}
return json;
}
} }