Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ressurect the extension by using newer API for getting databases #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions lib/index-injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@
var PouchDB = global.PouchDB;

if (PouchDB) {
PouchDB = PouchDB.defaults({});

PouchDB.allDbs = function () {
var prefix = typeof PouchDB.prefix === "undefined" ? "_pouch_" : PouchDB.prefix;
return getIDBDatabaseNames().then(function (names) {
return names.filter(function (name) {
return name.indexOf(prefix) === 0;
}).map(function (name) {
return name.substr(prefix.length);
}).filter(function (name) {
return (
["_checkModernIdb", "pouch__all_dbs__"].indexOf(name) === -1 &&
name.indexOf("-mrview-") === -1 &&
name.indexOf("-session-") === -1
);
});
});
};
PouchDB = PouchDB.defaults({});

PouchDB.allDbs = function () {
var prefix = typeof PouchDB.prefix === "undefined" ? "_pouch_" : PouchDB.prefix;
return getIDBDatabaseNames().then(function (databasesMap) {
let names = databasesMap.map((databaseEntry) => {
return databaseEntry.name;
});

return names.filter(function (name) {
return name.indexOf(prefix) === 0;
}).map(function (name) {
return name.substr(prefix.length);
}).filter(function (name) {
return (
["_checkModernIdb", "pouch__all_dbs__"].indexOf(name) === -1 &&
name.indexOf("-mrview-") === -1 &&
name.indexOf("-session-") === -1
);
});
});
};
}

var req2resp = require("./req2resp.js")(PouchDB);
Expand All @@ -33,16 +37,14 @@ global.addEventListener("message", rpc.onMessage);
rpc.serve("req2resp", req2resp);

var getIDBDatabaseNames;
if (global.indexedDB.webkitGetDatabaseNames) {
getIDBDatabaseNames = function () {
//Promisify webkitGetDatabaseNames()
return new global.Promise(function (resolve, reject) {
var req = global.indexedDB.webkitGetDatabaseNames().onsuccess = function () {
resolve(Array.prototype.slice.call(this.result));
};
});
};
// supported on every browser except firefox at the time of writing
// https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/databases
if (global.indexedDB.databases) {
getIDBDatabaseNames = function () {
return global.indexedDB.databases();
};
} else {
//In Firefox, ask the add-on to provide the idb db names
getIDBDatabaseNames = rpc.call.bind(rpc, "main", "getIDBDatabaseNames");
// I don't know how the fallback worked before but it probably no longer work on those browsers
//In Firefox, ask the add-on to provide the idb db names
getIDBDatabaseNames = rpc.call.bind(rpc, "main", "getIDBDatabaseNames");
}