Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Jul 11, 2012
0 parents commit 525fefd
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
69 changes: 69 additions & 0 deletions index.js
@@ -0,0 +1,69 @@
var Stream = require('stream')

/*
was gonna use through for this,
but it does not match quite right,
because you need a seperate pause
mechanism for the readable and writable
sides.
*/

module.exports = function () {
var buffer = [], ended = false
var stream = new Stream()
stream.writable = stream.readable = true
stream.paused = false

stream.write = function (data) {{
if(!this.paused)
this.emit('data', data)
else
buffer.push(data)
return !(this.paused || buffer.length)
}

stream.end = function (data) {
if(data) this.write(data)
this.ended = true
this.once('drain', function () {
stream.emit('end')
process.nextTick(stream.destroy.bind(stream))
})
this.drain()
})

stream.drain = function () {
if(!buffer.length) return
while(!this.paused && buffer.length)
this.emit('data', buffer.shift())
//if the buffer has emptied. emit drain.
if(!buffer.length) {
this.emit('drain')
}
}

stream.resume = function () {
//this is where I need pauseRead, and pauseWrite.
//here the reading side is unpaused,
//but the writing side may still be paused.
//the whole buffer might not empity at once.
//it might pause again.
//the stream should never emit data inbetween pause()...resume()
//and write should return !buffer.length
this.paused = false
this.drain() //will emit drain if buffer empties.
}

stream.destroy = function () {
if(destroyed) return
destroyed = ended = true
buffer.length = 0
this.emit('close')
})

stream.pause = function () {
stream.paused = true
}

return stream
}
24 changes: 24 additions & 0 deletions package.json
@@ -0,0 +1,24 @@
{
"name": "pause-stream",
"version": "0.0.0",
"description": "a ThroughStream that strictly buffers all readable events when paused.",
"main": "index.js",
"directories": {
"test": "test"
},
"devDependencies": {
"stream-spec": "~0.2.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/dominictarr/pause-stream.git"
},
"keywords": [
"stream", "pipe", "pause", "drain", "buffer"
],
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)",
"license": ["MIT", "Apache2"]
}
24 changes: 24 additions & 0 deletions readme.markdown
@@ -0,0 +1,24 @@
# PauseStream

This is a `Stream` that will strictly buffer when paused.
Connect it to anything you need buffered.

```
var pause = require('pause-stream')();
pause.pause()
badlyBehavedStream.pipe(pause)
aLittleLater(function (err, data) {
pause.pipe(createAnotherStream(data))
pause.resume()
})
```

`PauseStream` will buffer whenever paused.
it will buffer when yau have called `pause` manually.
but also when it's downstream `dest.write()===false`.
it will attempt to drain the buffer when you call resume
or the downstream emits `'drain'`

0 comments on commit 525fefd

Please sign in to comment.