Skip to content

Commit

Permalink
added: debug opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Pasynok committed Nov 3, 2024
1 parent 5ee0def commit cb93454
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/compose/__tests__/up.debug.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CONTAINER_STATUS, createContainer, type AnyContainer } from '../../createContainer';
import { compose } from '../index';

test('up.debug', async () => {
const consoleLogSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});

const a = createContainer({ id: 'a', start: () => ({ api: null }) });
const b = createContainer({ id: 'b', dependsOn: [a], enable: () => false, start: () => ({ api: null }) });
const c = createContainer({ id: 'c', start: () => ({ api: null }) });

await compose.up([a, b, c], { debug: true });

expect(consoleLogSpy.mock.calls[0]).toStrictEqual([{ a: 'idle', b: 'idle', c: 'idle' }]);
expect(consoleLogSpy.mock.calls[1]).toStrictEqual([{ a: 'pending', b: 'idle', c: 'idle' }]);
expect(consoleLogSpy.mock.calls[2]).toStrictEqual([{ a: 'pending', b: 'idle', c: 'pending' }]);
expect(consoleLogSpy.mock.calls[3]).toStrictEqual([{ a: 'done', b: 'idle', c: 'pending' }]);
expect(consoleLogSpy.mock.calls[4]).toStrictEqual([{ a: 'done', b: 'idle', c: 'done' }]);
expect(consoleLogSpy.mock.calls[5]).toStrictEqual([{ a: 'done', b: 'off', c: 'done' }]);

consoleLogSpy.mockRestore();
});
6 changes: 5 additions & 1 deletion src/compose/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const statusIs = {
idle: (s: ContainerStatus) => s === CONTAINER_STATUS.idle,
};

const upFn = (containers: AnyContainer[]) => {
const upFn = (containers: AnyContainer[], config?: { debug?: boolean }) => {
const CONTAINER_IDS = new Set<string>();

for (const container of containers) {
Expand All @@ -42,6 +42,10 @@ const upFn = (containers: AnyContainer[]) => {
};
});

if (config?.debug) {
$result.watch((x) => console.debug(x.statuses));
}

for (const container of containers) {
const $strictDepsResolving: Store<ContainerStatus> = combine(
(container.dependsOn ?? []).map((d) => d.$status),
Expand Down

0 comments on commit cb93454

Please sign in to comment.