update storage.js so no error when the window is not defined

pull/7/head
Rob Stupay 7 years ago
parent 14a09cc7ea
commit a42040de74
  1. 14
      remix-lib/src/storage.js

@ -2,16 +2,22 @@
function Storage (prefix) {
this.exists = function (name) {
if (typeof window !== 'undefined') {
return this.get(name) !== null
}
}
this.get = function (name) {
if (typeof window !== 'undefined') {
return window.localStorage.getItem(prefix + name)
}
}
this.set = function (name, content) {
try {
if (typeof window !== 'undefined') {
window.localStorage.setItem(prefix + name, content)
}
} catch (exception) {
return false
}
@ -19,9 +25,11 @@ function Storage (prefix) {
}
this.remove = function (name) {
if (typeof window !== 'undefined') {
window.localStorage.removeItem(prefix + name)
return true
}
}
this.rename = function (originalName, newName) {
var content = this.get(originalName)
@ -34,8 +42,10 @@ function Storage (prefix) {
function safeKeys () {
// NOTE: this is a workaround for some browsers
if (typeof window !== 'undefined') {
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
}
}
this.keys = function () {
return safeKeys()
@ -46,6 +56,7 @@ function Storage (prefix) {
}
// on startup, upgrade the old storage layout
if (typeof window !== 'undefined') {
safeKeys().forEach(function (name) {
if (name.indexOf('sol-cache-file-', 0) === 0) {
var content = window.localStorage.getItem(name)
@ -53,9 +64,12 @@ function Storage (prefix) {
window.localStorage.removeItem(name)
}
})
}
// remove obsolete key
if (typeof window !== 'undefined') {
window.localStorage.removeItem('editor-size-cache')
}
}
module.exports = Storage

Loading…
Cancel
Save