Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/completion-listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ const emitFakeCloseEvent = (command: FakeCommand, event?: Partial<CloseEvent>) =
const flushPromises = () => new Promise((resolve) => setTimeout(resolve, 0));

describe('listen', () => {
it('resolves when there are no commands', async () => {
const result = createController().listen([]);
await expect(result).resolves.toHaveLength(0);
});

it('completes only when commands emit a close event, returns close event', async () => {
const abortCtrl = new AbortController();
const result = createController('all').listen(commands, abortCtrl.signal);
Expand Down
5 changes: 5 additions & 0 deletions src/completion-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export class CompletionListener {
* Commands that didn't spawn are filtered out.
*/
listen(commands: Command[], abortSignal?: AbortSignal): Promise<CloseEvent[]> {
if (!commands.length) {
return Promise.resolve([]);
}

const abort =
abortSignal &&
Rx.fromEvent(abortSignal, 'abort', { once: true }).pipe(
Expand All @@ -112,6 +116,7 @@ export class CompletionListener {
Rx.race(command.close, abort.pipe(filter(() => command.state === 'stopped')))
: command.close,
);

return Rx.lastValueFrom(
Rx.combineLatest(closeStreams).pipe(
filter(() => commands.every((command) => command.state !== 'started')),
Expand Down