Skip to content

Commit

Permalink
Merge pull request #2154 from codefori/feature/awaitEvents
Browse files Browse the repository at this point in the history
Await on event subscribers functions
  • Loading branch information
sebjulliand authored Jul 9, 2024
2 parents 4948181 + d188bdf commit b225ea4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/api/IBMi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export default class IBMi {
if (!bashrcExists) {
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}` });
if (createBashrc.code !== 0) {
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 });
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 });
}
}
else {
Expand Down Expand Up @@ -803,7 +803,7 @@ export default class IBMi {
}
}
catch (error) {
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 });
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 });
}
}
});
Expand Down Expand Up @@ -902,7 +902,9 @@ export default class IBMi {
if (!reconnecting) {
vscode.workspace.getConfiguration().update(`workbench.editor.enablePreview`, false, true);
await vscode.commands.executeCommand(`setContext`, `code-for-ibmi:connected`, true);
delayedOperations.forEach(func => func());
for (const operation of delayedOperations) {
await operation();
}
instance.fire("connected");
}

Expand Down
22 changes: 15 additions & 7 deletions src/api/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { ConnectionConfiguration } from "./Configuration";
import IBMi from "./IBMi";
import { ConnectionStorage, GlobalStorage } from "./Storage";

type IBMiEventSubscription = {
event: IBMiEvent,
func: Function
};

export default class Instance {
private connection: IBMi | undefined;
private storage: ConnectionStorage;
private emitter: vscode.EventEmitter<IBMiEvent> = new vscode.EventEmitter();
private events: { event: IBMiEvent, func: Function }[] = [];
private subscribers: IBMiEventSubscription[] = [];

constructor(context: vscode.ExtensionContext) {
this.events = [];
this.subscribers = [];
this.storage = new ConnectionStorage(context);
this.emitter.event(e => {
this.events.filter(event => event.event === e)
.forEach(event => event.func());
})
this.emitter.event(e => this.processEvent(e));
}

async setConnection(connection?: IBMi) {
Expand Down Expand Up @@ -55,10 +57,16 @@ export default class Instance {
}

onEvent(event: IBMiEvent, func: Function): void {
this.events.push({ event, func });
this.subscribers.push({ event, func });
}

fire(event: IBMiEvent) {
this.emitter?.fire(event);
}

async processEvent(event: IBMiEvent) {
for (const subscriber of this.subscribers.filter(s => s.event === event)) {
await subscriber.func();
}
}
}

0 comments on commit b225ea4

Please sign in to comment.