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

View File

@ -2,10 +2,12 @@ define(function(require) {
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME; var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = require('src/constants').FILE_STORE_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.mozIndexedDB ||
window.webkitIndexedDB || window.webkitIndexedDB ||
window.msIndexedDB; window.msIndexedDB;
}(this));
var IDB_RW = require('src/constants').IDB_RW; var IDB_RW = require('src/constants').IDB_RW;
var IDB_RO = require('src/constants').IDB_RO; var IDB_RO = require('src/constants').IDB_RO;

View File

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