From c6f698024573c09d6cb6b0ff848703650b410076 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:22:54 +0100 Subject: [PATCH] chore(application): using publisher class Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- .../src/managers/applicationManager.spec.ts | 10 ++++---- .../src/managers/applicationManager.ts | 25 ++++++------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/packages/backend/src/managers/applicationManager.spec.ts b/packages/backend/src/managers/applicationManager.spec.ts index 367d56b81..afe3323ec 100644 --- a/packages/backend/src/managers/applicationManager.spec.ts +++ b/packages/backend/src/managers/applicationManager.spec.ts @@ -1171,7 +1171,7 @@ describe('pod detection', async () => { mocks.onMachineStopMock.mockImplementation((f: machineStopHandle) => { f(); }); - const sendApplicationStateSpy = vi.spyOn(manager, 'sendApplicationState').mockResolvedValue(); + const sendApplicationStateSpy = vi.spyOn(manager, 'notify').mockResolvedValue(); manager.adoptRunningApplications(); expect(sendApplicationStateSpy).toHaveBeenCalledOnce(); }); @@ -1190,7 +1190,7 @@ describe('pod detection', async () => { }, } as unknown as PodInfo); }); - const sendApplicationStateSpy = vi.spyOn(manager, 'sendApplicationState').mockResolvedValue(); + const sendApplicationStateSpy = vi.spyOn(manager, 'notify').mockResolvedValue(); manager.adoptRunningApplications(); expect(sendApplicationStateSpy).toHaveBeenCalledOnce(); }); @@ -1205,7 +1205,7 @@ describe('pod detection', async () => { kind: 'podman', } as unknown as PodInfo); }); - const sendApplicationStateSpy = vi.spyOn(manager, 'sendApplicationState').mockResolvedValue(); + const sendApplicationStateSpy = vi.spyOn(manager, 'notify').mockResolvedValue(); manager.adoptRunningApplications(); expect(sendApplicationStateSpy).not.toHaveBeenCalledOnce(); }); @@ -1236,7 +1236,7 @@ describe('pod detection', async () => { } as unknown as PodInfo); }, 1); }); - const sendApplicationStateSpy = vi.spyOn(manager, 'sendApplicationState').mockResolvedValue(); + const sendApplicationStateSpy = vi.spyOn(manager, 'notify').mockResolvedValue(); manager.adoptRunningApplications(); await new Promise(resolve => setTimeout(resolve, 10)); expect(sendApplicationStateSpy).toHaveBeenCalledTimes(2); @@ -1261,7 +1261,7 @@ describe('pod detection', async () => { f('pod-id-1'); }, 1); }); - const sendApplicationStateSpy = vi.spyOn(manager, 'sendApplicationState').mockResolvedValue(); + const sendApplicationStateSpy = vi.spyOn(manager, 'notify').mockResolvedValue(); manager.adoptRunningApplications(); await new Promise(resolve => setTimeout(resolve, 10)); expect(sendApplicationStateSpy).toHaveBeenCalledTimes(2); diff --git a/packages/backend/src/managers/applicationManager.ts b/packages/backend/src/managers/applicationManager.ts index f6b651373..7a1a09308 100644 --- a/packages/backend/src/managers/applicationManager.ts +++ b/packages/backend/src/managers/applicationManager.ts @@ -44,6 +44,7 @@ import { MSG_APPLICATIONS_STATE_UPDATE } from '@shared/Messages'; import type { CatalogManager } from './catalogManager'; import { ApplicationRegistry } from '../registries/ApplicationRegistry'; import type { TaskRegistry } from '../registries/TaskRegistry'; +import { Publisher } from '../utils/Publisher'; export const LABEL_RECIPE_ID = 'ai-studio-recipe-id'; export const LABEL_APP_PORTS = 'ai-studio-app-ports'; @@ -75,7 +76,7 @@ export interface ImageInfo { appName: string; } -export class ApplicationManager { +export class ApplicationManager extends Publisher { #applications: ApplicationRegistry; protectTasks: Set = new Set(); @@ -83,13 +84,14 @@ export class ApplicationManager { private appUserDirectory: string, private git: GitManager, private taskRegistry: TaskRegistry, - private webview: Webview, + webview: Webview, private podmanConnection: PodmanConnection, private catalogManager: CatalogManager, private modelsManager: ModelsManager, private telemetry: TelemetryLogger, private localRepositories: LocalRepositoryRegistry, ) { + super(webview, MSG_APPLICATIONS_STATE_UPDATE, () => this.getApplicationsState()); this.#applications = new ApplicationRegistry(); } @@ -613,7 +615,7 @@ export class ApplicationManager { } this.#applications.clear(); - this.sendApplicationState(); + this.notify(); }); this.podmanConnection.onPodStart((pod: PodInfo) => { @@ -658,7 +660,7 @@ export class ApplicationManager { return; } this.#applications.delete({ recipeId, modelId }); - this.sendApplicationState(); + this.notify(); const protect = this.protectTasks.has(pod.Id); if (!protect) { @@ -685,7 +687,7 @@ export class ApplicationManager { return; } this.#applications.delete({ recipeId, modelId }); - this.sendApplicationState(); + this.notify(); const protect = this.protectTasks.has(podId); if (!protect) { @@ -700,24 +702,13 @@ export class ApplicationManager { updateApplicationState(recipeId: string, modelId: string, state: ApplicationState): void { this.#applications.set({ recipeId, modelId }, state); - this.sendApplicationState(); + this.notify(); } getApplicationsState(): ApplicationState[] { return Array.from(this.#applications.values()); } - sendApplicationState() { - this.webview - .postMessage({ - id: MSG_APPLICATIONS_STATE_UPDATE, - body: this.getApplicationsState(), - }) - .catch((err: unknown) => { - console.error(`Something went wrong while emitting MSG_APPLICATIONS_STATE_UPDATE: ${String(err)}`); - }); - } - async deleteApplication(recipeId: string, modelId: string) { // clear any existing status / tasks related to the pair recipeId-modelId. this.taskRegistry.deleteByLabels({