Skip to content

Commit

Permalink
refactor: Move restart debouncing into RPCProcessService
Browse files Browse the repository at this point in the history
  • Loading branch information
dividedmind authored and dustinbyrne committed Sep 11, 2024
1 parent b95139d commit 979cdc6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import RpcProcessService from './services/rpcProcessService';
import CommandRegistry from './commands/commandRegistry';
import AssetService from './assets/assetService';
import clearNavieAiSettings from './commands/clearNavieAiSettings';
import EnvironmentVariableService from './services/environmentVariableService';
import ExtensionSettings from './configuration/extensionSettings';

export async function activate(context: vscode.ExtensionContext): Promise<AppMapService> {
Expand Down Expand Up @@ -241,8 +240,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<AppMap
workspaceServices.getServiceInstances(configManager)
);

context.subscriptions.push(new EnvironmentVariableService(rpcService));
context.subscriptions.push(
vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('appMap.commandLineEnvironment')) rpcService.scheduleRestart();
}),
vscode.commands.registerCommand('appmap.rpc.restart', async () => {
await rpcService.restartServer();
vscode.window.showInformationMessage('Navie restarted successfully.');
Expand Down
42 changes: 0 additions & 42 deletions src/services/environmentVariableService.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/services/rpcProcessService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default class RpcProcessService implements Disposable {
private readonly processWatcher: RpcProcessWatcher;
private rpcPort: number | undefined;
private diposables: Disposable[] = [];
private debounce?: NodeJS.Timeout;
private restarting = false;

public constructor(
private readonly context: ExtensionContext,
Expand Down Expand Up @@ -90,6 +92,25 @@ export default class RpcProcessService implements Disposable {
return this.processWatcher.restart();
}

public debounceTime = 5000;

public scheduleRestart() {
if (this.debounce) {
this.debounce.refresh();
} else {
this.debounce = setTimeout(() => this.debouncedRestart(), this.debounceTime).unref();
}
}

private debouncedRestart(): void {
if (this.restarting) this.scheduleRestart();
else {
this.debounce = undefined;
this.restarting = true;
this.restart().finally(() => (this.restarting = false));
}
}

protected async pushConfiguration() {
if (!this.available) return;

Expand Down
12 changes: 12 additions & 0 deletions test/unit/services/rpcProcessService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,17 @@ describe('RpcProcessService', () => {

expect(rpcService.port()).to.equal(port);
});

it('debounces the restart calls', async () => {
const restartSpy = sinon.spy(rpcService, 'restart');

rpcService.debounceTime = 50;

rpcService.scheduleRestart();
rpcService.scheduleRestart();
rpcService.scheduleRestart();

await waitFor(`Expecting debounced restart`, () => restartSpy.calledOnce);
});
});
});

0 comments on commit 979cdc6

Please sign in to comment.