Skip to content

Commit

Permalink
Rewrite getConfig to attempt to reacquire config if missing
Browse files Browse the repository at this point in the history
While this does not solve latent navigation issues when they show up
because getConfig must remain a synchronous function, this does allow the
application to attempt to recover should the user try further navigation
actions after it finds the configuration is somehow missing from session
storage.

Signed-off-by: Jeremy Ho <[email protected]>
  • Loading branch information
jujaga committed Feb 21, 2024
1 parent 0316035 commit 8318978
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions frontend/src/services/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export default class ConfigService {
return new Promise((resolve, reject) => {
if (storageType.getItem(StorageKey.CONFIG) === null) {
axios
.get('/config')
.get('/config', {
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
})
.then(({ data }) => {
storageType.setItem(StorageKey.CONFIG, JSON.stringify(data));
resolve(new ConfigService());
Expand All @@ -54,11 +59,30 @@ export default class ConfigService {
*/
public getConfig(): any | undefined {
try {
const cfgString = storageType.getItem(StorageKey.CONFIG);
return cfgString ? JSON.parse(cfgString) : undefined;
let cfgString = storageType.getItem(StorageKey.CONFIG);
if (cfgString === null) {
// eslint-disable-next-line no-console
console.warn('Configuration missing. Attempting to reacquire...');
axios
.get('/config', {
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
})
.then(({ data }) => {
storageType.setItem(StorageKey.CONFIG, JSON.stringify(data));
cfgString = data;
})
.catch((err) => {
// eslint-disable-next-line no-console
console.error(`Failed to reacquire configuration: ${err}`);
});
}
return JSON.parse(cfgString as string);
} catch (err: unknown) {
// eslint-disable-next-line no-console
console.error(`Missing configuration: ${err}`);
console.error(`Unparseable configuration: ${err}`);
return undefined;
}
}
Expand Down

0 comments on commit 8318978

Please sign in to comment.