Comparing version 1.0.5 to 1.0.6
1.0.6 / 2016-02-04 | ||
================== | ||
* fixed running actions concurrently. started writing tests | ||
1.0.5 / 2016-02-02 | ||
@@ -3,0 +8,0 @@ ================== |
40
index.js
@@ -24,2 +24,3 @@ /** | ||
this.state = bind(actions, state || {}) | ||
this.context = this | ||
this.queue = [] | ||
@@ -75,3 +76,3 @@ } | ||
}) | ||
return this | ||
return this.context || this | ||
} | ||
@@ -86,2 +87,4 @@ } else { | ||
child.queue = this.queue | ||
// save the root context | ||
child.context = this.context || this | ||
return child | ||
@@ -95,18 +98,27 @@ }) | ||
this.queue = [] | ||
var self = this | ||
return new Promise(function (success, failure) { | ||
// execute the queue serially | ||
function next(err, value) { | ||
if (err) return done(err) | ||
var fn = queue.shift() | ||
if (!fn) return done(null, value) | ||
fn(next) | ||
} | ||
function promise () { | ||
return new Promise(function (success, failure) { | ||
// execute the queue serially | ||
function next(err, value) { | ||
if (err) return done(err) | ||
var fn = queue.shift() | ||
if (!fn) return done(null, value) | ||
fn(next) | ||
} | ||
function done(err, value) { | ||
return err ? failure(err) : success(value) | ||
} | ||
function done(err, value) { | ||
self.running = false | ||
return err ? failure(err) : success(value) | ||
} | ||
next() | ||
}) | ||
next() | ||
}) | ||
} | ||
// either attach to the previous or start a new one | ||
return this.running | ||
? this.running.then(function () { return promise() }) | ||
: this.running = promise() | ||
} | ||
@@ -113,0 +125,0 @@ |
{ | ||
"name": "internal", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "internal queue for your public libraries and APIs", | ||
@@ -15,3 +15,5 @@ "main": "index.js", | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"mocha": "^2.4.5" | ||
}, | ||
"scripts": { | ||
@@ -18,0 +20,0 @@ "test": "make test" |
@@ -1,27 +0,27 @@ | ||
var Internal = require('../') | ||
// var Internal = require('../') | ||
var API = Internal({ | ||
echo(msg, fn) { | ||
this.test = 'test' | ||
this.hi(msg, fn) | ||
}, | ||
hi(msg, fn) { | ||
this.test2 = 'whatever!!' | ||
fn() | ||
}, | ||
redis: { | ||
message(msg, fn) { | ||
var self = this | ||
this.echo(msg, function() { | ||
console.log('here...') | ||
fn(null, msg + ':' + self.test + ':' + self.test2) | ||
}) | ||
} | ||
} | ||
}) | ||
// var API = Internal({ | ||
// echo(msg, fn) { | ||
// this.test = 'test' | ||
// this.hi(msg, fn) | ||
// }, | ||
// hi(msg, fn) { | ||
// this.test2 = 'whatever!!' | ||
// fn() | ||
// }, | ||
// redis: { | ||
// message(msg, fn) { | ||
// var self = this | ||
// this.echo(msg, function() { | ||
// console.log('here...') | ||
// fn(null, msg + ':' + self.test + ':' + self.test2) | ||
// }) | ||
// } | ||
// } | ||
// }) | ||
var api = API() | ||
// var api = API() | ||
api.redis.message('hi') | ||
.then(v => console.log(v)) | ||
.catch(e => console.log(e.stack)) | ||
// api.redis.message('hi') | ||
// .then(v => console.log(v)) | ||
// .catch(e => console.log(e.stack)) |
@@ -1,39 +0,39 @@ | ||
var Internal = require('..') | ||
var internal = Internal(require('./fixtures/actions')) | ||
// var Internal = require('..') | ||
// var internal = Internal(require('./fixtures/actions')) | ||
var actions1 = internal() | ||
var actions2 = internal() | ||
// var actions1 = internal() | ||
// var actions2 = internal() | ||
actions1 | ||
.connect('test') | ||
.create('hi') | ||
// actions1 | ||
// .connect('test') | ||
// .create('hi') | ||
actions1.window | ||
.localStorage | ||
.setItem('hi', 'value') | ||
.removeItem('ok') | ||
// actions1.window | ||
// .localStorage | ||
// .setItem('hi', 'value') | ||
// .removeItem('ok') | ||
actions1.window.location.href = 'test' | ||
actions1.window.location.href | ||
.then(res => { | ||
console.log(res) | ||
}) | ||
// actions1.window.location.href = 'test' | ||
// actions1.window.location.href | ||
// .then(res => { | ||
// console.log(res) | ||
// }) | ||
// actions1.then(res => actions2.connect(res).promise()) | ||
// actions1.query('hi') | ||
// // actions1.then(res => actions2.connect(res).promise()) | ||
// // actions1.query('hi') | ||
// actions2 | ||
// .connect('test2') | ||
// .create('hi2') | ||
// // actions2 | ||
// // .connect('test2') | ||
// // .create('hi2') | ||
// Promise.all([ | ||
// actions1.promise(), | ||
// actions2.promise() | ||
// ]) | ||
// .then(function() { | ||
// console.log('done!') | ||
// }) | ||
// .catch(function(e) { | ||
// console.log('e', e.stack) | ||
// }) | ||
// // Promise.all([ | ||
// // actions1.promise(), | ||
// // actions2.promise() | ||
// // ]) | ||
// // .then(function() { | ||
// // console.log('done!') | ||
// // }) | ||
// // .catch(function(e) { | ||
// // console.log('e', e.stack) | ||
// // }) |
@@ -8,28 +8,120 @@ /** | ||
var API = Internal({ | ||
echo(msg, time, fn) { | ||
setTimeout(function() { | ||
if (msg === 'b') { | ||
return fn(new Error('no b\'s!')) | ||
/** | ||
* Tests | ||
*/ | ||
describe('Internal', function() { | ||
it('should ensure serial order even when running concurrently', function(done) { | ||
var order = [] | ||
var API = Internal({ | ||
echo: function (id, wait, fn) { | ||
setTimeout(function() { | ||
order.push(id) | ||
fn(null, id) | ||
}, wait) | ||
} | ||
fn(null, msg) | ||
}, time) | ||
} | ||
}) | ||
}) | ||
var api = API() | ||
var api = API() | ||
api.echo('a', 500) | ||
.echo('c', 1000) | ||
.then(function (v) { | ||
console.log('c', v) | ||
assert.equal(v, 'c') | ||
api | ||
.echo(1, 200) | ||
.then((id) => assert.equal(id, 1)) | ||
.catch(done) | ||
api | ||
.echo(2, 100) | ||
.then((id) => { | ||
assert.equal(id, 2) | ||
assert.equal('1,2', order.join(',')) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
api.echo('b', 1000) | ||
.then(function (v) { | ||
console.log('b', v) | ||
assert.equal(v, 'b') | ||
it('should support a shared context', function(done) { | ||
var API = Internal({ | ||
echo(msg, fn) { | ||
this.a = 'echo' | ||
this.hi(msg, fn) | ||
}, | ||
hi(msg, fn) { | ||
this.b = 'hi' | ||
fn() | ||
}, | ||
redis: { | ||
message(msg, fn) { | ||
var self = this | ||
this.c = 'redis' | ||
this.echo(msg, function() { | ||
fn(null, msg + ':' + self.a + ':' + self.b + ':' + self.c) | ||
}) | ||
} | ||
} | ||
}) | ||
var api = API() | ||
api.redis.message('start') | ||
.then(v => { | ||
assert.equal(v, 'start:echo:hi:redis') | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
.catch (e => console.error(e.stack)) | ||
it('should support namespaces', function(done) { | ||
var order = [] | ||
var API = Internal({ | ||
a(msg) { | ||
order.push(msg) | ||
}, | ||
b: { | ||
b: { | ||
b(msg) { | ||
order.push(msg) | ||
} | ||
} | ||
} | ||
}) | ||
var api = API() | ||
api | ||
.b.b.b('b') | ||
.a('a') | ||
.then(() => { | ||
assert.equal('b,a', order.join(',')) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
it('should support namespaces (reverse call)', function(done) { | ||
var order = [] | ||
var API = Internal({ | ||
a(msg) { | ||
order.push(msg) | ||
}, | ||
b: { | ||
b: { | ||
b(msg) { | ||
order.push(msg) | ||
} | ||
} | ||
} | ||
}) | ||
var api = API() | ||
api | ||
.a('a') | ||
.b.b.b('b') | ||
.then(() => { | ||
assert.equal('a,b', order.join(',')) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
}) |
12795
11
415
1