-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackpressure.js
More file actions
52 lines (41 loc) · 1.16 KB
/
backpressure.js
File metadata and controls
52 lines (41 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var _ = require('lodash')
var through = require('through2')
var exec = require('child_process').exec;
var streamify = require('stream-array')
var multimeter = require('multimeter');
var multi = multimeter(process);
process.stdout.write('\033c'); // Clear the console
var jobs = 20
var progress = {
fastStream: 1,
slowStream: 1
}
var fastStreamProgressBar = multi(0, 1, {
width : jobs,
solid : { text : '█'},
empty : { text : ' ' },
})
var slowStreamProgressBar = multi(0, 2, {
width : jobs,
solid : { text : '█'},
empty : { text : ' ' },
})
sourceStream = streamify(_.range(jobs))
var fastStream = through.obj({highWaterMark: 16}, function (obj, enc, next) {
var self = this
exec('sleep 1', function (err, stdout, stderr) {
fastStreamProgressBar.ratio(progress.fastStream++, jobs)
self.push(obj)
next()
})
})
var slowStream = through.obj({highWaterMark: 16}, function (obj, enc, next) {
var self = this
exec('sleep 2', function (err, stdout, stderr) {
slowStreamProgressBar.ratio(progress.slowStream++, jobs)
self.push(obj)
next()
})
})
sourceStream.pipe(fastStream).pipe(slowStream)
slowStream.resume()