Comparing version 0.2.3 to 0.3.1
/* Copyright 2015, Wang Wenlin */ | ||
"use strict"; | ||
@@ -8,2 +9,4 @@ var Channel = require('../').Channel; | ||
require('../').patchPromise(); | ||
// Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
@@ -13,14 +16,15 @@ // | ||
db.query('SELECT 1', ch); | ||
var r1 = yield ch; | ||
var rows = yield ch; | ||
request('http://www.google.com', ch); | ||
var r2 = yield ch; | ||
var [resp, body] = yield; | ||
redis.get('k1', ch); | ||
redis.hget('k2', ch); | ||
redis.hget('k1', ch); | ||
redis.get('k2', ch); | ||
var rk1 = yield; | ||
var rk2 = yield; | ||
console.log(rk1, rk2); | ||
db.queryPromise('SELECT 1').then(print).done(ch); | ||
var rp = yield ch; | ||
go(function* (ch2) { | ||
@@ -27,0 +31,0 @@ db.query('SELECT 1', bind(ch2, 'r3')); |
92
index.js
@@ -8,40 +8,6 @@ /* Copyright 2015, Wang Wenlin */ | ||
exports.then = then; | ||
exports.patchPromise = patchPromise; | ||
// Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
// | ||
// go(function* (ch) { | ||
// db.query('SELECT 1', ch); | ||
// var r1 = yield ch; | ||
// | ||
// request('http://www.google.com', ch); | ||
// var r2 = yield ch; | ||
// | ||
// redis.get('k1', ch); | ||
// redis.hget('k2', ch); | ||
// | ||
// var rk1 = yield; | ||
// var rk2 = yield; | ||
// console.log(rk1, rk2); | ||
// | ||
// go(function* (ch2) { | ||
// db.query('SELECT 1', bind(ch2, 'r3')); | ||
// db2.query('SELECT 3', bind(ch2, 'r4')); | ||
// var rx = yield; | ||
// var ry = yield; | ||
// ch(null, rx[1] + ry[1]); | ||
// }); | ||
// | ||
// try { | ||
// var rz = yield ch; | ||
// console.log(rz); | ||
// } catch (e) {} | ||
// | ||
// var ch3 = Channel(); | ||
// db.query('SELECT 1 FROM dummy', then(ch3, function (res) { ch3(null, res[0]); })); | ||
// db2.query('SELECT 3', ch); | ||
// | ||
// var r3 = yield ch3; | ||
// var r4 = yield; | ||
// console.log(r3, r4); | ||
// }); | ||
@@ -51,9 +17,10 @@ /** | ||
* @constructor | ||
* @param {Varargs} arg0 - Varargs of prequeued messages | ||
* @param {Varargs} arg - Varargs of prequeued messages | ||
* @returns {Channel} - new channel | ||
*/ | ||
function Channel(arg0) { | ||
var q_ = new Array(arguments.length); | ||
function Channel(arg) { | ||
var l = arguments.length; | ||
var q_ = new Array(l); | ||
var waitq_ = []; | ||
for (var i in arguments) q_[i] = arguments[i]; | ||
for (var i = 0; i < l; i++) q_[i] = arguments[i]; | ||
@@ -65,5 +32,6 @@ chan.ctor_ = Channel; | ||
function chan(arg0) { | ||
var args = new Array(arguments.length); | ||
for (var i in arguments) args[i] = arguments[i]; | ||
function chan(arg) { | ||
var l = arguments.length; | ||
var args = new Array(l); | ||
for (var i = 0; i < l; i++) args[i] = arguments[i]; | ||
return write(args); | ||
@@ -86,6 +54,6 @@ } | ||
* @param {Generator} goroutine - resumable goroutine | ||
* @param {Varargs} arg0 - Varargs forward to the goroutine | ||
* @param {Varargs} arg - Varargs forward to the goroutine | ||
* @returns {Channel} - channel binds on the goroutine | ||
*/ | ||
function go(goroutine, arg0) { | ||
function go(goroutine, arg) { | ||
var inst; | ||
@@ -98,3 +66,3 @@ var chan = Channel([]); | ||
} else if (arguments.length <= 2) { | ||
inst = goroutine(chan, arguments[1]); | ||
inst = goroutine(chan, arg); | ||
} else { | ||
@@ -134,6 +102,6 @@ var l = arguments.length; | ||
* @param {Channel} channel - target channel | ||
* @param {Varargs} bind0 - Varargs forward to channel | ||
* @param {Varargs} arg - Varargs binds on channel | ||
* @returns {Function(e, v)} - view of channel with binds | ||
*/ | ||
function bind(chan, bind0) { | ||
function bind(chan, arg) { | ||
var l = arguments.length; | ||
@@ -143,3 +111,3 @@ var binds = new Array(l-1); | ||
return function (err_, arg0, arg1) { | ||
return function (err_, arg, arg2) { | ||
if (err_) { | ||
@@ -149,5 +117,5 @@ err_.extra = (binds.length <= 1) ? binds[0] : binds; | ||
} else if (arguments.length <= 2) { | ||
chan.apply(null, [].concat(err_, binds, arg0)); | ||
chan.apply(null, [].concat(err_, binds, arg)); | ||
} else if (arguments.length <= 3) { | ||
chan.apply(null, [].concat(err_, binds, arg0, arg1)); | ||
chan.apply(null, [].concat(err_, binds, arg, arg2)); | ||
} else { | ||
@@ -163,15 +131,15 @@ var l = arguments.length; | ||
/** | ||
* Wrap standalone error and success callbacks into node.js' standard callback. | ||
* Wrap standalone error and success callbacks into node.js style callback. | ||
* @param {Function(e)} err - error channel or callback | ||
* @param {Function(v)} cb - success callback | ||
* @returns {Function(e, v)} - standard callback | ||
* @returns {Function(e, v)} - node.js style callback | ||
*/ | ||
function then(err, cb) { | ||
return function (err_, arg0, arg1) { | ||
return function (err_, arg, arg2) { | ||
if (err_) { | ||
err(err_); | ||
} else if (arguments.length <= 2) { | ||
cb(arg0); | ||
cb(arg); | ||
} else if (arguments.length <= 3) { | ||
cb(arg0, arg1); | ||
cb(arg, arg2); | ||
} else { | ||
@@ -185,1 +153,15 @@ var l = arguments.length; | ||
} | ||
/** | ||
* Patch ES6 Promise to add non-standard Promise#done() support | ||
* @param {Function(e, v)} cb - node.js style callback | ||
* @returns {Promise} - chaining promise | ||
*/ | ||
function patchPromise() { | ||
Promise.prototype.done = function (cb) { | ||
return this.then( | ||
function (val) { cb(null, val); }, | ||
function (err) { cb(err); } | ||
); | ||
}; | ||
} |
{ | ||
"name": "gojs", | ||
"version": "0.2.3", | ||
"version": "0.3.1", | ||
"description": "go.js, Golang like channels, goroutine and go.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,2 +10,4 @@ go.js, Golang like channels, goroutine and go. | ||
require('gojs').patchPromise(); | ||
// Ref: http://swannodette.github.io/2013/08/24/es6-generators-and-csp/ | ||
@@ -15,14 +17,15 @@ // | ||
db.query('SELECT 1', ch); | ||
var r1 = yield ch; | ||
var rows = yield ch; | ||
request('http://www.google.com', ch); | ||
var r2 = yield ch; | ||
var [resp, body] = yield; | ||
redis.get('k1', ch); | ||
redis.hget('k2', ch); | ||
redis.hget('k1', ch); | ||
redis.get('k2', ch); | ||
var rk1 = yield; | ||
var rk2 = yield; | ||
console.log(rk1, rk2); | ||
db.queryPromise('SELECT 1').then(print).done(ch); | ||
var rp = yield ch; | ||
go(function* (ch2) { | ||
@@ -29,0 +32,0 @@ db.query('SELECT 1', bind(ch2, 'r3')); |
/* Copyright 2015, Wang Wenlin */ | ||
"use strict"; | ||
@@ -11,2 +12,4 @@ var assert = require('assert'); | ||
require('../').patchPromise(); | ||
describe('go.js', function () { | ||
@@ -206,2 +209,26 @@ describe('Channel', function () { | ||
}); | ||
describe('patchPromise()', function () { | ||
it('merge resolve and reject callbacks into one', function (done) { | ||
var prms = new Promise(function (resolve, reject) { resolve(1); }); | ||
var prms2 = new Promise(function (resolve, reject) { reject(Error('err')); }); | ||
prms.done(function (e, val) { | ||
try { | ||
assert.equal(e, null); | ||
assert.equal(val, 1); | ||
} catch (e) { | ||
return done(e); | ||
} | ||
prms2.done(function (e, val) { | ||
try { | ||
assert.equal(e.message, 'err'); | ||
} catch (e) { | ||
return done(e); | ||
} | ||
}).then(done); | ||
}); | ||
}); | ||
}); | ||
}); |
13187
383
51