Merge pull request #229 from humphd/global-fix

Fix #228: Uncaught [Filer Error] Your browser doesn't support IndexedDB or WebSQL.
This commit is contained in:
Alan K 2014-06-23 14:35:21 -04:00
commit d38d1d0b6d
4 changed files with 334 additions and 347 deletions

View File

@ -1,20 +1,19 @@
(function(global) {
var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME;
var IDB_RW = require('../constants.js').IDB_RW;
var IDB_RO = require('../constants.js').IDB_RO;
var Errors = require('../errors.js');
var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME;
var IDB_RW = require('../constants.js').IDB_RW;
var IDB_RO = require('../constants.js').IDB_RO;
var Errors = require('../errors.js');
var indexedDB = global.indexedDB ||
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
function IndexedDBContext(db, mode) {
function IndexedDBContext(db, mode) {
var transaction = db.transaction(FILE_STORE_NAME, mode);
this.objectStore = transaction.objectStore(FILE_STORE_NAME);
}
IndexedDBContext.prototype.clear = function(callback) {
}
IndexedDBContext.prototype.clear = function(callback) {
try {
var request = this.objectStore.clear();
request.onsuccess = function(event) {
@ -26,8 +25,8 @@
} catch(e) {
callback(e);
}
};
IndexedDBContext.prototype.get = function(key, callback) {
};
IndexedDBContext.prototype.get = function(key, callback) {
try {
var request = this.objectStore.get(key);
request.onsuccess = function onsuccess(event) {
@ -40,8 +39,8 @@
} catch(e) {
callback(e);
}
};
IndexedDBContext.prototype.put = function(key, value, callback) {
};
IndexedDBContext.prototype.put = function(key, value, callback) {
try {
var request = this.objectStore.put(value, key);
request.onsuccess = function onsuccess(event) {
@ -54,8 +53,8 @@
} catch(e) {
callback(e);
}
};
IndexedDBContext.prototype.delete = function(key, callback) {
};
IndexedDBContext.prototype.delete = function(key, callback) {
try {
var request = this.objectStore.delete(key);
request.onsuccess = function onsuccess(event) {
@ -68,18 +67,18 @@
} catch(e) {
callback(e);
}
};
};
function IndexedDB(name) {
function IndexedDB(name) {
this.name = name || FILE_SYSTEM_NAME;
this.db = null;
}
IndexedDB.isSupported = function() {
}
IndexedDB.isSupported = function() {
return !!indexedDB;
};
};
IndexedDB.prototype.open = function(callback) {
IndexedDB.prototype.open = function(callback) {
var that = this;
// Bail if we already have a db open
@ -114,17 +113,15 @@
openRequest.onerror = function onerror(error) {
callback(new Errors.EINVAL('IndexedDB cannot be accessed. If private browsing is enabled, disable it.'));
};
};
IndexedDB.prototype.getReadOnlyContext = function() {
};
IndexedDB.prototype.getReadOnlyContext = function() {
// Due to timing issues in Chrome with readwrite vs. readonly indexeddb transactions
// always use readwrite so we can make sure pending commits finish before callbacks.
// See https://github.com/js-platform/filer/issues/128
return new IndexedDBContext(this.db, IDB_RW);
};
IndexedDB.prototype.getReadWriteContext = function() {
};
IndexedDB.prototype.getReadWriteContext = function() {
return new IndexedDBContext(this.db, IDB_RW);
};
};
module.exports = IndexedDB;
}(this));
module.exports = IndexedDB;

View File

@ -1,13 +1,12 @@
(function(global) {
var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME;
var WSQL_VERSION = require('../constants.js').WSQL_VERSION;
var WSQL_SIZE = require('../constants.js').WSQL_SIZE;
var WSQL_DESC = require('../constants.js').WSQL_DESC;
var u8toArray = require('../shared.js').u8toArray;
var Errors = require('../errors.js');
var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME;
var WSQL_VERSION = require('../constants.js').WSQL_VERSION;
var WSQL_SIZE = require('../constants.js').WSQL_SIZE;
var WSQL_DESC = require('../constants.js').WSQL_DESC;
var u8toArray = require('../shared.js').u8toArray;
var Errors = require('../errors.js');
function WebSQLContext(db, isReadOnly) {
function WebSQLContext(db, isReadOnly) {
var that = this;
this.getTransaction = function(callback) {
if(that.transaction) {
@ -20,8 +19,8 @@
callback(transaction);
});
};
}
WebSQLContext.prototype.clear = function(callback) {
}
WebSQLContext.prototype.clear = function(callback) {
function onError(transaction, error) {
callback(error);
}
@ -32,8 +31,8 @@
transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + ";",
[], onSuccess, onError);
});
};
WebSQLContext.prototype.get = function(key, callback) {
};
WebSQLContext.prototype.get = function(key, callback) {
function onSuccess(transaction, result) {
// If the key isn't found, return null
var value = result.rows.length === 0 ? null : result.rows.item(0).data;
@ -57,8 +56,8 @@
transaction.executeSql("SELECT data FROM " + FILE_STORE_NAME + " WHERE id = ?;",
[key], onSuccess, onError);
});
};
WebSQLContext.prototype.put = function(key, value, callback) {
};
WebSQLContext.prototype.put = function(key, value, callback) {
// We do extra work to make sure typed arrays survive
// being stored in the db and still get the right prototype later.
if(Object.prototype.toString.call(value) === "[object Uint8Array]") {
@ -78,8 +77,8 @@
transaction.executeSql("INSERT OR REPLACE INTO " + FILE_STORE_NAME + " (id, data) VALUES (?, ?);",
[key, value], onSuccess, onError);
});
};
WebSQLContext.prototype.delete = function(key, callback) {
};
WebSQLContext.prototype.delete = function(key, callback) {
function onSuccess(transaction, result) {
callback(null);
}
@ -90,18 +89,18 @@
transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + " WHERE id = ?;",
[key], onSuccess, onError);
});
};
};
function WebSQL(name) {
function WebSQL(name) {
this.name = name || FILE_SYSTEM_NAME;
this.db = null;
}
WebSQL.isSupported = function() {
}
WebSQL.isSupported = function() {
return !!global.openDatabase;
};
};
WebSQL.prototype.open = function(callback) {
WebSQL.prototype.open = function(callback) {
var that = this;
// Bail if we already have a db open
@ -148,14 +147,12 @@
transaction.executeSql("CREATE TABLE IF NOT EXISTS " + FILE_STORE_NAME + " (id unique, data TEXT);",
[], createIndex, onError);
});
};
WebSQL.prototype.getReadOnlyContext = function() {
};
WebSQL.prototype.getReadOnlyContext = function() {
return new WebSQLContext(this.db, true);
};
WebSQL.prototype.getReadWriteContext = function() {
};
WebSQL.prototype.getReadWriteContext = function() {
return new WebSQLContext(this.db, false);
};
};
module.exports = WebSQL;
}(this));
module.exports = WebSQL;

View File

@ -1,19 +1,18 @@
(function(global) {
var Filer = require("../..");
var Filer = require("../..");
var indexedDB = global.indexedDB ||
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
var needsCleanup = [];
if(global.addEventListener) {
var needsCleanup = [];
if(global.addEventListener) {
global.addEventListener('beforeunload', function() {
needsCleanup.forEach(function(f) { f(); });
});
}
}
function IndexedDBTestProvider(name) {
function IndexedDBTestProvider(name) {
var _done = false;
var that = this;
@ -49,8 +48,6 @@
this.init = init;
this.cleanup = cleanup;
}
}
module.exports = IndexedDBTestProvider;
}(this));
module.exports = IndexedDBTestProvider;

View File

@ -1,15 +1,13 @@
(function(global) {
var Filer = require('../..');
var Filer = require('../..');
var needsCleanup = [];
if(global.addEventListener) {
var needsCleanup = [];
if(global.addEventListener) {
window.addEventListener('beforeunload', function() {
needsCleanup.forEach(function(f) { f(); });
});
}
}
function WebSQLTestProvider(name) {
function WebSQLTestProvider(name) {
var _done = false;
var that = this;
@ -40,8 +38,6 @@
this.init = init;
this.cleanup = cleanup;
}
}
module.exports = WebSQLTestProvider;
}(this));
module.exports = WebSQLTestProvider;