-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
27 lines (25 loc) · 757 Bytes
/
index.js
File metadata and controls
27 lines (25 loc) · 757 Bytes
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
var destroy = require('destroy')
var assert = require('assert')
module.exports = function (stream, fn) {
assert(stream._writableState, 'Only writable streams are supported.')
fn = fn || destroy
var write = stream.write
var called = false
stream.write = function () {
var val = write.apply(this, arguments)
if (val === false) maybeExecuteCallback()
return val
}
function maybeExecuteCallback() {
if (called) return
// needs to be on two ticks as the first tick is when `.needDrain=false` is set
process.nextTick(function () {
process.nextTick(function () {
if (called || !stream._writableState.needDrain) return
called = true
fn.call(stream, stream)
})
})
}
return stream
}