Skip to content

Commit 80ec2ac

Browse files
authored
fix: allow Iterable | AsyncIterable union input (#59)
Compatiblity fix - where the input cannot be separated between Iterable and AsyncIterable sources, update the types to return async generators. For best performance users should pass either Iterable or AsyncIterable, not a union of both.
1 parent 0033bfd commit 80ec2ac

File tree

17 files changed

+53
-48
lines changed

17 files changed

+53
-48
lines changed

packages/it-all/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
66
* Collects all values from an (async) iterable and returns them as an array
77
*/
88
function all <T> (source: Iterable<T>): T[]
9-
function all <T> (source: AsyncIterable<T>): Promise<T[]>
10-
function all <T> (source: AsyncIterable<T> | Iterable<T>): Promise<T[]> | T[] {
9+
function all <T> (source: Iterable<T> | AsyncIterable<T>): Promise<T[]>
10+
function all <T> (source: Iterable<T> | AsyncIterable<T>): Promise<T[]> | T[] {
1111
if (isAsyncIterable(source)) {
1212
return (async () => {
1313
const arr = []

packages/it-batch/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
77
* emits those things in fixed-sized batches
88
*/
99
function batch <T> (source: Iterable<T>, size?: number): Generator<T[], void, undefined>
10-
function batch <T> (source: AsyncIterable<T> | Iterable<T>, size?: number): AsyncGenerator<T[], void, undefined>
11-
function batch <T> (source: AsyncIterable<T> | Iterable<T>, size: number = 1): Generator<T[], void, undefined> | AsyncGenerator<T[], void, undefined> {
10+
function batch <T> (source: Iterable<T> | AsyncIterable<T>, size?: number): AsyncGenerator<T[], void, undefined>
11+
function batch <T> (source: Iterable<T> | AsyncIterable<T>, size: number = 1): Generator<T[], void, undefined> | AsyncGenerator<T[], void, undefined> {
1212
size = Number(size)
1313

1414
if (isAsyncIterable(source)) {

packages/it-batched-bytes/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface AsyncBatchedBytesOptions extends BatchedBytesOptions {
3535
* or the next event loop tick occurs, yield any bytes from the buffer.
3636
*/
3737
function batchedBytes (source: Iterable<Uint8Array | Uint8ArrayList>, options?: BatchedBytesOptions): Iterable<Uint8Array>
38-
function batchedBytes (source: AsyncIterable<Uint8Array | Uint8ArrayList>, options?: AsyncBatchedBytesOptions): AsyncIterable<Uint8Array>
38+
function batchedBytes (source: Iterable<Uint8Array | Uint8ArrayList> | AsyncIterable<Uint8Array | Uint8ArrayList>, options?: AsyncBatchedBytesOptions): AsyncIterable<Uint8Array>
3939
function batchedBytes (source: Iterable<Uint8Array | Uint8ArrayList> | AsyncIterable<Uint8Array | Uint8ArrayList>, options: AsyncBatchedBytesOptions = {}): AsyncIterable<Uint8Array> | Iterable<Uint8Array> {
4040
if (isAsyncIterable(source)) {
4141
return (async function * () {

packages/it-drain/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
77
* anything
88
*/
99
function drain (source: Iterable<unknown>): void
10-
function drain (source: AsyncIterable<unknown>): Promise<void>
11-
function drain (source: AsyncIterable<unknown> | Iterable<unknown>): Promise<void> | void {
10+
function drain (source: Iterable<unknown> | AsyncIterable<unknown>): Promise<void>
11+
function drain (source: Iterable<unknown> | AsyncIterable<unknown>): Promise<void> | void {
1212
if (isAsyncIterable(source)) {
1313
return (async () => {
1414
for await (const _ of source) { } // eslint-disable-line no-unused-vars,no-empty,@typescript-eslint/no-unused-vars

packages/it-first/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
77
* case returns `undefined`
88
*/
99
function first <T> (source: Iterable<T>): T | undefined
10-
function first <T> (source: AsyncIterable<T>): Promise<T | undefined>
11-
function first <T> (source: AsyncIterable<T> | Iterable<T>): Promise<T | undefined> | T | undefined {
10+
function first <T> (source: Iterable<T> | AsyncIterable<T>): Promise<T | undefined>
11+
function first <T> (source: Iterable<T> | AsyncIterable<T>): Promise<T | undefined> | T | undefined {
1212
if (isAsyncIterable(source)) {
1313
return (async () => {
1414
for await (const entry of source) { // eslint-disable-line no-unreachable-loop

packages/it-flat-batch/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
77
* returns an async iterable that emits those things in fixed-size batches
88
*/
99
function batch <T> (source: Iterable<T[]>, batchSize?: number): Generator<T[], void, undefined>
10-
function batch <T> (source: AsyncIterable<T[]>, batchSize?: number): AsyncGenerator<T[], void, undefined>
11-
function batch <T> (source: AsyncIterable<T[]> | Iterable<T[]>, batchSize: number = 1): AsyncGenerator<T[], void, undefined> | Generator<T[], void, undefined> {
10+
function batch <T> (source: Iterable<T[]> | AsyncIterable<T[]>, batchSize?: number): AsyncGenerator<T[], void, undefined>
11+
function batch <T> (source: Iterable<T[]> | AsyncIterable<T[]>, batchSize: number = 1): AsyncGenerator<T[], void, undefined> | Generator<T[], void, undefined> {
1212
let size = parseInt(`${batchSize}`)
1313

1414
if (isNaN(size) || size < 1) {

packages/it-foreach/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
99
*/
1010
function forEach <T> (source: Iterable<T>, fn: (thing: T) => Promise<void>): AsyncGenerator<T, void, undefined>
1111
function forEach <T> (source: Iterable<T>, fn: (thing: T) => void): Generator<T, void, undefined>
12-
function forEach <T> (source: AsyncIterable<T>, fn: (thing: T) => void | Promise<void>): AsyncGenerator<T, void, undefined>
13-
function forEach <T> (source: AsyncIterable<T> | Iterable<T>, fn: (thing: T) => void | Promise<void>): AsyncGenerator<T, void, undefined> | Generator<T, void, undefined> {
12+
function forEach <T> (source: Iterable<T> | AsyncIterable<T>, fn: (thing: T) => void | Promise<void>): AsyncGenerator<T, void, undefined>
13+
function forEach <T> (source: Iterable<T> | AsyncIterable<T>, fn: (thing: T) => void | Promise<void>): AsyncGenerator<T, void, undefined> | Generator<T, void, undefined> {
1414
if (isAsyncIterable(source)) {
1515
return (async function * () {
1616
for await (const thing of source) {

packages/it-last/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
77
* return `undefined`
88
*/
99
function last <T> (source: Iterable<T>): T | undefined
10-
function last <T> (source: AsyncIterable<T>): Promise<T | undefined>
11-
function last <T> (source: AsyncIterable<T> | Iterable<T>): Promise<T | undefined> | T | undefined {
10+
function last <T> (source: Iterable<T> | AsyncIterable<T>): Promise<T | undefined>
11+
function last <T> (source: Iterable<T> | AsyncIterable<T>): Promise<T | undefined> | T | undefined {
1212
if (isAsyncIterable(source)) {
1313
return (async () => {
1414
let res

packages/it-length/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
66
* Consumes the passed iterator and returns the number of items it contained
77
*/
88
function length (source: Iterable<unknown>): number
9-
function length (source: AsyncIterable<unknown>): Promise<number>
10-
function length (source: AsyncIterable<unknown> | Iterable<unknown>): Promise<number> | number {
9+
function length (source: Iterable<unknown> | AsyncIterable<unknown>): Promise<number>
10+
function length (source: Iterable<unknown> | AsyncIterable<unknown>): Promise<number> | number {
1111
if (isAsyncIterable(source)) {
1212
return (async () => {
1313
let count = 0

packages/it-map/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function isAsyncIterable <T> (thing: any): thing is AsyncIterable<T> {
1010
*/
1111
function map <I, O> (source: Iterable<I>, func: (val: I) => Promise<O>): AsyncGenerator<O, void, undefined>
1212
function map <I, O> (source: Iterable<I>, func: (val: I) => O): Generator<O, void, undefined>
13-
function map <I, O> (source: AsyncIterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined>
13+
function map <I, O> (source: AsyncIterable<I> | Iterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined>
1414
function map <I, O> (source: AsyncIterable<I> | Iterable<I>, func: (val: I) => O | Promise<O>): AsyncGenerator<O, void, undefined> | Generator<O, void, undefined> {
1515
if (isAsyncIterable(source)) {
1616
return (async function * () {

0 commit comments

Comments
 (0)