-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create a dedicated class for listening the registration of podman pro…
…vider
- Loading branch information
Showing
5 changed files
with
137 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import { type ContainerProviderConnection, type RegisterContainerConnectionEvent, provider } from '@podman-desktop/api'; | ||
|
||
type startupHandle = () => void; | ||
|
||
export class PodmanConnection { | ||
#firstFound = false; | ||
#connection: ContainerProviderConnection | undefined = undefined; | ||
|
||
#toExecuteAtStartup: startupHandle[] = []; | ||
|
||
init(): void { | ||
// In case the extension has not yet registered, we listen for new registrations | ||
// and retain the first started podman provider | ||
const disposable = provider.onDidRegisterContainerConnection((e: RegisterContainerConnectionEvent) => { | ||
if (e.connection.type !== 'podman' || e.connection.status() !== 'started') { | ||
return; | ||
} | ||
if (this.#firstFound) { | ||
return; | ||
} | ||
this.#firstFound = true; | ||
this.#connection = e.connection; | ||
for (const f of this.#toExecuteAtStartup) { | ||
f(); | ||
} | ||
this.#toExecuteAtStartup = []; | ||
disposable.dispose(); | ||
}); | ||
|
||
// In case at least one extension has already registered, we get one started podman provider | ||
const engines = provider | ||
.getContainerConnections() | ||
.filter(connection => connection.connection.type === 'podman') | ||
.filter(connection => connection.connection.status() === 'started'); | ||
if (engines.length > 0) { | ||
disposable.dispose(); | ||
this.#firstFound = true; | ||
this.#connection = engines[0].connection; | ||
} | ||
} | ||
|
||
// startupSubscribe registers f to be executed when a podman container provider | ||
// registers, or immediately if already registered | ||
startupSubscribe(f: startupHandle): void { | ||
if (this.#firstFound) { | ||
f(); | ||
} else { | ||
this.#toExecuteAtStartup.push(f); | ||
} | ||
} | ||
|
||
getConnection(): ContainerProviderConnection | undefined { | ||
return this.#connection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters