1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.writableFork = void 0;
|
4 | const stream_1 = require("stream");
|
5 | const __1 = require("../..");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function writableFork(chains, opt) {
|
15 | const readables = [];
|
16 | const allChainsDone = Promise.all(chains.map(async (chain) => {
|
17 | const readable = (0, __1.readableCreate)();
|
18 | readables.push(readable);
|
19 | return await (0, __1._pipeline)([readable, ...chain]);
|
20 | })).catch(err => {
|
21 | console.error(err);
|
22 | throw err;
|
23 | });
|
24 | return new stream_1.Writable({
|
25 | objectMode: true,
|
26 | ...opt,
|
27 | write(chunk, _, cb) {
|
28 |
|
29 |
|
30 | readables.forEach(readable => readable.push(chunk));
|
31 | cb();
|
32 | },
|
33 | async final(cb) {
|
34 | try {
|
35 |
|
36 | readables.forEach(readable => readable.push(null));
|
37 | console.log(`writableFork.final is waiting for all chains to be done`);
|
38 | await allChainsDone;
|
39 | console.log(`writableFork.final all chains done`);
|
40 | cb();
|
41 | }
|
42 | catch (err) {
|
43 | cb(err);
|
44 | }
|
45 | },
|
46 | });
|
47 | }
|
48 | exports.writableFork = writableFork;
|