Skip to content

Commit

Permalink
(#1387) - prototype based plugins api
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Feb 28, 2014
1 parent bdd283f commit e8d8c62
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
12 changes: 12 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,3 +742,15 @@ db.revsDiff({
}
}
{% endhighlight %}

## Plugins<a id="plugins"></a>

Writing a plugin is easy the api is

{% highlight js %}
PouchDB.plugin({
methodName: function
});
{% endhighlight %}

This will add the function as a method of all databases with the given name, it will always be called in the so that `this` is db.
17 changes: 0 additions & 17 deletions lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,6 @@ function PouchDB(name, opts, callback) {
callback(null, self);

});
for (var plugin in PouchDB.plugins) {
if (PouchDB.plugins.hasOwnProperty(plugin)) {

// In future these will likely need to be async to allow the plugin
// to initialise
var pluginObj = PouchDB.plugins[plugin](self);
for (var api in pluginObj) {
if (pluginObj.hasOwnProperty(api)) {
// We let things like the http adapter use its own implementation
// as it shares a lot of code
if (!(api in self)) {
self[api] = pluginObj[api];
}
}
}
}
}
if (opts.skipSetup) {
self.taskqueue.ready(self);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PouchDB.adapter('https', httpAdapter);

PouchDB.adapter('idb', require('./adapters/idb'));
PouchDB.adapter('websql', require('./adapters/websql'));
PouchDB.plugin('mapreduce', require('pouchdb-mapreduce'));
PouchDB.plugin(require('pouchdb-mapreduce'));

if (!process.browser) {
var ldbAdapter = require('./adapters/leveldb');
Expand Down
13 changes: 4 additions & 9 deletions lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var PouchDB = require("./constructor");
var utils = require('./utils');
PouchDB.adapters = {};
PouchDB.plugins = {};

PouchDB.prefix = '_pouch_';

Expand Down Expand Up @@ -50,12 +49,6 @@ PouchDB.destroy = utils.toPromise(function (name, opts, callback) {
var backend = PouchDB.parseAdapter(opts.name || name);
var dbName = backend.name;

for (var plugin in PouchDB.plugins) {
if (PouchDB.plugins.hasOwnProperty(plugin)) {
PouchDB.plugins[plugin]._delete(dbName);
}
}

// call destroy method of the particular adaptor
PouchDB.adapters[backend.adapter].destroy(dbName, opts, callback);
});
Expand All @@ -70,8 +63,10 @@ PouchDB.adapter = function (id, obj) {
}
};

PouchDB.plugin = function (id, obj) {
PouchDB.plugins[id] = obj;
PouchDB.plugin = function (obj) {
Object.keys(obj).forEach(function (id) {
PouchDB.prototype[id] = obj[id];
});
};

module.exports = PouchDB;
7 changes: 7 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,16 @@ exports.fixBinary = function (bin) {
};

exports.toPromise = function (func) {
//create the function we will be returning
return function () {
var self = this;
var args = Array.prototype.slice.call(arguments);
var tempCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false;
// if the last argument is a function, assume its a callback
var usedCB;
if (tempCB) {
// if it was a callback, create a new callback which calls the callback function
// but we do so async so we don't trap any errors
usedCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
Expand All @@ -371,9 +375,12 @@ exports.toPromise = function (func) {
fulfill(mesg);
}
}
// create a callback for this invocation
args.push(callback);
func.apply(self, args);
// apply the function in the orig context
});
// if there is a callback, call it back
if (usedCB) {
promise.then(function (result) {
usedCB(null, result);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pouchdb",
"version": "1.2.0-alpha",
"version": "2.0.0-alpha",
"description": "PouchDB is a pocket-sized database.",
"release": "nightly",
"main": "./lib/index.js",
Expand All @@ -18,7 +18,7 @@
],
"dependencies": {
"request": "~2.28.0",
"pouchdb-mapreduce": "0.6.2",
"pouchdb-mapreduce": "1.0.0",
"bluebird": "~1.0.0",
"level-sublevel": "~5.2.0",
"levelup": "~0.18.2",
Expand Down

0 comments on commit e8d8c62

Please sign in to comment.