Merge pull request #656 from ethereum/theme_switch

storage.js update
pull/7/head
yann300 7 years ago committed by GitHub
commit 821b672d77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      remix-lib/src/storage.js

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

Loading…
Cancel
Save