hoodie-plugins-manager
Advanced tools
Comparing version 0.0.11 to 0.1.1
@@ -20,2 +20,3 @@ var PluginAPI = require('hoodie-plugins-api').PluginAPI, | ||
couch_url.auth = options.couchdb.user + ':' + options.couchdb.pass; | ||
manager.couch_url = url.format(couch_url); | ||
@@ -22,0 +23,0 @@ /** |
var databases_api = require('hoodie-plugins-api/lib/databases'), | ||
changes_pool = require('./changes_pool'), | ||
events = require('events'), | ||
@@ -18,12 +19,8 @@ couchr = require('couchr'), | ||
var feeds = {}; | ||
changes_pool.create(manager.couch_url, function (err, pool) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
tm.addSource = function (name, /*optional*/callback) { | ||
callback = callback || function (err) { | ||
if (err) { | ||
console.error('Error adding source: ' + name); | ||
console.error(err); | ||
} | ||
}; | ||
function docChangeEvent(doc) { | ||
function docChangeEvent(doc, name) { | ||
doc = databases_api.parseDoc(doc); | ||
@@ -34,60 +31,45 @@ if (doc.type && doc.type[0] === '$') { | ||
} | ||
if (!feeds[name]) { | ||
var db_url = manager._resolve(encodeURIComponent(name)); | ||
var feed = couchr.changes(db_url, {include_docs: true}); | ||
feed.on('change', function (change) { | ||
if (change.doc) { | ||
docChangeEvent(change.doc); | ||
tm.addSource = function (name, /*optional*/callback) { | ||
//console.log(['addSource', name]); | ||
callback = callback || function (err) { | ||
if (err) { | ||
console.error('Error adding source: ' + name); | ||
console.error(err); | ||
} | ||
}); | ||
feed.on('error', callback); | ||
feeds[name] = feed; | ||
var q = { | ||
start_key: '"$"', | ||
end_key: '"${}"', | ||
include_docs: true | ||
}; | ||
couchr.get(db_url + '/_all_docs', q, function (err, body, res) { | ||
function docChangeEvent(doc) { | ||
doc = databases_api.parseDoc(doc); | ||
if (doc.type && doc.type[0] === '$') { | ||
tm.emit('change', name, {doc: doc}); | ||
} | ||
} | ||
pool(name, {since: 0, include_docs: true}, function (err, change) { | ||
if (err) { | ||
return callback(err); | ||
console.error('Error getting update from changes pool'); | ||
console.error(err); | ||
return; | ||
} | ||
body.rows.forEach(function (row) { | ||
if (row.doc) { | ||
docChangeEvent(row.doc); | ||
} | ||
}); | ||
callback(); | ||
}); | ||
} | ||
else { | ||
callback(); | ||
} | ||
}; | ||
tm.removeSource = function (name, /*optional*/callback) { | ||
var feed = feeds[name]; | ||
if (feed) { | ||
feed.once('error', function (err) { | ||
// ignore connection errors during stopping of feed | ||
if (err.code !== 'ECONNREFUSED' && | ||
err.code !== 'ECONNRESET') { | ||
throw err; | ||
if (change.doc) { | ||
docChangeEvent(change.doc); | ||
} | ||
}); | ||
feed.once('stop', function () { | ||
delete feeds[name]; | ||
if (callback) { | ||
return callback(); | ||
} | ||
}); | ||
feed.stop(); | ||
} | ||
}; | ||
}; | ||
tm.stop = function (callback) { | ||
var names = Object.keys(feeds); | ||
async.map(names, tm.removeSource, callback); | ||
}; | ||
tm.removeSource = function (name, /*optional*/callback) { | ||
pool.remove(name); | ||
callback(null, tm); | ||
// TODO: remove this from API docs and eliminate callback argument | ||
if (callback) { | ||
return callback(); | ||
} | ||
}; | ||
tm.stop = function (callback) { | ||
var names = Object.keys(feeds); | ||
async.map(names, tm.removeSource, callback); | ||
}; | ||
callback(null, tm); | ||
}); | ||
}; |
{ | ||
"name": "hoodie-plugins-manager", | ||
"version": "0.0.11", | ||
"version": "0.1.1", | ||
"description": "Run and dispatch events to Hoodie plugin server extensions", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -1,3 +0,3 @@ | ||
This is a component of [hoodie-server](/hoodiehq/hoodie-server) that manages Hoodie plugins. | ||
This is a component of [hoodie-server](https://github.com/hoodiehq/hoodie-server) that manages Hoodie plugins. | ||
See [My First Hoodie](/hoodiehq/my-first-hoodie) for how to get started with Hoodie. | ||
See [My First Hoodie](https://github.com/hoodiehq/my-first-hoodie) for how to get started with Hoodie. |
48295
12
1359
4