Skip to content

Commit e025f45

Browse files
committed
getConfigFile can now lazy load new configuration files in
Signed-off-by: worksofliam <mrliamallan@live.co.uk>
1 parent 701e00a commit e025f45

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

src/api/IBMi.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ interface ConnectionCallbacks {
6868

6969
interface ConnectionConfigFiles {
7070
settings: ConfigFile<RemoteConfigFile>;
71+
[key: string]: ConfigFile<any>;
7172
}
7273

7374
export default class IBMi {
@@ -157,8 +158,37 @@ export default class IBMi {
157158
this.disconnectedCallback = callback;
158159
}
159160

161+
/**
162+
* getConfigFile can return pre-defined configuration files,
163+
* but can lazy load new configuration files as well.
164+
*
165+
* This does not load the configuration file from the server,
166+
* it only returns a ConfigFile instance. You should check the
167+
* state of the ConfigFile instance to see if it has been loaded,
168+
* and if not, call `loadFromServer()` on it.
169+
*/
160170
getConfigFile<T>(id: keyof ConnectionConfigFiles) {
161-
return this.configFiles[id] as ConfigFile<T>;
171+
if (!this.configFiles[id]) {
172+
this.configFiles[id] = new ConfigFile<T>(this, id as string, {} as T);
173+
}
174+
175+
const configFile = this.configFiles[id] as ConfigFile<T>;
176+
177+
return configFile;
178+
}
179+
180+
async loadRemoteConfigs() {
181+
for (const configFile in this.configFiles) {
182+
const currentConfig = this.configFiles[configFile as keyof ConnectionConfigFiles];
183+
184+
currentConfig.reset();
185+
186+
try {
187+
await currentConfig.loadFromServer();
188+
} catch (e) { }
189+
190+
this.appendOutput(`${configFile} config state: ` + JSON.stringify(currentConfig.getState()) + `\n`);
191+
}
162192
}
163193

164194
get canUseCqsh() {
@@ -507,7 +537,7 @@ export default class IBMi {
507537
await this.loadRemoteConfigs();
508538

509539
const remoteConnectionConfig = this.getConfigFile<RemoteConfigFile>(`settings`);
510-
if (remoteConnectionConfig.getState().server === `ok`) {
540+
if (remoteConnectionConfig.getState() === `ok`) {
511541
const remoteConfig = await remoteConnectionConfig.get();
512542

513543
if (remoteConfig.codefori) {
@@ -1075,20 +1105,6 @@ export default class IBMi {
10751105
return this.shell === IBMi.bashShellPath;
10761106
}
10771107

1078-
async loadRemoteConfigs() {
1079-
for (const configFile in this.configFiles) {
1080-
const currentConfig = this.configFiles[configFile as keyof ConnectionConfigFiles];
1081-
1082-
this.configFiles[configFile as keyof ConnectionConfigFiles].reset();
1083-
1084-
try {
1085-
await this.configFiles[configFile as keyof ConnectionConfigFiles].loadFromServer();
1086-
} catch (e) { }
1087-
1088-
this.appendOutput(`${configFile} config state: ` + JSON.stringify(currentConfig.getState()) + `\n`);
1089-
}
1090-
}
1091-
10921108
/**
10931109
* - Send PASE/QSH/ILE commands simply
10941110
* - Commands sent here end in the 'IBM i Output' channel

src/api/configuration/serverFile.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface LoadResult {
1313
}
1414

1515
export class ConfigFile<T> {
16-
private state: LoadResult = {server: `not_loaded`};
16+
private state: ConfigResult = `not_loaded`;
1717
private basename: string;
1818
private serverFile: string;
1919
private serverData: T|undefined;
@@ -34,24 +34,24 @@ export class ConfigFile<T> {
3434
async loadFromServer() {
3535
let serverConfig: any|undefined;
3636

37-
this.state.server = `no_exist`;
37+
this.state = `no_exist`;
3838

3939
const isAvailable = await this.connection.getContent().testStreamFile(this.serverFile, `r`);
4040
if (isAvailable) {
4141
const content = await this.connection.getContent().downloadStreamfileRaw(this.serverFile);
4242
try {
4343
serverConfig = JSON.parse(content.toString());
44-
this.state.server = `ok`;
44+
this.state = `ok`;
4545
} catch (e: any) {
46-
this.state.server = `failed_to_parse`;
46+
this.state = `failed_to_parse`;
4747
}
4848

4949
if (this.validateData) {
5050
// Should throw an error.
5151
try {
5252
this.serverData = this.validateData(serverConfig);
5353
} catch (e) {
54-
this.state.server = `invalid`;
54+
this.state = `invalid`;
5555
this.serverData = undefined;
5656
}
5757
} else {
@@ -66,7 +66,7 @@ export class ConfigFile<T> {
6666

6767
reset() {
6868
this.serverData = undefined;
69-
this.state.server = `not_loaded`;
69+
this.state = `not_loaded`;
7070
}
7171

7272
getState() {

src/webviews/settings/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class SettingsUI {
5151
config = await IBMi.connectionManager.load(connection.currentConnectionName);
5252

5353
const remoteConnectionConfig = connection.getConfigFile<RemoteConfigFile>(`settings`);
54-
const serverConfigOk = remoteConnectionConfig.getState().server === `ok`;
54+
const serverConfigOk = remoteConnectionConfig.getState() === `ok`;
5555

5656
if (serverConfigOk) {
5757
serverConfig = await remoteConnectionConfig.get();

0 commit comments

Comments
 (0)