Comparing version 0.1.5 to 0.2.0
12
index.js
@@ -51,8 +51,8 @@ /* Copyright 2015, Wang Wenlin */ | ||
var q_ = new Array(arguments.length); | ||
var readable_ = []; | ||
var reader_ = []; | ||
for (var i in arguments) q_[i] = arguments[i]; | ||
chan.ctor_ = Channel; | ||
chan.poll = poll; | ||
chan.read = function () { return q_.shift(); } | ||
chan.wait = wait; | ||
return chan; | ||
@@ -68,8 +68,8 @@ | ||
q_.push(msg); | ||
while (readable_.length) process.nextTick(readable_.shift()); | ||
while (reader_.length) process.nextTick(reader_.shift()); | ||
} | ||
function poll(cb) { | ||
function wait(cb) { | ||
if (q_.length) return process.nextTick(cb); | ||
return readable_.push(cb); | ||
return reader_.push(cb); | ||
} | ||
@@ -103,3 +103,3 @@ } | ||
var msg = runq.read(); | ||
if (!msg) return runq.poll(loop); | ||
if (!msg) return runq.wait(loop); | ||
var iter = next(msg); | ||
@@ -106,0 +106,0 @@ if (iter.done) return; |
{ | ||
"name": "gojs", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "go.js, Golang like channels and go.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,7 +13,16 @@ /* Copyright 2015, Wang Wenlin */ | ||
describe('Channel', function () { | ||
it('can write message and poll to read', function (done) { | ||
it('can write message and read', function () { | ||
var chan = Channel(); | ||
chan(null, 1); | ||
chan.poll(function () { | ||
var msg = chan.read(); | ||
assert.equal(msg[1], 1); | ||
assert(!chan.read()); | ||
}); | ||
it('can write message and wait to read', function (done) { | ||
var chan = Channel(); | ||
chan(null, 1); | ||
chan.wait(function () { | ||
var msg = chan.read(); | ||
@@ -24,3 +33,15 @@ assert.equal(msg[1], 1); | ||
}); | ||
}) | ||
}); | ||
it('can wait to read and write message later', function (done) { | ||
var chan = Channel(); | ||
chan.wait(function () { | ||
var msg = chan.read(); | ||
assert.equal(msg[1], 1); | ||
assert(!chan.read()); | ||
return done(); | ||
}); | ||
chan(null, 1); | ||
}); | ||
}); | ||
@@ -27,0 +48,0 @@ |
12057
351