Skip to content

Commit

Permalink
Smarter activation / deactivation of server polling (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Aug 22, 2024
1 parent 69b4c35 commit 4d70e08
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
31 changes: 31 additions & 0 deletions src/controllers/ExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class ExtensionController implements Disposable {
this.initializeConnectionController();
this.initializeCommands();
this.initializeWebViews();
this.initializePolling();

logger.info(
'Congratulations, your extension "vscode-deephaven" is now active!'
Expand Down Expand Up @@ -296,6 +297,21 @@ export class ExtensionController implements Disposable {
);
};

/**
* Listen to events that will enable / disable server polling.
*/
initializePolling = (): void => {
assertDefined(this._serverTreeView, 'serverManager');

vscode.window.onDidChangeWindowState(
this.onUpdateServerPolling,
undefined,
this._context.subscriptions
);

this._serverTreeView.onDidChangeVisibility(this.onUpdateServerPolling);
};

/**
* Handle connecting to a server
*/
Expand Down Expand Up @@ -374,6 +390,21 @@ export class ExtensionController implements Disposable {
await dhService?.runEditorCode(editor, true);
};

/**
* Enable / disable server polling based on whether server tree view is visible
* and vscode window is active and focused.
*/
onUpdateServerPolling = (): void => {
// Only poll servers if vscode window is active and focused and server
// connection tree is visible
const enablePolling =
this._serverTreeView?.visible &&
vscode.window.state.active &&
vscode.window.state.focused;

this._serverManager?.setPolling(enablePolling ?? false);
};

/**
* Register a command and add it's subscription to the context.
*/
Expand Down
26 changes: 20 additions & 6 deletions src/services/ServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ export class ServerManager implements IServerManager {
this.loadServerConfig();
}

/**
* Enable / disable polling configured servers.
* @param enablePolling
*/
setPolling = (enablePolling: boolean): void => {
if (enablePolling) {
if (!this._poller.isRunning) {
logger.debug('Start polling servers.');
this._poller.start(this.updateStatus, SERVER_STATUS_CHECK_INTERVAL);
}
} else {
if (this._poller.isRunning) {
logger.debug('Stop polling servers.');
this._poller.stop();
}
}
};

ensureHasPolledServers = async (): Promise<void> => {
if (this._hasPolledServers) {
return;
Expand Down Expand Up @@ -88,6 +106,8 @@ export class ServerManager implements IServerManager {
this.disconnectFromServer(serverUrl);
}
}

this.updateStatus();
};

connectToServer = async (serverUrl: URL): Promise<IDhService | null> => {
Expand Down Expand Up @@ -174,12 +194,6 @@ export class ServerManager implements IServerManager {
isRunning?: boolean;
hasConnections?: boolean;
} = {}): ServerState[] => {
// Start polling server status the first time servers are requested.
// TBD: Is there a way to stop this when the servers list goes out of view?
if (!this._poller.isRunning) {
this._poller.start(this.updateStatus, SERVER_STATUS_CHECK_INTERVAL);
}

const servers = [...this._serverMap.values()];

const match = (server: ServerState): boolean =>
Expand Down
2 changes: 2 additions & 0 deletions src/types/serviceTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export type IDhServiceFactory = IFactory<IDhService, [serverUrl: URL]>;
*/
export interface IServerManager extends Disposable {
ensureHasPolledServers: () => Promise<void>;
setPolling: (isPolling: boolean) => void;

connectToServer: (serverUrl: URL) => Promise<IDhService | null>;
disconnectEditor: (uri: vscode.Uri) => void;
disconnectFromServer: (serverUrl: URL) => Promise<void>;
Expand Down

0 comments on commit 4d70e08

Please sign in to comment.