Skip to content

Commit

Permalink
A stupid example to poke at it, and fixed up some edge cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jan 26, 2010
1 parent 4f12cb3 commit 1e932a5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/stream.js
Expand Up @@ -12,13 +12,13 @@ function Stream () {
};
this.resume = function () {
buffer.paused = false;
flow(this, buffer);
if (!buffer.flowing && !buffer.paused) flow(this, buffer);
};
this.write = function (data) {
if (buffer.closed) throw new Error("Cannot write after EOF.");
if (data === null) buffer.closed = true;
buffer.push(data);
flow(this, buffer);
if (!buffer.flowing && !buffer.paused) flow(this, buffer);
};
this.close = function () {
if (buffer.closed) return;
Expand All @@ -27,7 +27,6 @@ function Stream () {
};

function flow (emitter, buffer) {
if (buffer.flowing || buffer.paused) return;
buffer.flowing = true;
if (buffer.length === 0) {
buffer.flowing = false;
Expand Down
51 changes: 51 additions & 0 deletions test/test.js
@@ -0,0 +1,51 @@
var Stream = require("../lib/stream"),
assert = require("assert"),
sys = require("sys");

// s1 | s2 | s3

var s1 = new Stream(),
s2 = new Stream(),
s3 = new Stream(),
out = "";
s1.addListener("data", function (chunk) { s2.write(chunk) });
s2.addListener("data", function (chunk) { s3.write(chunk) });
s3.addListener("data", function (chunk) { chunk && (out += chunk + " ") });

s2.addListener("drain", function () { out += "\n--DRAIN--\n" });

function message () {
out += "-1-";
s1.write("order");
s2.pause();
process.nextTick(function () { out += "-A-" });
setTimeout(function () { out += "-!-" });
out += "-2-";
s1.write("is");
process.nextTick(function () { out += "-B-" });
setTimeout(function () { out += "-@-" });
out += "-3-";
s1.write("guaranteed");
out += "-4-";
process.nextTick(function () { out += "-C-" });
setTimeout(function () { out += "-#-" });
s1.write("but");
out += "-5-";
process.nextTick(function () { out += "-D-" });
setTimeout(function () { out += "-$-" });
s1.write("NOT");
out += "-6-";
s1.write("synchronicity\n");
process.nextTick(function () { out += "-E-" });
setTimeout(function () { out += "-%-" });
}
message();
setTimeout(message);
setTimeout(function () { s1.close() }, 100);

setTimeout(function () { s2.resume() }, 500);



setTimeout(function () { sys.error(out) }, 1000);

0 comments on commit 1e932a5

Please sign in to comment.