Skip to content

Commit

Permalink
._push: init adapter framework
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Aug 31, 2014
1 parent 7c3ed6d commit 95d8cd5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 57 deletions.
63 changes: 47 additions & 16 deletions lib/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,58 @@ var assert = require('assert');

/**
* Persist transactions to the adapters.
* Emits a 'succes' or 'failure' event with an object attached.
* Called internally by the transaction
* function. Emits an error if any error
* is present, else it emits a sync event.
*
* store._push()
*
* @return {Object}
* @api public
* @api private
*/

exports._push = function() {
debug('Pushing transactions: ' + this._transactions);
parallel.call(this, this._adapters, callback.bind(this));
function callback(err) {
if (err.length) {
this.emit('failure', err);
return;
}
this.emit('success', this._transactions);
this._transactions = [];
exports._push = function(method, data) {

assert('string' == typeof method, 'Method should be a string');
assert(data, 'Data should not be undefined');

var supply = generate(this._adapters);
var self = this;

// The callback function that gets called when done.

function cb(err, res) {
if (err) return self.emit('error', err, res);
else self.emit('sync', err, res);
}

return this;
// Create the generator object
// that keeps track of the adapter
// state.

function generate(adapters) {
if (!(this instanceof generate)) return new generate(adapters);
this._adapterCount = adapters.length - 1;
this.next = next.bind(this);
this._adapters = adapters;
this._index = 0;
return this;
}

// Get an adapter from ._adapters
// and pass it the arguments.
// Returns true if an adapter was
// called, and false if there are
// no adapters left.

function next(data, supply, cb) {
if (this._index > this._adapterCount) return cb(null, data);
var fn = this._adapters[this._index][method];
this._index += 1;
fn(data, supply, cb)
return true;
}

// Initialize the adapter chain.

supply.next(data, supply, cb);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ module.exports.validate = function(record) {
assert(recordProp, 'Property should exist on the record');
assert(modelProp, 'Property should exist on the model');

if (!modelProp.type) { return result[key] = recordProp == undefined ?
undefined : true;
if (!modelProp.type) {
return result[key] = recordProp == undefined ? undefined : true;
}
if (undefined === typeof recordProp) return result[key] = undefined;
if (modelProp.type != typeof recordProp) return result[key] = false;
Expand Down
41 changes: 30 additions & 11 deletions test/_push.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,39 @@ beforeEach(function() {
*/

describe('._push()', function () {
it('should catch errors', function (done) {
books.on('failure', function(){done()});
books._adapters = [function(end) {end('nope')}];
it('should catch errors', function() {
books._push.bind(books, 123)
.should.throw('Method should be a string');

books._push();
books._push.bind(books, 'hello')
.should.throw('Data should not be undefined');
});

it('should call the adapters', function (done) {
books.on('success', function() {done()});
books._adapters = [
function(end) {setTimeout(function(){end()}, 300);},
function(end) {end()}
];
it('should call a single adapter', function(done) {
books.on('sync', function() {done()});

books._push();
books._adapters = [{
create: function(data, supply, cb) {
supply.next(data, supply, cb);
}
}];

books._push('create', {cat: 'fish'});
});

it('should chain call multiple adapters', function(done) {
books.on('sync', function() {done()});

books._adapters = [{
create: function(data, supply, cb) {
supply.next(data, supply, cb);
}
}, {
create: function(data, supply, cb) {
supply.next(data, supply, cb);
}
}];

books._push('create', {cat: 'fish'});
});
});
28 changes: 0 additions & 28 deletions test/rest.js

This file was deleted.

0 comments on commit 95d8cd5

Please sign in to comment.