diff --git a/src/api/IBMi.ts b/src/api/IBMi.ts index d224250a7..36faab517 100644 --- a/src/api/IBMi.ts +++ b/src/api/IBMi.ts @@ -68,6 +68,7 @@ interface ConnectionCallbacks { interface ConnectionConfigFiles { settings: ConfigFile; + [key: string]: ConfigFile; } export default class IBMi { @@ -157,8 +158,37 @@ export default class IBMi { this.disconnectedCallback = callback; } + /** + * getConfigFile can return pre-defined configuration files, + * but can lazy load new configuration files as well. + * + * This does not load the configuration file from the server, + * it only returns a ConfigFile instance. You should check the + * state of the ConfigFile instance to see if it has been loaded, + * and if not, call `loadFromServer()` on it. + */ getConfigFile(id: keyof ConnectionConfigFiles) { - return this.configFiles[id] as ConfigFile; + if (!this.configFiles[id]) { + this.configFiles[id] = new ConfigFile(this, id as string, {} as T); + } + + const configFile = this.configFiles[id] as ConfigFile; + + return configFile; + } + + async loadRemoteConfigs() { + for (const configFile in this.configFiles) { + const currentConfig = this.configFiles[configFile as keyof ConnectionConfigFiles]; + + currentConfig.reset(); + + try { + await currentConfig.loadFromServer(); + } catch (e) { } + + this.appendOutput(`${configFile} config state: ` + JSON.stringify(currentConfig.getState()) + `\n`); + } } get canUseCqsh() { @@ -507,7 +537,7 @@ export default class IBMi { await this.loadRemoteConfigs(); const remoteConnectionConfig = this.getConfigFile(`settings`); - if (remoteConnectionConfig.getState().server === `ok`) { + if (remoteConnectionConfig.getState() === `ok`) { const remoteConfig = await remoteConnectionConfig.get(); if (remoteConfig.codefori) { @@ -1075,20 +1105,6 @@ export default class IBMi { return this.shell === IBMi.bashShellPath; } - async loadRemoteConfigs() { - for (const configFile in this.configFiles) { - const currentConfig = this.configFiles[configFile as keyof ConnectionConfigFiles]; - - this.configFiles[configFile as keyof ConnectionConfigFiles].reset(); - - try { - await this.configFiles[configFile as keyof ConnectionConfigFiles].loadFromServer(); - } catch (e) { } - - this.appendOutput(`${configFile} config state: ` + JSON.stringify(currentConfig.getState()) + `\n`); - } - } - /** * - Send PASE/QSH/ILE commands simply * - Commands sent here end in the 'IBM i Output' channel diff --git a/src/api/configuration/serverFile.ts b/src/api/configuration/serverFile.ts index 5f6b8702b..e0bf12a80 100644 --- a/src/api/configuration/serverFile.ts +++ b/src/api/configuration/serverFile.ts @@ -13,7 +13,7 @@ interface LoadResult { } export class ConfigFile { - private state: LoadResult = {server: `not_loaded`}; + private state: ConfigResult = `not_loaded`; private basename: string; private serverFile: string; private serverData: T|undefined; @@ -34,16 +34,16 @@ export class ConfigFile { async loadFromServer() { let serverConfig: any|undefined; - this.state.server = `no_exist`; + this.state = `no_exist`; const isAvailable = await this.connection.getContent().testStreamFile(this.serverFile, `r`); if (isAvailable) { const content = await this.connection.getContent().downloadStreamfileRaw(this.serverFile); try { serverConfig = JSON.parse(content.toString()); - this.state.server = `ok`; + this.state = `ok`; } catch (e: any) { - this.state.server = `failed_to_parse`; + this.state = `failed_to_parse`; } if (this.validateData) { @@ -51,7 +51,7 @@ export class ConfigFile { try { this.serverData = this.validateData(serverConfig); } catch (e) { - this.state.server = `invalid`; + this.state = `invalid`; this.serverData = undefined; } } else { @@ -66,7 +66,7 @@ export class ConfigFile { reset() { this.serverData = undefined; - this.state.server = `not_loaded`; + this.state = `not_loaded`; } getState() { diff --git a/src/webviews/settings/index.ts b/src/webviews/settings/index.ts index 63f06f77e..ff929c626 100644 --- a/src/webviews/settings/index.ts +++ b/src/webviews/settings/index.ts @@ -51,7 +51,7 @@ export class SettingsUI { config = await IBMi.connectionManager.load(connection.currentConnectionName); const remoteConnectionConfig = connection.getConfigFile(`settings`); - const serverConfigOk = remoteConnectionConfig.getState().server === `ok`; + const serverConfigOk = remoteConnectionConfig.getState() === `ok`; if (serverConfigOk) { serverConfig = await remoteConnectionConfig.get();