Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code size #4

Merged
merged 1 commit into from
Aug 21, 2024
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {spawn} from 'node:child_process';
import {lineIterator, combineAsyncIterables} from './utilities.js';
import {lineIterator, combineAsyncIterators} from './utilities.js';

export default function nanoSpawn(command, rawArguments = [], rawOptions = {}) {
const [commandArguments, {signal, timeout, nativeOptions}] = Array.isArray(rawArguments)
Expand Down Expand Up @@ -31,7 +31,7 @@ export default function nanoSpawn(command, rawArguments = [], rawOptions = {}) {

return Object.assign(promise, {
subprocess,
[Symbol.asyncIterator]: () => combineAsyncIterables(stdoutLines, stderrLines),
[Symbol.asyncIterator]: () => combineAsyncIterators(stdoutLines, stderrLines),
stdout: stdoutLines,
stderr: stderrLines,
});
Expand Down
11 changes: 1 addition & 10 deletions utilities.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
export function streamToIterable(stream) {
return {
[Symbol.asyncIterator]: stream[Symbol.asyncIterator].bind(stream),
};
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used.


export async function * combineAsyncIterables(iterable1, iterable2) {
const iterator1 = iterable1[Symbol.asyncIterator]();
const iterator2 = iterable2[Symbol.asyncIterator]();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js streams are both iterables and iterators.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name is no longer correct then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. 👍


export async function * combineAsyncIterators(iterator1, iterator2) {
while (true) {
// eslint-disable-next-line no-await-in-loop
const [result1, result2] = await Promise.all([
Expand Down