|
| 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 { RpcBrowser } from '/@common/rpc/rpc'; |
| 20 | + |
| 21 | +import { InversifyBinding } from './inject/inversify-binding'; |
| 22 | +import { IDisposable } from '/@common/types/disposable'; |
| 23 | +import { States } from './state/states'; |
| 24 | +import { StateObject } from './state/util/state-object.svelte'; |
| 25 | + |
| 26 | +export interface MainContext { |
| 27 | + states: States; |
| 28 | +} |
| 29 | + |
| 30 | +export class Main implements IDisposable { |
| 31 | + private disposables: IDisposable[] = []; |
| 32 | + |
| 33 | + async init(): Promise<MainContext> { |
| 34 | + const webViewApi = acquirePodmanDesktopApi(); |
| 35 | + |
| 36 | + const rpcBrowser: RpcBrowser = new RpcBrowser(window, webViewApi); |
| 37 | + |
| 38 | + const inversifyBinding = new InversifyBinding(rpcBrowser, webViewApi); |
| 39 | + const container = await inversifyBinding.initBindings(); |
| 40 | + |
| 41 | + // Grab all state object instances |
| 42 | + const stateObjectInstances = container.getAll<StateObject<unknown>>(StateObject); |
| 43 | + |
| 44 | + // Init all state object instances |
| 45 | + for (const stateObjectInstance of stateObjectInstances) { |
| 46 | + await stateObjectInstance.init(); |
| 47 | + } |
| 48 | + |
| 49 | + // Register all disposables |
| 50 | + const disposables = await container.getAllAsync<IDisposable>(IDisposable); |
| 51 | + this.disposables.push(...disposables); |
| 52 | + |
| 53 | + const mainContext: MainContext = { |
| 54 | + states: await container.getAsync<States>(States), |
| 55 | + }; |
| 56 | + |
| 57 | + return mainContext; |
| 58 | + } |
| 59 | + |
| 60 | + dispose(): void { |
| 61 | + for (const disposable of this.disposables) { |
| 62 | + disposable.dispose(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments