Skip to content

Commit 46a0779

Browse files
committed
Use event driven
1 parent 39bf1b6 commit 46a0779

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/client/repl/nativeRepl.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class NativeRepl implements Disposable {
4242

4343
private envChangeListenerRegistered = false;
4444

45-
private pendingInterpreterChangeTimer?: NodeJS.Timeout;
45+
private pendingInterpreterChange?: { resource?: Uri };
4646

4747
// TODO: In the future, could also have attribute of URI for file specific REPL.
4848
private constructor() {
@@ -63,9 +63,6 @@ export class NativeRepl implements Disposable {
6363
}
6464

6565
dispose(): void {
66-
if (this.pendingInterpreterChangeTimer) {
67-
clearTimeout(this.pendingInterpreterChangeTimer);
68-
}
6966
this.disposables.forEach((d) => d.dispose());
7067
}
7168

@@ -150,11 +147,20 @@ export class NativeRepl implements Disposable {
150147
this.updateInterpreterForChange(event.uri).catch(() => undefined);
151148
}),
152149
);
150+
this.disposables.push(
151+
this.pythonServer.onCodeExecuted(() => {
152+
if (this.pendingInterpreterChange) {
153+
const { resource } = this.pendingInterpreterChange;
154+
this.pendingInterpreterChange = undefined;
155+
this.updateInterpreterForChange(resource).catch(() => undefined);
156+
}
157+
}),
158+
);
153159
}
154160

155161
private async updateInterpreterForChange(resource?: Uri): Promise<void> {
156162
if (this.pythonServer?.isExecuting) {
157-
this.scheduleInterpreterUpdate(resource);
163+
this.pendingInterpreterChange = { resource };
158164
return;
159165
}
160166
if (!this.shouldApplyInterpreterChange(resource)) {
@@ -181,16 +187,6 @@ export class NativeRepl implements Disposable {
181187
}
182188
}
183189

184-
private scheduleInterpreterUpdate(resource?: Uri): void {
185-
if (this.pendingInterpreterChangeTimer) {
186-
clearTimeout(this.pendingInterpreterChangeTimer);
187-
}
188-
this.pendingInterpreterChangeTimer = setTimeout(() => {
189-
this.pendingInterpreterChangeTimer = undefined;
190-
this.updateInterpreterForChange(resource).catch(() => undefined);
191-
}, 200);
192-
}
193-
194190
private shouldApplyInterpreterChange(resource?: Uri): boolean {
195191
if (!resource || !this.cwd) {
196192
return true;

0 commit comments

Comments
 (0)