Comparing version 0.0.5 to 0.1.0
16
index.js
@@ -11,3 +11,2 @@ /* Copyright 2015, Wang Wenlin */ | ||
// go(function* (chan) { | ||
// var ch1 = Channel(); | ||
// var ch2 = Channel(); | ||
@@ -18,5 +17,5 @@ // | ||
// | ||
// db1.query('SELECT 1 FROM dummy', then(ch1, function (res) { ch1(null, res[0]); })); | ||
// db1.query('SELECT 1 FROM dummy', then(ch2, function (res) { ch2(null, res[0]); })); | ||
// db2.query('SELECT 3', chan); | ||
// var r3 = yield ch1; | ||
// var r3 = yield ch2; | ||
// var r4 = yield; | ||
@@ -29,6 +28,9 @@ // | ||
// var ry = yield; | ||
// chan(null, rx[0] + ry[0]); | ||
// chan(null, rx[1] + ry[1]); | ||
// }); | ||
// yield; | ||
// | ||
// try { | ||
// var r5 = yield; | ||
// } catch (e) {} | ||
// | ||
// redis.get('k1', chan); | ||
@@ -94,2 +96,4 @@ // redis.hget('k2', chan); | ||
return chan; | ||
function next(msg) { | ||
@@ -125,3 +129,3 @@ if (msg[0]) { | ||
} | ||
} | ||
}; | ||
} | ||
@@ -128,0 +132,0 @@ |
{ | ||
"name": "gojs", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "go.js, Golang like channels and go.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha -R spec", | ||
"jshint": "jshint index.js test/ example/ || exit 0" | ||
}, | ||
@@ -9,0 +10,0 @@ "keywords": [ |
@@ -5,6 +5,10 @@ go.js, Golang like channels and go. | ||
```javascript | ||
var Channel = require('../').Channel; | ||
var go = require('../').go; | ||
var bind = require('../').bind; | ||
var then = require('../').then; | ||
// Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
// | ||
go(function* (chan) { | ||
var ch1 = Channel(); | ||
var ch2 = Channel(); | ||
@@ -15,5 +19,5 @@ | ||
db1.query('SELECT 1 FROM dummy', then(ch1, function (res) { ch1(null, res[0]); })); | ||
db1.query('SELECT 1 FROM dummy', then(ch2, function (res) { ch2(null, res[0]); })); | ||
db2.query('SELECT 3', chan); | ||
var r3 = yield ch1; | ||
var r3 = yield ch2; | ||
var r4 = yield; | ||
@@ -26,6 +30,9 @@ | ||
var ry = yield; | ||
chan(null, rx[0] + ry[0]); | ||
chan(null, rx[1] + ry[1]); | ||
}); | ||
yield; | ||
try { | ||
var r5 = yield; | ||
} catch (e) {} | ||
redis.get('k1', chan); | ||
@@ -36,2 +43,2 @@ redis.hget('k2', chan); | ||
}); | ||
``` | ||
``` |
166
test/test.js
@@ -1,1 +0,165 @@ | ||
var mocha = require('mocha'); | ||
/* Copyright 2015, Wang Wenlin */ | ||
var assert = require('assert'); | ||
var util = require('util'); | ||
// | ||
var Channel = require('../').Channel; | ||
var go = require('../').go; | ||
var bind = require('../').bind; | ||
var then = require('../').then; | ||
describe('go.js', function () { | ||
describe('Channel', function () { | ||
it('can write message and poll to read', function (done) { | ||
var chan = Channel(); | ||
chan(null, 1); | ||
chan.poll(function () { | ||
var msg = chan.read(); | ||
assert.equal(msg[1], 1); | ||
assert(!chan.read()); | ||
return done(); | ||
}); | ||
}) | ||
}); | ||
describe('go()', function () { | ||
it('only support generator', function () { | ||
go(function* () {}); | ||
assert.throws(function () { go(function () {}); }); | ||
}); | ||
it('addition args for generator', function (done) { | ||
go(function* (chan, done) { done(); }, done); | ||
}); | ||
it('yield from default chan', function (done) { | ||
go(function* (chan) { | ||
process.nextTick(function () { chan(null, 1); }); | ||
assert.equal((yield), 1); | ||
return done(); | ||
}); | ||
}); | ||
it('yield from default chan with multiple args', function (done) { | ||
go(function* (chan) { | ||
process.nextTick(function () { chan(null, 1, 2); }); | ||
var res = yield; | ||
assert(Array.isArray(res), 'multi-args message should be an array'); | ||
assert(res[0] === 1 && res[1] === 2); | ||
return done(); | ||
}); | ||
}); | ||
it('multiple message can be queued and yield one by one', function (done) { | ||
go(function* (chan) { | ||
process.nextTick(function () { chan(null, 1); }); | ||
process.nextTick(function () { chan(null, 2); }); | ||
assert((yield) === 1 && (yield) === 2); | ||
return done(); | ||
}); | ||
}); | ||
it('yield combined with async function call', function (done) { | ||
go(function* (chan) { | ||
var res = yield process.nextTick(function () { chan(null, 1); }); | ||
assert.equal(res, 1); | ||
return done(); | ||
}); | ||
}); | ||
it('throw in generator would crash without protects', function () { | ||
assert.throws(function () { | ||
go(function* () { throw Error('err'); }); | ||
}); | ||
assert.throws(function () { | ||
go(function* () { chan(null, Error('err')); yield; }); | ||
}); | ||
}); | ||
it('write error to default chan would throw by yield', function (done) { | ||
go(function* (chan) { | ||
try { | ||
yield; | ||
} catch (e) { | ||
return done(); | ||
} | ||
throw Error('yield not throw'); | ||
})(Error('err')); | ||
}); | ||
it('embed child go()', function (done) { | ||
go(function* (chan) { | ||
go(function* (ch2) { | ||
chan(null, 1); | ||
}); | ||
assert.equal((yield), 1); | ||
return done(); | ||
}); | ||
}); | ||
it('yield specific chan would make runloop to read from it', function (done) { | ||
go(function* (chan) { | ||
var ch2 = Channel(); | ||
process.nextTick(function () { chan(null, 1); }); | ||
process.nextTick(function () { ch2(null, 2); }); | ||
assert((yield) === 1 && (yield ch2) === 2); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
describe('bind()', function () { | ||
it('bind one param', function (done) { | ||
go(function* (chan) { | ||
bind(chan, 'b1')(null, 1); | ||
var res = yield; | ||
assert(Array.isArray(res)); | ||
assert(res[0] === 'b1'&& res[1] === 1); | ||
return done(); | ||
}); | ||
}); | ||
it('bind multiple params', function (done) { | ||
go(function* (chan) { | ||
bind(chan, 'b1', 'b2')(null, 1); | ||
var res = yield; | ||
assert(Array.isArray(res)); | ||
assert(res[0] === 'b1' && res[1] === 'b2' && res[2] === 1); | ||
return done(); | ||
}); | ||
}); | ||
it('binds would presence as `extra` on error', function (done) { | ||
go(function* (chan) { | ||
bind(chan, 'b1')(Error('err')); | ||
try { | ||
yield; | ||
} catch (e) { | ||
assert.equal(e.extra, 'b1'); | ||
return done(); | ||
} | ||
}); | ||
}); | ||
}); | ||
describe('then()', function () { | ||
it('split err from callbacks', function () { | ||
then(null, function (res) { | ||
assert.equal(res, 1); | ||
})(null, 1); | ||
then(function (e) { | ||
assert.equal(e.message, 'err'); | ||
})(Error('err')); | ||
}); | ||
it('support multiple params', function () { | ||
then(null, function (res, r2, r3) { | ||
assert.equal(res, 1); | ||
assert.equal(r2, 2); | ||
assert.equal(r3, 3); | ||
})(null, 1, 2, 3); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10698
7
304
1
41