Skip to content

Commit

Permalink
(pouchdb#1068) - cache websql dbs
Browse files Browse the repository at this point in the history
Safari 7 starts throwing DOM Exception 11 if
we open up too many db handles, which crashes
our unit tests. Caching them fixes that.

We actually already tried this once before in
6575e8d, but
we forgot to put the cachedDatabases outside
the constructor method.
  • Loading branch information
nolanlawson authored and sygi committed Apr 28, 2014
1 parent b4440a1 commit c80159e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/adapters/websql.js
Expand Up @@ -7,6 +7,8 @@ function quote(str) {
return "'" + str + "'";
}

var cachedDatabases = {};

var openDB = utils.getArguments(function (args) {
if (typeof global !== 'undefined') {
if (global.navigator && global.navigator.sqlitePlugin &&
Expand All @@ -17,7 +19,11 @@ var openDB = utils.getArguments(function (args) {
return global.sqlitePlugin.openDatabase
.apply(global.sqlitePlugin, args);
} else {
return global.openDatabase.apply(global, args);
var db = cachedDatabases[args[0]];
if (!db) {
db = cachedDatabases[args[0]] = global.openDatabase.apply(global, args);
}
return db;
}
}
});
Expand Down Expand Up @@ -46,7 +52,6 @@ var DOC_STORE_WINNINGSEQ_INDEX_SQL = 'CREATE INDEX IF NOT EXISTS \'doc-winningse


var idRequests = [];
var cachedDatabases = {};

function unknownError(callback) {
return function (event) {
Expand Down Expand Up @@ -106,10 +111,7 @@ function WebSqlPouch(opts, callback) {
var instanceId = null;
var name = opts.name;

var db = cachedDatabases[name];
if (!db) {
cachedDatabases[name] = db = openDB(name, POUCH_VERSION, name, POUCH_SIZE);
}
var db = openDB(name, POUCH_VERSION, name, POUCH_SIZE);
if (!db) {
return callback(errors.UNKNOWN_ERROR);
}
Expand Down

0 comments on commit c80159e

Please sign in to comment.