Skip to content

Commit

Permalink
feat: dispose extension resources on deactivate (#342)
Browse files Browse the repository at this point in the history
* feat: dispose extension resources

Signed-off-by: axel7083 <[email protected]>

* fix: linter&prettier

Signed-off-by: axel7083 <[email protected]>

---------

Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored Feb 15, 2024
1 parent 60aaa3e commit 65ba79e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
14 changes: 11 additions & 3 deletions packages/backend/src/managers/catalogManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import type { Category } from '@shared/src/models/ICategory';
import type { Recipe } from '@shared/src/models/IRecipe';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
import { MSG_NEW_CATALOG_STATE } from '@shared/Messages';
import { fs } from '@podman-desktop/api';
import type { Webview } from '@podman-desktop/api';
import { type Disposable, type Webview, fs } from '@podman-desktop/api';

export class CatalogManager {
export class CatalogManager implements Disposable {
private watchers: Map<string, Disposable> = new Map<string, Disposable>();
private catalog: Catalog;

constructor(
Expand All @@ -42,6 +42,10 @@ export class CatalogManager {
};
}

dispose(): void {
Array.from(this.watchers.values()).forEach(watcher => watcher.dispose());
}

public getCatalog(): Catalog {
return this.catalog;
}
Expand Down Expand Up @@ -98,7 +102,11 @@ export class CatalogManager {
}

watchCatalogFile(path: string) {
if (this.watchers.has(path)) throw new Error(`A watcher already exist for file ${path}.`);

const watcher = fs.createFileSystemWatcher(path);
this.watchers.set(path, watcher);

watcher.onDidCreate(async () => {
try {
const cat = await this.readAndAnalyzeCatalog(path);
Expand Down
9 changes: 7 additions & 2 deletions packages/backend/src/managers/modelsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import type { LocalModelInfo } from '@shared/src/models/ILocalModelInfo';
import fs from 'fs';
import * as path from 'node:path';
import { type Webview, fs as apiFs } from '@podman-desktop/api';
import { type Webview, fs as apiFs, type Disposable } from '@podman-desktop/api';
import { MSG_NEW_MODELS_STATE } from '@shared/Messages';
import type { CatalogManager } from './catalogManager';
import type { ModelInfo } from '@shared/src/models/IModelInfo';
Expand All @@ -40,7 +40,7 @@ interface DownloadModelFailureResult {
error: string;
}

export class ModelsManager {
export class ModelsManager implements Disposable {
#modelsDir: string;
#models: Map<string, ModelInfo>;
#watcher?: podmanDesktopApi.FileSystemWatcher;
Expand All @@ -56,6 +56,11 @@ export class ModelsManager {
this.#models = new Map();
}

dispose(): void {
this.#models.clear();
this.#watcher.dispose();
}

async loadLocalModels() {
this.catalogManager.getModels().forEach(m => this.#models.set(m.id, m));
const reloadLocalModels = async () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/backend/src/managers/podmanConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
type UpdateContainerConnectionEvent,
containerEngine,
type PodInfo,
type Disposable,
} from '@podman-desktop/api';

export type startupHandle = () => void;
Expand All @@ -31,7 +32,7 @@ export type podStartHandle = (pod: PodInfo) => void;
export type podStopHandle = (pod: PodInfo) => void;
export type podRemoveHandle = (podId: string) => void;

export class PodmanConnection {
export class PodmanConnection implements Disposable {
#firstFound = false;
#toExecuteAtStartup: startupHandle[] = [];
#toExecuteAtMachineStop: machineStopHandle[] = [];
Expand All @@ -40,12 +41,18 @@ export class PodmanConnection {
#toExecuteAtPodStop: podStopHandle[] = [];
#toExecuteAtPodRemove: podRemoveHandle[] = [];

#onEventDisposable: Disposable | undefined;

init(): void {
this.listenRegistration();
this.listenMachine();
this.watchPods();
}

dispose(): void {
this.#onEventDisposable?.dispose();
}

listenRegistration() {
// In case the extension has not yet registered, we listen for new registrations
// and retain the first started podman provider
Expand Down Expand Up @@ -111,7 +118,9 @@ export class PodmanConnection {
}

watchPods() {
containerEngine.onEvent(event => {
if (this.#onEventDisposable !== undefined) throw new Error('already watching pods.');

this.#onEventDisposable = containerEngine.onEvent(event => {
if (event.Type !== 'pod') {
return;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/backend/src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { Uri, window, env } from '@podman-desktop/api';
import type {
ExtensionContext,
TelemetryLogger,
WebviewOptions,
WebviewPanel,
WebviewPanelOnDidChangeViewStateEvent,
} from '@podman-desktop/api';
import { Uri, window, env } from '@podman-desktop/api';
import { RpcExtension } from '@shared/src/messages/MessageProxy';
import { StudioApiImpl } from './studio-api-impl';
import { ApplicationManager } from './managers/applicationManager';
Expand Down Expand Up @@ -167,6 +167,9 @@ export class Studio {

// Register the instance
this.rpcExtension.registerInstance<StudioApiImpl>(StudioApiImpl, this.studioApi);
this.#extensionContext.subscriptions.push(this.catalogManager);
this.#extensionContext.subscriptions.push(this.modelsManager);
this.#extensionContext.subscriptions.push(podmanConnection);
}

public async deactivate(): Promise<void> {
Expand Down

0 comments on commit 65ba79e

Please sign in to comment.