Fix issue #56: Support Filer in node.js as an fs alternative

This commit is contained in:
David Humphrey 2014-05-17 15:18:32 -04:00 committed by Kieran Sedgwick
parent 81b4d26b90
commit 5fcd313e2f
3 changed files with 19 additions and 10 deletions

View File

@ -31,7 +31,8 @@ define(function(require) {
}
var localStorage = (function(window) {
if (typeof window.localStorage === 'undefined') {
if (typeof window === 'undefined' ||
typeof window.localStorage === 'undefined') {
return {
getItem : function() {},
setItem : function() {},
@ -53,6 +54,12 @@ define(function(require) {
var storageHandler = function() {
self._onStorageEvent.apply(self, arguments);
};
// If we're in node.js, skip event registration
if (typeof window === 'undefined' || typeof document === 'undefined') {
return;
}
if (document.attachEvent) {
document.attachEvent('onstorage', storageHandler);
} else {
@ -80,7 +87,7 @@ define(function(require) {
self._on('storage', lock);
listening = true;
}
waitTimer = window.setTimeout(lock, WAIT);
waitTimer = setTimeout(lock, WAIT);
return;
}
executed = true;
@ -95,7 +102,7 @@ define(function(require) {
self._off('storage', lock);
}
if (waitTimer) {
window.clearTimeout(waitTimer);
clearTimeout(waitTimer);
}
localStorage.removeItem(INDEX_LOCK);
}
@ -240,7 +247,7 @@ define(function(require) {
localStorage.setItem(INDEX_EMIT, data);
self.trigger(name, message);
window.setTimeout(function() {
setTimeout(function() {
self._cleanup_emit();
}, 50);
});
@ -277,7 +284,7 @@ define(function(require) {
localStorage.setItem(INDEX_ONCE, JSON.stringify(data));
fn();
window.setTimeout(function() {
setTimeout(function() {
self._cleanup_once();
}, 50);
});

View File

@ -2,10 +2,12 @@ define(function(require) {
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('src/constants').FILE_STORE_NAME;
var indexedDB = window.indexedDB ||
var indexedDB = (function(window) {
return window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB;
}(this));
var IDB_RW = require('src/constants').IDB_RW;
var IDB_RO = require('src/constants').IDB_RO;

View File

@ -98,7 +98,7 @@ define(function(require) {
this.db = null;
}
WebSQL.isSupported = function() {
return !!window.openDatabase;
return typeof window === 'undefined' ? false : !!window.openDatabase;
};
WebSQL.prototype.open = function(callback) {