merge-readable 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +65 -18
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -2,34 +2,63 @@ const { Readable } = require('streamx')
2
2
 
3
3
  const PREMATURE_CLOSE = new Error('Premature close')
4
4
 
5
- function merge(stream, ...streams) {
6
- const all = Array.isArray(stream) ? [...stream, ...streams] : [stream, ...streams]
7
- const map = all.length && typeof all[all.length - 1] === 'function' ? all.pop() : null
5
+ function mergeAll(streams, opts = {}) {
8
6
  const output = new Readable({
9
- map
7
+ map: opts.map
10
8
  })
11
9
 
12
- let active = all.length
13
-
14
- if (active === 0) {
10
+ if (streams.length === 0) {
15
11
  output.end()
16
12
  return output
17
13
  }
18
14
 
19
- for (const source of all) {
20
- source.on('data', (data) => {
21
- output.push(data)
22
- })
15
+ if (opts.sequential) {
16
+ for (const s of streams) {
17
+ s.pause()
18
+ }
19
+
20
+ let index = 0
23
21
 
24
- source.on('end', () => {
25
- active--
26
- if (active === 0) {
22
+ function readNext() {
23
+ if (index >= streams.length) {
27
24
  output.push(null)
25
+ return
28
26
  }
29
- })
30
27
 
31
- source.on('error', onerror)
32
- source.on('close', onclose.bind(null, source))
28
+ const source = streams[index]
29
+
30
+ source.on('data', (data) => {
31
+ output.push(data)
32
+ })
33
+
34
+ source.on('end', () => {
35
+ index++
36
+ readNext()
37
+ })
38
+
39
+ source.on('error', onerror)
40
+ source.on('close', onclose.bind(null, source))
41
+
42
+ source.resume()
43
+ }
44
+
45
+ readNext()
46
+ } else {
47
+ let active = streams.length
48
+
49
+ for (const source of streams) {
50
+ source.on('data', (data) => {
51
+ output.push(data)
52
+ })
53
+ source.on('end', () => {
54
+ active--
55
+ if (active === 0) {
56
+ output.push(null)
57
+ }
58
+ })
59
+ source.on('error', onerror)
60
+ source.on('close', onclose.bind(null, source))
61
+ }
33
62
  }
34
63
 
35
64
  return output
@@ -41,11 +70,29 @@ function merge(stream, ...streams) {
41
70
  }
42
71
 
43
72
  function onerror(err) {
44
- for (const s of all) {
73
+ for (const s of streams) {
45
74
  s.destroy(err)
46
75
  }
47
76
  output.destroy(err)
48
77
  }
49
78
  }
50
79
 
80
+ function getArgs(stream, ...streams) {
81
+ const all = Array.isArray(stream) ? [...stream, ...streams] : [stream, ...streams]
82
+ const map = all.length && typeof all[all.length - 1] === 'function' ? all.pop() : null
83
+
84
+ return { all, map }
85
+ }
86
+
87
+ function mergeSequential(stream, ...streams) {
88
+ const { all, map } = getArgs(stream, ...streams)
89
+ return mergeAll(all, { map, sequential: true })
90
+ }
91
+
92
+ function merge(stream, ...streams) {
93
+ const { all, map } = getArgs(stream, ...streams)
94
+ return mergeAll(all, { map })
95
+ }
96
+
51
97
  module.exports = merge
98
+ module.exports.mergeSequential = mergeSequential
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "merge-readable",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "merge-readable",
5
5
  "main": "index.js",
6
6
  "exports": {