Skip to content

Commit

Permalink
Resource disposal (152)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Nov 13, 2024
1 parent 34a6ea4 commit 9603cf9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/controllers/ExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ export class ExtensionController implements Disposable {
};

this._coreClientCache = new URLMap();
this._context.subscriptions.push(this._coreClientCache);

this._dheClientCache = new URLMap();
this._context.subscriptions.push(this._dheClientCache);

this._panelService = new PanelService();
this._context.subscriptions.push(this._panelService);
Expand Down
6 changes: 4 additions & 2 deletions src/services/DheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ export class DheService implements IDheService {
const querySerials = [...this._querySerialSet];

this._querySerialSet.clear();
this._workerInfoMap.clear();

await this._disposeQueries(querySerials);
await Promise.all([
this._workerInfoMap.dispose(),
this._disposeQueries(querySerials),
]);
};
}
7 changes: 5 additions & 2 deletions src/services/PanelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ export class PanelService implements IPanelService, Disposable {
* Cleanup resources.
*/
dispose = async (): Promise<void> => {
this._cnPanelMap.clear();
this._cnVariableMap.clear();
this._onDidUpdate.dispose();

await Promise.all([
this._cnPanelMap.dispose(),
this._cnVariableMap.dispose(),
]);
};

/**
Expand Down
15 changes: 14 additions & 1 deletion src/services/SerializedKeyMap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import type { Disposable } from '../types';
import { isDisposable } from '../util';

/**
* Base class for Maps that need to store their keys as serialized string values
Expand Down Expand Up @@ -42,8 +43,20 @@ export abstract class SerializedKeyMap<TKey, TValue> implements Disposable {
}

dispose = async (): Promise<void> => {
this._map.clear();
this._onDidChange.dispose();

const promises = [...this._map.values()];
this._map.clear();

const disposing = promises.map(async maybePromise => {
// If value is a Promise, it has to be resolved before it can be disposed.
const resolved = await maybePromise;
if (isDisposable(resolved)) {
await resolved.dispose();
}
});

await Promise.all(disposing);
};

get(key: TKey): TValue | undefined {
Expand Down
12 changes: 10 additions & 2 deletions src/services/ServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,21 @@ export class ServerManager implements IServerManager {

canStartServer: boolean;

async dispose(): Promise<void> {
dispose = async (): Promise<void> => {
this._onDidConnect.dispose();
this._onDidDisconnect.dispose();
this._onDidLoadConfig.dispose();
this._onDidServerStatusChange.dispose();
this._onDidRegisterEditor.dispose();
this._onDidUpdate.dispose();
}

await Promise.all([
this._connectionMap.dispose(),
this._serverMap.dispose(),
this._uriConnectionsMap.dispose(),
this._workerURLToServerURLMap.dispose(),
]);
};

loadServerConfig = async (): Promise<void> => {
// We want to keep any existing managed servers that aren't overridden by
Expand Down Expand Up @@ -243,6 +250,7 @@ export class ServerManager implements IServerManager {
this._onDidUpdate.fire();

if (!(await connection.initSession())) {
connection.dispose();
this._connectionMap.delete(serverUrl);
return null;
}
Expand Down
15 changes: 1 addition & 14 deletions src/services/cache/ByURLAsyncCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import type { IAsyncCacheService } from '../../types';
import { isDisposable } from '../../util';
import { URLMap } from '../URLMap';

/**
Expand Down Expand Up @@ -37,18 +36,6 @@ export class ByURLAsyncCache<TValue>

dispose = async (): Promise<void> => {
this._onDidInvalidate.dispose();

const promises = [...this._promiseMap.values()];
this._promiseMap.clear();

// Values have to be resolved before they can be disposed.
const disposing = promises.map(async promise => {
const resolved = await promise;
if (isDisposable(resolved)) {
await resolved.dispose();
}
});

await Promise.all(disposing);
await this._promiseMap.dispose();
};
}

0 comments on commit 9603cf9

Please sign in to comment.