This repository has been archived by the owner on Nov 9, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 525fefd
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'` |