1 | import { Writable } from 'stream'
|
2 | import { readableCreate, ReadableTyped, _pipeline } from '../..'
|
3 | import { TransformOptions, WritableTyped } from '../stream.model'
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export function writableFork<T>(
|
12 | chains: NodeJS.WritableStream[][],
|
13 | opt?: TransformOptions,
|
14 | ): WritableTyped<T> {
|
15 | const readables: ReadableTyped<T>[] = []
|
16 |
|
17 | const allChainsDone = Promise.all(
|
18 | chains.map(async chain => {
|
19 | const readable = readableCreate<T>()
|
20 | readables.push(readable)
|
21 |
|
22 | return await _pipeline([readable, ...chain])
|
23 | }),
|
24 | ).catch(err => {
|
25 | console.error(err)
|
26 | throw err
|
27 | })
|
28 |
|
29 | return new Writable({
|
30 | objectMode: true,
|
31 | ...opt,
|
32 | write(chunk: T, _encoding, cb) {
|
33 |
|
34 |
|
35 | readables.forEach(readable => readable.push(chunk))
|
36 |
|
37 | cb()
|
38 | },
|
39 | async final(cb) {
|
40 | try {
|
41 |
|
42 | readables.forEach(readable => readable.push(null))
|
43 |
|
44 | console.log(`writableFork.final is waiting for all chains to be done`)
|
45 | await allChainsDone
|
46 | console.log(`writableFork.final all chains done`)
|
47 | cb()
|
48 | } catch (err) {
|
49 | cb(err)
|
50 | }
|
51 | },
|
52 | })
|
53 | }
|