refactor: initialize config via APP_INITIALIZER

Closes #181
This commit is contained in:
Jovan Krunić
2022-02-09 20:43:08 +01:00
parent 411e0970b8
commit bde0df219c
9 changed files with 37 additions and 72 deletions

View File

@@ -107,7 +107,6 @@ describe('ConfigProvider', () => {
expect(storageProviderSpy.has).toHaveBeenCalled();
expect(storageProviderSpy.get).toHaveBeenCalledTimes(0);
expect(configProvider.client.handshake).toHaveBeenCalled();
expect(configProvider.initialised).toBe(true);
expect(await configProvider.getValue('name')).toEqual(
sampleIndexResponse.app.name,
);
@@ -129,7 +128,6 @@ describe('ConfigProvider', () => {
expect(error).toEqual(new ConfigFetchError());
expect(storageProviderSpy.has).toHaveBeenCalled();
expect(storageProviderSpy.get).toHaveBeenCalled();
expect(configProvider.initialised).toBe(true);
expect(await configProvider.getValue('name')).toEqual(
sampleIndexResponse.app.name,
);

View File

@@ -50,21 +50,16 @@ export class ConfigProvider {
*/
config: SCIndexResponse;
/**
* First session indicator
*/
firstSession = true;
/**
* Initialised status flag of config provider
*/
initialised = false;
/**
* Version of the @openstapps/core package that app is using
*/
scVersion = packageJson.dependencies['@openstapps/core'];
/**
* First session indicator (config not found in storage)
*/
firstSession = true;
/**
* Constructor, initialise api client
*
@@ -100,17 +95,7 @@ export class ConfigProvider {
*
* @param attribute requested attribute from app configuration
*/
public async getValue(attribute: keyof SCAppConfiguration) {
if (!this.initialised) {
try {
await this.init();
} catch (error) {
// don't throw ConfigFetchError if saved config is available
if (error.name === 'ConfigFetchError' && !this.initialised) {
throw error;
}
}
}
public getValue(attribute: keyof SCAppConfiguration) {
if (typeof this.config.app[attribute] !== 'undefined') {
return this.config.app[attribute];
}
@@ -138,12 +123,10 @@ export class ConfigProvider {
async init(): Promise<void> {
let loadError;
let fetchError;
this.initialised = false;
// load saved configuration
try {
this.config = await this.loadLocal();
this.firstSession = false;
this.initialised = true;
this.logger.log(`initialised configuration from storage`);
if (this.config.backend.SCVersion !== this.scVersion) {
loadError = new WrongConfigVersionInStorage(
@@ -159,7 +142,6 @@ export class ConfigProvider {
try {
const fetchedConfig: SCIndexResponse = await this.fetch();
await this.set(fetchedConfig);
this.initialised = true;
this.logger.log(`initialised configuration from remote`);
} catch (error) {
fetchError = error;
@@ -169,7 +151,7 @@ export class ConfigProvider {
throw new ConfigInitError();
}
if (typeof loadError !== 'undefined') {
throw loadError;
this.logger.warn(loadError);
}
if (typeof fetchError !== 'undefined') {
throw fetchError;
@@ -182,7 +164,6 @@ export class ConfigProvider {
* @throws SavedConfigNotAvailable if no configuration could be loaded
*/
async loadLocal(): Promise<SCIndexResponse> {
await this.storageProvider.init();
// get local configuration
if (await this.storageProvider.has(STORAGE_KEY_CONFIG)) {
return this.storageProvider.get<SCIndexResponse>(STORAGE_KEY_CONFIG);