Skip to content

Commit 40d97c3

Browse files
authored
refactor: using enum for message channel (#484)
* refactor: using enum for message channel Signed-off-by: axel7083 <[email protected]> * refactor: rename MESSAGES to Messages Signed-off-by: axel7083 <[email protected]> * fix: rebase with proper enum message Signed-off-by: axel7083 <[email protected]> --------- Signed-off-by: axel7083 <[email protected]>
1 parent b76f906 commit 40d97c3

19 files changed

+49
-48
lines changed

packages/backend/src/managers/applicationManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import type { LocalRepositoryRegistry } from '../registries/LocalRepositoryRegis
4040
import { LABEL_MODEL_ID, LABEL_MODEL_PORTS } from './playground';
4141
import type { ApplicationState } from '@shared/src/models/IApplicationState';
4242
import type { PodmanConnection } from './podmanConnection';
43-
import { MSG_APPLICATIONS_STATE_UPDATE } from '@shared/Messages';
43+
import { Messages } from '@shared/Messages';
4444
import type { CatalogManager } from './catalogManager';
4545
import { ApplicationRegistry } from '../registries/ApplicationRegistry';
4646
import type { TaskRegistry } from '../registries/TaskRegistry';
@@ -91,7 +91,7 @@ export class ApplicationManager extends Publisher<ApplicationState[]> {
9191
private telemetry: TelemetryLogger,
9292
private localRepositories: LocalRepositoryRegistry,
9393
) {
94-
super(webview, MSG_APPLICATIONS_STATE_UPDATE, () => this.getApplicationsState());
94+
super(webview, Messages.MSG_APPLICATIONS_STATE_UPDATE, () => this.getApplicationsState());
9595
this.#applications = new ApplicationRegistry<ApplicationState>();
9696
}
9797

packages/backend/src/managers/catalogManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import path from 'node:path';
2121
import defaultCatalog from '../ai.json';
2222
import type { Recipe } from '@shared/src/models/IRecipe';
2323
import type { ModelInfo } from '@shared/src/models/IModelInfo';
24-
import { MSG_NEW_CATALOG_STATE } from '@shared/Messages';
24+
import { Messages } from '@shared/Messages';
2525
import { type Disposable, type Webview } from '@podman-desktop/api';
2626
import { JsonWatcher } from '../utils/JsonWatcher';
2727
import { Publisher } from '../utils/Publisher';
@@ -34,7 +34,7 @@ export class CatalogManager extends Publisher<Catalog> implements Disposable {
3434
webview: Webview,
3535
private appUserDirectory: string,
3636
) {
37-
super(webview, MSG_NEW_CATALOG_STATE, () => this.getCatalog());
37+
super(webview, Messages.MSG_NEW_CATALOG_STATE, () => this.getCatalog());
3838
// We start with an empty catalog, for the methods to work before the catalog is loaded
3939
this.catalog = {
4040
categories: [],

packages/backend/src/managers/inference/inferenceManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
LABEL_INFERENCE_SERVER,
3434
} from '../../utils/inferenceUtils';
3535
import { Publisher } from '../../utils/Publisher';
36-
import { MSG_INFERENCE_SERVERS_UPDATE } from '@shared/Messages';
36+
import { Messages } from '@shared/Messages';
3737
import type { InferenceServerConfig } from '@shared/src/models/InferenceServerConfig';
3838
import type { ModelsManager } from '../modelsManager';
3939

@@ -52,7 +52,7 @@ export class InferenceManager extends Publisher<InferenceServer[]> implements Di
5252
private modelsManager: ModelsManager,
5353
private telemetry: TelemetryLogger,
5454
) {
55-
super(webview, MSG_INFERENCE_SERVERS_UPDATE, () => this.getServers());
55+
super(webview, Messages.MSG_INFERENCE_SERVERS_UPDATE, () => this.getServers());
5656
this.#servers = new Map<string, InferenceServer>();
5757
this.#disposables = [];
5858
this.#initialized = false;

packages/backend/src/managers/modelsManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type { LocalModelInfo } from '@shared/src/models/ILocalModelInfo';
2020
import fs from 'fs';
2121
import * as path from 'node:path';
2222
import { type Webview, fs as apiFs, type Disposable } from '@podman-desktop/api';
23-
import { MSG_NEW_MODELS_STATE } from '@shared/Messages';
23+
import { Messages } from '@shared/Messages';
2424
import type { CatalogManager } from './catalogManager';
2525
import type { ModelInfo } from '@shared/src/models/IModelInfo';
2626
import * as podmanDesktopApi from '@podman-desktop/api';
@@ -75,7 +75,7 @@ export class ModelsManager implements Disposable {
7575
async sendModelsInfo() {
7676
const models = this.getModelsInfo();
7777
await this.webview.postMessage({
78-
id: MSG_NEW_MODELS_STATE,
78+
id: Messages.MSG_NEW_MODELS_STATE,
7979
body: models,
8080
});
8181
}

packages/backend/src/managers/playground.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
import path from 'node:path';
2929
import { getFreePort } from '../utils/ports';
3030
import type { QueryState } from '@shared/src/models/IPlaygroundQueryState';
31-
import { MSG_NEW_PLAYGROUND_QUERIES_STATE, MSG_PLAYGROUNDS_STATE_UPDATE } from '@shared/Messages';
31+
import { Messages } from '@shared/Messages';
3232
import type { PlaygroundState, PlaygroundStatus } from '@shared/src/models/IPlaygroundState';
3333
import type { ContainerRegistry } from '../registries/ContainerRegistry';
3434
import type { PodmanConnection } from './podmanConnection';
@@ -144,7 +144,7 @@ export class PlayGroundManager {
144144
sendPlaygroundState() {
145145
this.webview
146146
.postMessage({
147-
id: MSG_PLAYGROUNDS_STATE_UPDATE,
147+
id: Messages.MSG_PLAYGROUNDS_STATE_UPDATE,
148148
body: this.getPlaygroundsState(),
149149
})
150150
.catch((err: unknown) => {
@@ -366,7 +366,7 @@ export class PlayGroundManager {
366366
sendQueriesState(): void {
367367
this.webview
368368
.postMessage({
369-
id: MSG_NEW_PLAYGROUND_QUERIES_STATE,
369+
id: Messages.MSG_NEW_PLAYGROUND_QUERIES_STATE,
370370
body: this.getQueriesState(),
371371
})
372372
.catch((err: unknown) => {

packages/backend/src/registries/LocalRepositoryRegistry.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
***********************************************************************/
1818
import { beforeEach, expect, test, vi } from 'vitest';
1919
import { LocalRepositoryRegistry } from './LocalRepositoryRegistry';
20-
import { MSG_LOCAL_REPOSITORY_UPDATE } from '@shared/Messages';
20+
import { Messages } from '@shared/Messages';
2121
import type { Webview } from '@podman-desktop/api';
2222

2323
const mocks = vi.hoisted(() => ({
@@ -51,7 +51,7 @@ test('should notify webview when register', () => {
5151
} as unknown as Webview);
5252
localRepositories.register({ path: 'random', labels: { 'recipe-id': 'random' } });
5353
expect(mocks.postMessageMock).toHaveBeenNthCalledWith(1, {
54-
id: MSG_LOCAL_REPOSITORY_UPDATE,
54+
id: Messages.MSG_LOCAL_REPOSITORY_UPDATE,
5555
body: [{ path: 'random', labels: { 'recipe-id': 'random' } }],
5656
});
5757
});
@@ -64,7 +64,7 @@ test('should notify webview when unregister', () => {
6464
localRepositories.unregister('random');
6565

6666
expect(mocks.postMessageMock).toHaveBeenLastCalledWith({
67-
id: MSG_LOCAL_REPOSITORY_UPDATE,
67+
id: Messages.MSG_LOCAL_REPOSITORY_UPDATE,
6868
body: [],
6969
});
7070
});

packages/backend/src/registries/LocalRepositoryRegistry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
***********************************************************************/
1818
import type { LocalRepository } from '@shared/src/models/ILocalRepository';
19-
import { MSG_LOCAL_REPOSITORY_UPDATE } from '@shared/Messages';
19+
import { Messages } from '@shared/Messages';
2020
import { type Webview, Disposable } from '@podman-desktop/api';
2121
import { Publisher } from '../utils/Publisher';
2222

@@ -28,7 +28,7 @@ export class LocalRepositoryRegistry extends Publisher<LocalRepository[]> {
2828
private repositories: Map<string, LocalRepository> = new Map();
2929

3030
constructor(webview: Webview) {
31-
super(webview, MSG_LOCAL_REPOSITORY_UPDATE, () => this.getLocalRepositories());
31+
super(webview, Messages.MSG_LOCAL_REPOSITORY_UPDATE, () => this.getLocalRepositories());
3232
}
3333

3434
register(localRepository: LocalRepository): Disposable {

packages/backend/src/registries/TaskRegistry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
***********************************************************************/
1818

1919
import type { Task, TaskState } from '@shared/src/models/ITask';
20-
import { MSG_TASKS_UPDATE } from '@shared/Messages';
20+
import { Messages } from '@shared/Messages';
2121
import type { Webview } from '@podman-desktop/api';
2222

2323
/**
@@ -140,7 +140,7 @@ export class TaskRegistry {
140140
private notify() {
141141
this.webview
142142
.postMessage({
143-
id: MSG_TASKS_UPDATE,
143+
id: Messages.MSG_TASKS_UPDATE,
144144
body: this.getTasks(),
145145
})
146146
.catch((err: unknown) => {

packages/backend/src/utils/Publisher.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { expect, test, vi } from 'vitest';
1919
import { Publisher } from './Publisher';
2020
import type { Webview } from '@podman-desktop/api';
21+
import { Messages } from '@shared/Messages';
2122

2223
test('ensure publisher properly use getter', async () => {
2324
const postMessageMock = vi.fn().mockResolvedValue(undefined);
@@ -26,14 +27,14 @@ test('ensure publisher properly use getter', async () => {
2627
{
2728
postMessage: postMessageMock,
2829
} as unknown as Webview,
29-
'dummyChannel',
30+
Messages.MSG_TASKS_UPDATE,
3031
getterMock,
3132
);
3233
publisher.notify();
3334

3435
await vi.waitFor(() => {
3536
expect(postMessageMock).toHaveBeenCalledWith({
36-
id: 'dummyChannel',
37+
id: Messages.MSG_TASKS_UPDATE,
3738
body: 'dummyValue',
3839
});
3940
});

packages/backend/src/utils/Publisher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
***********************************************************************/
1818
import type { Webview } from '@podman-desktop/api';
19+
import type { Messages } from '@shared/Messages';
1920

2021
export class Publisher<T> {
2122
constructor(
2223
private webview: Webview,
23-
private channel: string,
24+
private channel: Messages,
2425
private getter: () => T,
2526
) {}
2627

0 commit comments

Comments
 (0)