-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
61 lines (49 loc) · 1.05 KB
/
index.js
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
53
54
55
56
57
58
59
60
61
/*!
* pause
* Copyright(c) 2012 TJ Holowaychuk
* Copyright(c) 2015 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Module exports.
* @public
*/
module.exports = pause
/**
* Pause the data events on a stream.
*
* @param {object} stream
* @public
*/
function pause (stream) {
var events = []
var onData = createEventListener('data', events)
var onEnd = createEventListener('end', events)
// buffer data
stream.on('data', onData)
// buffer end
stream.on('end', onEnd)
return {
end: function end () {
stream.removeListener('data', onData)
stream.removeListener('end', onEnd)
},
resume: function resume () {
this.end()
for (var i = 0; i < events.length; i++) {
stream.emit.apply(stream, events[i])
}
}
}
}
function createEventListener (name, events) {
return function onEvent () {
var args = new Array(arguments.length + 1)
args[0] = name
for (var i = 0; i < arguments.length; i++) {
args[i + 1] = arguments[i]
}
events.push(args)
}
}