Skip to content

Commit a8e9d9b

Browse files
committed
Add parallelForEach and transform stream utilities
1 parent 526331b commit a8e9d9b

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

registry/lib/streams.d.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
declare type AnyIterable<T> = Iterable<T> | AsyncIterable<T>
22
declare const Streams: {
3+
parallelForEach<T>(
4+
concurrency: number,
5+
func: (data: T) => Promise<void>,
6+
iterable: AsyncIterable<T>
7+
): Promise<void>
38
parallelMap<T, R>(
49
concurrency: number
510
): {
@@ -20,8 +25,26 @@ declare const Streams: {
2025
func: (data: T) => R | Promise<R>,
2126
iterable: AnyIterable<T>
2227
): AsyncIterableIterator<R>
28+
transform(concurrency: number): {
29+
<T, R>(
30+
func: (data: T) => R | Promise<R>,
31+
iterable: AnyIterable<T>
32+
): AsyncIterableIterator<R>
33+
<T, R>(
34+
func: (data: T) => R | Promise<R>
35+
): (iterable: AnyIterable<T>) => AsyncIterableIterator<R>
36+
}
37+
transform<T, R>(
38+
concurrency: number,
39+
func: (data: T) => R | Promise<R>
40+
): (iterable: AnyIterable<T>) => AsyncIterableIterator<R>
41+
transform<T, R>(
42+
concurrency: number,
43+
func: (data: T) => R | Promise<R>,
44+
iterable: AnyIterable<T>
45+
): AsyncIterableIterator<R>
2346
}
24-
declare namespace Promises {
47+
declare namespace Streams {
2548
export { AnyIterable }
2649
}
2750
export = Streams

registry/lib/streams.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { apply: ReflectApply } = Reflect
4+
35
let _streamingIterables
46
/*@__NO_SIDE_EFFECTS__*/
57
function getStreamingIterables() {
@@ -10,11 +12,26 @@ function getStreamingIterables() {
1012
}
1113

1214
/*@__NO_SIDE_EFFECTS__*/
13-
function parallelMap(concurrency, func) {
15+
async function parallelForEach(concurrency, func, iterable) {
16+
for await (const _ of parallelMap(concurrency, func, iterable)) {
17+
/* empty block */
18+
}
19+
}
20+
21+
/*@__NO_SIDE_EFFECTS__*/
22+
function parallelMap(...args) {
23+
const streamingIterables = getStreamingIterables()
24+
return ReflectApply(streamingIterables.parallelMap, undefined, args)
25+
}
26+
27+
/*@__NO_SIDE_EFFECTS__*/
28+
function transform(...args) {
1429
const streamingIterables = getStreamingIterables()
15-
return streamingIterables.parallelMap(concurrency, func)
30+
return ReflectApply(streamingIterables.transform, undefined, args)
1631
}
1732

1833
module.exports = {
19-
parallelMap
34+
parallelForEach,
35+
parallelMap,
36+
transform
2037
}

0 commit comments

Comments
 (0)