Skip to content

Commit

Permalink
feat: dispose extension resources
Browse files Browse the repository at this point in the history
Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 committed Feb 15, 2024
1 parent 4b65712 commit 92f09f7
Show file tree
Hide file tree
Showing 4 changed files with 34 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 @@ -20,7 +20,7 @@ import type { LocalModelInfo } from '@shared/src/models/ILocalModelInfo';
import fs from 'fs';
import * as https from 'node:https';
import * as path from 'node:path';

Check failure on line 22 in packages/backend/src/managers/modelsManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

Import "Disposable" is only used as types

Check failure on line 22 in packages/backend/src/managers/modelsManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Import "Disposable" is only used as types

Check failure on line 22 in packages/backend/src/managers/modelsManager.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

Import "Disposable" is only used as types
import { type Webview, fs as apiFs } from '@podman-desktop/api';
import { type Webview, fs as apiFs, 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 @@ -55,6 +55,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
6 changes: 5 additions & 1 deletion packages/backend/src/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type {
import {

Check failure on line 19 in packages/backend/src/studio.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

Imports "ExtensionContext", "TelemetryLogger", "WebviewOptions", "WebviewPanel" and "WebviewPanelOnDidChangeViewStateEvent" are only used as types

Check failure on line 19 in packages/backend/src/studio.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

Imports "ExtensionContext", "TelemetryLogger", "WebviewOptions", "WebviewPanel" and "WebviewPanelOnDidChangeViewStateEvent" are only used as types

Check failure on line 19 in packages/backend/src/studio.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

Imports "ExtensionContext", "TelemetryLogger", "WebviewOptions", "WebviewPanel" and "WebviewPanelOnDidChangeViewStateEvent" are only used as types
Disposable,

Check failure on line 20 in packages/backend/src/studio.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / windows-2022

'Disposable' is defined but never used

Check failure on line 20 in packages/backend/src/studio.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / ubuntu-22.04

'Disposable' is defined but never used

Check failure on line 20 in packages/backend/src/studio.ts

View workflow job for this annotation

GitHub Actions / linter, formatters and unit tests / macos-12

'Disposable' is defined but never used
ExtensionContext,
TelemetryLogger,
WebviewOptions,
Expand Down Expand Up @@ -161,6 +162,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 92f09f7

Please sign in to comment.