Skip to content

Commit b81a413

Browse files
authored
chore: use inversify (#18)
Signed-off-by: Philippe Martin <[email protected]>
1 parent ac15499 commit b81a413

File tree

7 files changed

+193
-154
lines changed

7 files changed

+193
-154
lines changed

packages/extension/src/dashboard-extension.ts

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

1919
import type { WebviewPanel, ExtensionContext, KubeconfigUpdateEvent } from '@podman-desktop/api';
20-
import { kubernetes, Uri, window } from '@podman-desktop/api';
20+
import { env, kubernetes, Uri, window } from '@podman-desktop/api';
2121

2222
import { RpcExtension } from '/@common/rpc/rpc';
2323

@@ -26,9 +26,15 @@ import { ContextsManager } from './manager/contexts-manager';
2626
import { existsSync } from 'node:fs';
2727
import { KubeConfig } from '@kubernetes/client-node';
2828
import { ContextsStatesDispatcher } from './manager/contexts-states-dispatcher';
29+
import { InversifyBinding } from './inject/inversify-binding';
30+
import type { Container } from 'inversify';
2931

3032
export class DashboardExtension {
33+
#container: Container | undefined;
34+
#inversifyBinding: InversifyBinding | undefined;
35+
3136
#extensionContext: ExtensionContext;
37+
3238
#contextsManager: ContextsManager;
3339
#contextsStatesDispatcher: ContextsStatesDispatcher;
3440

@@ -37,17 +43,21 @@ export class DashboardExtension {
3743
}
3844

3945
async activate(): Promise<void> {
46+
const telemetryLogger = env.createTelemetryLogger();
47+
4048
const panel = await this.createWebview();
4149

4250
// Register webview communication for this webview
4351
const rpcExtension = new RpcExtension(panel.webview);
4452
rpcExtension.init();
4553
this.#extensionContext.subscriptions.push(rpcExtension);
4654

47-
this.#contextsManager = new ContextsManager();
48-
this.#contextsStatesDispatcher = new ContextsStatesDispatcher(this.#contextsManager, rpcExtension);
49-
5055
const now = performance.now();
56+
this.#inversifyBinding = new InversifyBinding(rpcExtension, this.extensionContext, telemetryLogger);
57+
this.#container = await this.#inversifyBinding.initBindings();
58+
59+
this.#contextsManager = await this.#container.getAsync(ContextsManager);
60+
this.#contextsStatesDispatcher = await this.#container.getAsync(ContextsStatesDispatcher);
5161

5262
const afterFirst = performance.now();
5363

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**********************************************************************
2+
* Copyright (C) 2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import 'reflect-metadata';
20+
21+
import type { ExtensionContext as PodmanDesktopExtensionContext, TelemetryLogger } from '@podman-desktop/api';
22+
import { Container } from 'inversify';
23+
24+
import { ExtensionContextSymbol, TelemetryLoggerSymbol } from '/@/inject/symbol';
25+
import { RpcExtension } from '/@common/rpc/rpc';
26+
import { managersModule } from '/@/manager/_manager-module';
27+
28+
export class InversifyBinding {
29+
#container: Container | undefined;
30+
31+
readonly #rpcExtension: RpcExtension;
32+
33+
readonly #extensionContext: PodmanDesktopExtensionContext;
34+
35+
readonly #telemetryLogger: TelemetryLogger;
36+
37+
constructor(
38+
rpcExtension: RpcExtension,
39+
extensionContext: PodmanDesktopExtensionContext,
40+
telemetryLogger: TelemetryLogger,
41+
) {
42+
this.#rpcExtension = rpcExtension;
43+
this.#extensionContext = extensionContext;
44+
this.#telemetryLogger = telemetryLogger;
45+
}
46+
47+
public async initBindings(): Promise<Container> {
48+
this.#container = new Container();
49+
50+
this.#container.bind(RpcExtension).toConstantValue(this.#rpcExtension);
51+
this.#container.bind(ExtensionContextSymbol).toConstantValue(this.#extensionContext);
52+
this.#container.bind(TelemetryLoggerSymbol).toConstantValue(this.#telemetryLogger);
53+
54+
await this.#container.load(managersModule);
55+
56+
return this.#container;
57+
}
58+
59+
async dispose(): Promise<void> {
60+
if (this.#container) {
61+
await this.#container.unbindAll();
62+
}
63+
}
64+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**********************************************************************
2+
* Copyright (C) 2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
export const ExtensionContextSymbol = Symbol.for('ExtensionContext');
20+
21+
export const TelemetryLoggerSymbol = Symbol.for('TelemetryLogger');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**********************************************************************
2+
* Copyright (C) 2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import { ContainerModule } from 'inversify';
20+
21+
import { ContextsManager } from './contexts-manager';
22+
import { ContextsStatesDispatcher } from './contexts-states-dispatcher';
23+
24+
const managersModule = new ContainerModule(options => {
25+
options.bind<ContextsManager>(ContextsManager).toSelf().inSingletonScope();
26+
options.bind<ContextsStatesDispatcher>(ContextsStatesDispatcher).toSelf().inSingletonScope();
27+
});
28+
29+
export { managersModule };

packages/extension/src/manager/contexts-manager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import type { CacheUpdatedEvent, OfflineEvent, ResourceInformer } from '/@/types
4848
import { RoutesResourceFactory } from '/@/resources/routes-resource-factory.js';
4949
import { SecretsResourceFactory } from '/@/resources/secrets-resource-factory.js';
5050
import { ServicesResourceFactory } from '/@/resources/services-resource-factory.js';
51+
import { injectable } from 'inversify';
5152

5253
const HEALTH_CHECK_TIMEOUT_MS = 5_000;
5354

@@ -59,6 +60,7 @@ const HEALTH_CHECK_TIMEOUT_MS = 5_000;
5960
*
6061
* ContextsManager exposes the current state of the health checkers, permission checkers and informers.
6162
*/
63+
@injectable()
6264
export class ContextsManager {
6365
#resourceFactoryHandler: ResourceFactoryHandler;
6466
#dispatcher: ContextsDispatcher;

0 commit comments

Comments
 (0)