Skip to content

Commit

Permalink
(#1150) - use prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Feb 23, 2014
1 parent d55c8fa commit bca98c7
Show file tree
Hide file tree
Showing 8 changed files with 722 additions and 761 deletions.
1,316 changes: 606 additions & 710 deletions lib/adapter.js

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var utils = require('../utils');
var errors = require('../deps/errors');

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
Expand Down Expand Up @@ -129,18 +128,16 @@ function genUrl(opts, path) {

return '/' + path;
}

// Implements the PouchDB API for dealing with CouchDB instances over HTTP
function HttpPouch(opts, callback) {

// Parse the URI given by opts.name into an easy-to-use object
var host = getHost(opts.name, opts);

// Generate the database URL based on the host
var db_url = genDBUrl(host, '');

// The functions that will be publically available for HttpPouch
var api = {};
var api = this;
var ajaxOpts = opts.ajax || {};
opts = utils.extend(true, {}, opts);
function ajax(options, callback) {
Expand Down Expand Up @@ -1092,7 +1089,6 @@ function HttpPouch(opts, callback) {
method: 'DELETE'
}, callback);
});
return api;
}

// Delete the HttpPouch specified by the given name.
Expand Down
4 changes: 1 addition & 3 deletions lib/adapters/idb.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function isModernIdb() {
}
return result;
}

function IdbPouch(opts, callback) {

var POUCH_VERSION = 1;
Expand Down Expand Up @@ -61,7 +60,7 @@ function IdbPouch(opts, callback) {

var blobSupport = null;
var instanceId = null;
var api = {};
var api = this;
var idb = null;

req.onupgradeneeded = function (e) {
Expand Down Expand Up @@ -810,7 +809,6 @@ function IdbPouch(opts, callback) {
};
};

return api;
}

IdbPouch.valid = function () {
Expand Down
3 changes: 1 addition & 2 deletions lib/adapters/leveldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var UUID_KEY = '_local_uuid';

function LevelPouch(opts, callback) {
opts = utils.extend(true, {}, opts);
var api = {};
var api = this;
var instanceId;
var updateSeq = 0;
var docCount = 0;
Expand Down Expand Up @@ -653,7 +653,6 @@ function LevelPouch(opts, callback) {
});
}
});
return api;
}

LevelPouch.valid = function () {
Expand Down
26 changes: 11 additions & 15 deletions lib/adapters/websql.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ function decodeUtf8(str) {
}
return isMangledUnicode(result) ? unmangleUnicode(result) : result;
}

function webSqlPouch(opts, callback) {

var api = {};
function WebSqlPouch(opts, callback) {
var api = this;
var instanceId = null;
var name = opts.name;

Expand Down Expand Up @@ -233,8 +231,8 @@ function webSqlPouch(opts, callback) {

docsWritten++;

webSqlPouch.Changes.notify(name);
webSqlPouch.Changes.notifyLocalWindows(name);
WebSqlPouch.Changes.notify(name);
WebSqlPouch.Changes.notifyLocalWindows(name);
});

var updateseq = 'SELECT update_seq FROM ' + META_STORE;
Expand Down Expand Up @@ -625,14 +623,14 @@ function webSqlPouch(opts, callback) {
if (opts.continuous) {
var id = name + ':' + utils.uuid();
opts.cancelled = false;
webSqlPouch.Changes.addListener(name, id, api, opts);
webSqlPouch.Changes.notify(name);
WebSqlPouch.Changes.addListener(name, id, api, opts);
WebSqlPouch.Changes.notify(name);
return {
cancel: function () {
utils.call(opts.complete, null, {status: 'cancelled'});
opts.complete = null;
opts.cancelled = true;
webSqlPouch.Changes.removeListener(name, id);
WebSqlPouch.Changes.removeListener(name, id);
}
};
}
Expand Down Expand Up @@ -738,11 +736,9 @@ function webSqlPouch(opts, callback) {
});
});
};

return api;
}

webSqlPouch.valid = function () {
WebSqlPouch.valid = function () {
if (typeof global !== 'undefined') {
if (global.navigator && global.navigator.sqlitePlugin && global.navigator.sqlitePlugin.openDatabase) {
return true;
Expand All @@ -755,7 +751,7 @@ webSqlPouch.valid = function () {
return false;
};

webSqlPouch.destroy = utils.toPromise(function (name, opts, callback) {
WebSqlPouch.destroy = utils.toPromise(function (name, opts, callback) {
var db = openDB(name, POUCH_VERSION, name, POUCH_SIZE);
db.transaction(function (tx) {
tx.executeSql('DROP TABLE IF EXISTS ' + DOC_STORE, []);
Expand All @@ -767,6 +763,6 @@ webSqlPouch.destroy = utils.toPromise(function (name, opts, callback) {
});
});

webSqlPouch.Changes = new utils.Changes();
WebSqlPouch.Changes = new utils.Changes();

module.exports = webSqlPouch;
module.exports = WebSqlPouch;
124 changes: 99 additions & 25 deletions lib/constructor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*globals cordova */
"use strict";

var Adapter = require('./adapter')(PouchDB);
var Adapter = require('./adapter');
var utils = require('./utils');
var Promise = typeof global.Promise === 'function' ? global.Promise : require('bluebird');

Expand All @@ -14,6 +15,7 @@ function makeFailFunction(error) {
arguments[arguments.length - 1](error);
});
}
utils.inherits(PouchDB, Adapter);
function PouchDB(name, opts, callback) {

if (!(this instanceof PouchDB)) {
Expand All @@ -32,7 +34,53 @@ function PouchDB(name, opts, callback) {
if (typeof callback === 'undefined') {
callback = defaultCallback;
}
opts = opts || {};
var oldCB = callback;
self.auto_compaction = opts.auto_compaction;
self.prefix = PouchDB.prefix;
Adapter.call(self);
var taskqueue = {};

taskqueue.ready = false;
taskqueue.failed = false;
taskqueue.queue = [];

self.taskqueue = {};

self.taskqueue.execute = function (db) {
var d;
if (taskqueue.failed) {
while ((d = taskqueue.queue.shift())) {
d.parameters[d.parameters.length - 1](this.failed);
}
} else if (taskqueue.ready) {
while ((d = taskqueue.queue.shift())) {
d.task = db[d.name].apply(db, d.parameters);
}
}
};
self.taskqueue.fail = function (err) {
taskqueue.failed = err;
this.taskqueue.execute();
};

self.taskqueue.ready = function () {
if (taskqueue.failed) {
return false;
} else if (arguments.length === 0) {
return taskqueue.ready;
}
taskqueue.ready = arguments[0];
};

self.taskqueue.addTask = function (name, parameters) {
var task = { name: name, parameters: parameters };
taskqueue.queue.push(task);
if (taskqueue.failed) {
this.taskqueue.execute();
}
return task;
};
var promise = new Promise(function (fulfill, reject) {
callback = function (err, resp) {
if (err) {
Expand Down Expand Up @@ -88,40 +136,57 @@ function PouchDB(name, opts, callback) {
if (error) {
return reject(error); // constructor error, see above
}
var adapter = new Adapter(opts, function (err) {
self.adapter = opts.adapter;
// needs access to PouchDB;
self.replicate = PouchDB.replicate.bind(self, self);
self.replicate.from = function (url, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
return PouchDB.replicate(url, self, opts, callback);
};

self.replicate.to = function (dbName, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
return self.replicate(dbName, opts, callback);
};

self.replicate.sync = function (dbName, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
return PouchDB.sync(self, dbName, opts, callback);
};
self.destroy = utils.toPromise(function (callback) {
var self = this;
if (!self.taskqueue.ready()) {
self.taskqueue.addTask('destroy', arguments);
return;
}
self.id(function (err, id) {
if (err) {
return callback(err);
}
PouchDB.destroy(id, callback);
});
});
PouchDB.adapters[opts.adapter].call(self, opts, function (err, db) {
if (err) {
if (callback) {
callback(err);
}
return;
}

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];
}
}
}
}
}

self.taskqueue.ready(true);
self.taskqueue.execute(self);
callback(null, self);

});
for (var j in adapter) {
if (adapter.hasOwnProperty(j)) {
self[j] = adapter[j];
}
}
for (var plugin in PouchDB.plugins) {
if (PouchDB.plugins.hasOwnProperty(plugin)) {

Expand All @@ -139,6 +204,15 @@ function PouchDB(name, opts, callback) {
}
}
}
if (opts.skipSetup) {
self.taskqueue.ready(true);
self.taskqueue.execute(this);
}

if (utils.isCordova()) {
//to inform websql adapter that we can use api
cordova.fireWindowEvent(opts.name + "_pouch", {});
}
});
promise.then(function (resp) {
oldCB(null, resp);
Expand Down
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var reservedWords = [
'_local_seq',
'_rev_tree'
];
exports.inherits = require('inherits');
exports.uuids = function (count, options) {

if (typeof(options) !== 'object') {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"bluebird": "~1.0.0",
"level-sublevel": "~5.2.0",
"levelup": "~0.18.2",
"leveldown": "~0.10.2"
"leveldown": "~0.10.2",
"inherits": "~2.0.1"
},
"devDependencies": {
"commander": "~2.1.0",
Expand Down

0 comments on commit bca98c7

Please sign in to comment.