Skip to content

Commit b225ea4

Browse files
authored
Merge pull request #2154 from codefori/feature/awaitEvents
Await on event subscribers functions
2 parents 4948181 + d188bdf commit b225ea4

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/api/IBMi.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ export default class IBMi {
770770
if (!bashrcExists) {
771771
const createBashrc = await this.sendCommand({ command: `echo "# Generated by Code for IBM i\nexport PATH=/QOpenSys/pkgs/bin:\\$PATH" >> ${bashrcFile} && chown ${connectionObject.username.toLowerCase()} ${bashrcFile} && chmod 755 ${bashrcFile}` });
772772
if (createBashrc.code !== 0) {
773-
await vscode.window.showWarningMessage(`Error creating ${bashrcFile}):\n${createBashrc.stderr}.\n\n Code for IBM i may not function correctly. Please contact your system administrator.`, { modal: true });
773+
vscode.window.showWarningMessage(`Error creating ${bashrcFile}):\n${createBashrc.stderr}.\n\n Code for IBM i may not function correctly. Please contact your system administrator.`, { modal: true });
774774
}
775775
}
776776
else {
@@ -803,7 +803,7 @@ export default class IBMi {
803803
}
804804
}
805805
catch (error) {
806-
await vscode.window.showWarningMessage(`Error modifying PATH in ${bashrcFile}):\n${error}.\n\n Code for IBM i may not function correctly. Please contact your system administrator.`, { modal: true });
806+
vscode.window.showWarningMessage(`Error modifying PATH in ${bashrcFile}):\n${error}.\n\n Code for IBM i may not function correctly. Please contact your system administrator.`, { modal: true });
807807
}
808808
}
809809
});
@@ -902,7 +902,9 @@ export default class IBMi {
902902
if (!reconnecting) {
903903
vscode.workspace.getConfiguration().update(`workbench.editor.enablePreview`, false, true);
904904
await vscode.commands.executeCommand(`setContext`, `code-for-ibmi:connected`, true);
905-
delayedOperations.forEach(func => func());
905+
for (const operation of delayedOperations) {
906+
await operation();
907+
}
906908
instance.fire("connected");
907909
}
908910

src/api/Instance.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import { ConnectionConfiguration } from "./Configuration";
44
import IBMi from "./IBMi";
55
import { ConnectionStorage, GlobalStorage } from "./Storage";
66

7+
type IBMiEventSubscription = {
8+
event: IBMiEvent,
9+
func: Function
10+
};
11+
712
export default class Instance {
813
private connection: IBMi | undefined;
914
private storage: ConnectionStorage;
1015
private emitter: vscode.EventEmitter<IBMiEvent> = new vscode.EventEmitter();
11-
private events: { event: IBMiEvent, func: Function }[] = [];
16+
private subscribers: IBMiEventSubscription[] = [];
1217

1318
constructor(context: vscode.ExtensionContext) {
14-
this.events = [];
19+
this.subscribers = [];
1520
this.storage = new ConnectionStorage(context);
16-
this.emitter.event(e => {
17-
this.events.filter(event => event.event === e)
18-
.forEach(event => event.func());
19-
})
21+
this.emitter.event(e => this.processEvent(e));
2022
}
2123

2224
async setConnection(connection?: IBMi) {
@@ -55,10 +57,16 @@ export default class Instance {
5557
}
5658

5759
onEvent(event: IBMiEvent, func: Function): void {
58-
this.events.push({ event, func });
60+
this.subscribers.push({ event, func });
5961
}
6062

6163
fire(event: IBMiEvent) {
6264
this.emitter?.fire(event);
6365
}
66+
67+
async processEvent(event: IBMiEvent) {
68+
for (const subscriber of this.subscribers.filter(s => s.event === event)) {
69+
await subscriber.func();
70+
}
71+
}
6472
}

0 commit comments

Comments
 (0)