Skip to content

Commit

Permalink
feat(ContainerRegistry): start container event listener (#443)
Browse files Browse the repository at this point in the history
* feat(ContainerRegistry): start container event listener

Signed-off-by: axel7083 <[email protected]>

* fix: prettier & linter

Signed-off-by: axel7083 <[email protected]>

* fix: linter

Signed-off-by: axel7083 <[email protected]>

* test(ContainerRegistry): ensuring call in expected time

Signed-off-by: axel7083 <[email protected]>

---------

Signed-off-by: axel7083 <[email protected]>
  • Loading branch information
axel7083 authored Mar 8, 2024
1 parent 916e8b8 commit cbfad85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
45 changes: 43 additions & 2 deletions packages/backend/src/registries/ContainerRegistry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { expect, test, vi } from 'vitest';
import { beforeEach, expect, test, vi } from 'vitest';
import { ContainerRegistry } from './ContainerRegistry';
import type { ContainerJSONEvent } from '@podman-desktop/api';
import { type ContainerJSONEvent, EventEmitter } from '@podman-desktop/api';

const mocks = vi.hoisted(() => ({
onEventMock: vi.fn(),
Expand All @@ -26,6 +26,7 @@ const mocks = vi.hoisted(() => ({

vi.mock('@podman-desktop/api', async () => {
return {
EventEmitter: vi.fn(),
Disposable: {
create: mocks.DisposableCreateMock,
},
Expand All @@ -35,6 +36,20 @@ vi.mock('@podman-desktop/api', async () => {
};
});

beforeEach(() => {
const listeners: ((value: unknown) => void)[] = [];
const eventSubscriber = (listener: (value: unknown) => void) => {
listeners.push(listener);
};
const fire = (value: unknown) => {
listeners.forEach(listener => listener(value));
};
vi.mocked(EventEmitter).mockReturnValue({
event: eventSubscriber,
fire: fire,
} as unknown as EventEmitter<unknown>);
});

test('ContainerRegistry init', () => {
const registry = new ContainerRegistry();
registry.init();
Expand Down Expand Up @@ -121,3 +136,29 @@ test('ContainerRegistry subscriber disposed should not be called', () => {
// never should have been called
expect(subscribeMock).toHaveBeenCalledTimes(0);
});

test('ContainerRegistry should fire ContainerStart when container start', () => {
// Get the callback created by the ContainerRegistry
let callback: (event: ContainerJSONEvent) => void;
mocks.onEventMock.mockImplementation((method: (event: ContainerJSONEvent) => void) => {
callback = method;
});

// Create the ContainerRegistry and init
const registry = new ContainerRegistry();
registry.init();

const startListenerMock = vi.fn();
registry.onStartContainerEvent(startListenerMock);

// Generate a remove event
callback({ status: 'remove', id: 'random', type: 'container' });

expect(startListenerMock).not.toHaveBeenCalled();

// Call it a second time
callback({ status: 'start', id: 'random', type: 'container' });

// Our subscriber should only have been called once, the first, after it should have been removed.
expect(startListenerMock).toHaveBeenCalledOnce();
});
13 changes: 13 additions & 0 deletions packages/backend/src/registries/ContainerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ export type Subscriber = {
callback: (status: string) => void;
};

export interface ContainerStart {
id: string;
}

export class ContainerRegistry {
private count: number = 0;
private subscribers: Map<string, Subscriber[]> = new Map();

private readonly _onStartContainerEvent = new podmanDesktopApi.EventEmitter<ContainerStart>();
readonly onStartContainerEvent: podmanDesktopApi.Event<ContainerStart> = this._onStartContainerEvent.event;

init(): podmanDesktopApi.Disposable {
return podmanDesktopApi.containerEngine.onEvent(event => {
if (event.status === 'start') {
this._onStartContainerEvent.fire({
id: event.id,
});
}

if (this.subscribers.has(event.id)) {
this.subscribers.get(event.id).forEach(subscriber => subscriber.callback(event.status));

Expand Down

0 comments on commit cbfad85

Please sign in to comment.