Skip to content

Commit

Permalink
Merge pull request ethereum#377 from ethereum/storage-reworked
Browse files Browse the repository at this point in the history
Storage reworked
  • Loading branch information
chriseth authored Jan 17, 2017
2 parents f81504c + 5f63505 commit 2064ac9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 71 deletions.
42 changes: 14 additions & 28 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
var $ = require('jquery')
var base64 = require('js-base64').Base64

var utils = require('./app/utils')
var QueryParams = require('./app/query-params')
var queryParams = new QueryParams()
var GistHandler = require('./app/gist-handler')
Expand Down Expand Up @@ -45,11 +44,10 @@ var run = function () {

function loadFiles (files) {
for (var f in files) {
var key = utils.fileKey(f)
var content = files[f].content
storage.loadFile(key, content)
storage.loadFile(f, files[f].content)
}
editor.setCacheFile(utils.fileKey(Object.keys(files)[0]))
// Set the first file as current tab
editor.setCacheFile(Object.keys(files)[0])
updateFiles()
}

Expand Down Expand Up @@ -100,7 +98,7 @@ var run = function () {
function check (key) {
chrome.storage.sync.get(key, function (resp) {
console.log('comparing to cloud', key, resp)
if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + utils.fileNameFromKey(key) + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) {
if (typeof resp[key] !== 'undefined' && obj[key] !== resp[key] && confirm('Overwrite "' + key + '"? Click Ok to overwrite local file with file from cloud. Cancel will push your local file to the cloud.')) {
console.log('Overwriting', key)
storage.set(key, resp[key])
updateFiles()
Expand All @@ -120,9 +118,6 @@ var run = function () {
for (var y in storage.keys()) {
console.log('checking', y)
obj[y] = storage.get(y)
if (!utils.isCachedFile(y)) {
continue
}
count++
check(y)
}
Expand Down Expand Up @@ -246,9 +241,9 @@ var run = function () {
editor.hasFile(newName)
? 'Are you sure you want to overwrite: ' + newName + ' with ' + originalName + '?'
: 'Are you sure you want to rename: ' + originalName + ' to ' + newName + '?')) {
storage.rename(utils.fileKey(originalName), utils.fileKey(newName))
editor.renameSession(utils.fileKey(originalName), utils.fileKey(newName))
editor.setCacheFile(utils.fileKey(newName))
storage.rename(originalName, newName)
editor.renameSession(originalName, newName)
editor.setCacheFile(newName)
}

updateFiles()
Expand All @@ -263,16 +258,16 @@ var run = function () {
var name = $(this).parent().find('.name').text()

if (confirm('Are you sure you want to remove: ' + name + ' from local storage?')) {
storage.remove(utils.fileKey(name))
editor.removeSession(utils.fileKey(name))
editor.setNextFile(utils.fileKey(name))
storage.remove(name)
editor.removeSession(name)
editor.setNextFile(name)
updateFiles()
}
return false
})

function swicthToFile (file) {
editor.setCacheFile(utils.fileKey(file))
editor.setCacheFile(file)
updateFiles()
}

Expand All @@ -290,12 +285,12 @@ var run = function () {
$('#output').empty()

for (var f in files) {
var name = utils.fileNameFromKey(files[f])
var name = files[f]
$filesEl.append($('<li class="file"><span class="name">' + name + '</span><span class="remove"><i class="fa fa-close"></i></span></li>'))
}

if (editor.cacheFileIsPresent()) {
var currentFileName = utils.fileNameFromKey(editor.getCacheFile())
var currentFileName = editor.getCacheFile()
var active = $('#files .file').filter(function () { return $(this).find('.name').text() === currentFileName })
active.addClass('active')
editor.resetSession()
Expand Down Expand Up @@ -369,7 +364,6 @@ var run = function () {

// ----------------- resizeable ui ---------------

var EDITOR_SIZE_KEY = 'editor-size-cache'
var EDITOR_WINDOW_SIZE = 'editorWindowSize'

var dragging = false
Expand Down Expand Up @@ -413,14 +407,6 @@ var run = function () {
}
})

// convert old browser-solidity
if (storage.exists(EDITOR_SIZE_KEY)) {
if (!config.exists(EDITOR_WINDOW_SIZE)) {
config.set(EDITOR_WINDOW_SIZE, storage.get(EDITOR_SIZE_KEY))
}
storage.remove(EDITOR_SIZE_KEY)
}

if (config.exists(EDITOR_WINDOW_SIZE)) {
setEditorSize(config.get(EDITOR_WINDOW_SIZE))
} else {
Expand Down Expand Up @@ -536,7 +522,7 @@ var run = function () {

function runCompiler () {
var files = {}
var target = utils.fileNameFromKey(editor.getCacheFile())
var target = editor.getCacheFile()

files[target] = editor.getValue()

Expand Down
4 changes: 1 addition & 3 deletions src/app/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var utils = require('./utils')

var CONFIG_FILE = utils.fileKey('.browser-solidity.json')
var CONFIG_FILE = '.browser-solidity.json'

function Config (storage) {
this.items = {}
Expand Down
3 changes: 1 addition & 2 deletions src/app/debugger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

var remix = require('ethereum-remix')
var utils = require('./utils')
var ace = require('brace')
var Range = ace.acequire('ace/range').Range

Expand Down Expand Up @@ -70,7 +69,7 @@ Debugger.prototype.debug = function (txHash) {
* @param {Object} rawLocation - raw position of the source code to hightlight {start, length, file, jump}
*/
Debugger.prototype.highlight = function (lineColumnPos, rawLocation) {
var name = utils.fileNameFromKey(this.editor.getCacheFile()) // current opened tab
var name = this.editor.getCacheFile() // current opened tab
var source = this.compiler.lastCompilationResult.data.sourceList[rawLocation.file] // auto switch to that tab
this.removeCurrentMarker()
if (name !== source) {
Expand Down
17 changes: 8 additions & 9 deletions src/app/editor.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* global FileReader */
'use strict'

var utils = require('./utils')
var examples = require('./example-contracts')

var ace = require('brace')
require('../mode-solidity.js')

function Editor (loadingFromGist, storage) {
var SOL_CACHE_UNTITLED = utils.fileKey('Untitled')
var SOL_CACHE_UNTITLED = 'Untitled'
var SOL_CACHE_FILE = null

var editor = ace.edit('input')
Expand Down Expand Up @@ -37,7 +36,7 @@ function Editor (loadingFromGist, storage) {

this.uploadFile = function (file, callback) {
var fileReader = new FileReader()
var cacheName = utils.fileKey(file.name)
var cacheName = file.name

fileReader.onload = function (e) {
storage.set(cacheName, e.target.result)
Expand Down Expand Up @@ -85,18 +84,18 @@ function Editor (loadingFromGist, storage) {
}

this.hasFile = function (name) {
return this.getFiles().indexOf(utils.fileKey(name)) !== -1
return this.getFiles().indexOf(name) !== -1
}

this.getFile = function (name) {
return storage.get(utils.fileKey(name))
return storage.get(name)
}

function getFiles () {
var files = []
storage.keys().forEach(function (f) {
// NOTE: as a temporary measure do not show the config file in the editor
if (utils.isCachedFile(f) && (f !== (utils.fileKey('.browser-solidity.json')))) {
if (f !== '.browser-solidity.json') {
files.push(f)
if (!sessions[f]) sessions[f] = newEditorSession(f)
}
Expand All @@ -110,7 +109,7 @@ function Editor (loadingFromGist, storage) {
var filesArr = this.getFiles()

for (var f in filesArr) {
files[utils.fileNameFromKey(filesArr[f])] = {
files[filesArr[f]] = {
content: storage.get(filesArr[f])
}
}
Expand Down Expand Up @@ -174,8 +173,8 @@ function Editor (loadingFromGist, storage) {
function setupStuff (files) {
if (files.length === 0) {
if (loadingFromGist) return
files.push(utils.fileKey(examples.ballot.name))
storage.set(utils.fileKey(examples.ballot.name), examples.ballot.content)
files.push(examples.ballot.name)
storage.set(examples.ballot.name, examples.ballot.content)
}

SOL_CACHE_FILE = files[0]
Expand Down
8 changes: 4 additions & 4 deletions src/app/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Renderer.prototype.error = function (message, container, options) {
var errFile = err[1]
var errLine = parseInt(err[2], 10) - 1
var errCol = err[4] ? parseInt(err[4], 10) : 0
if (!opt.noAnnotations && (errFile === '' || errFile === utils.fileNameFromKey(self.editor.getCacheFile()))) {
if (!opt.noAnnotations && (errFile === '' || errFile === self.editor.getCacheFile())) {
self.editor.addAnnotation({
row: errLine,
column: errCol,
Expand All @@ -64,9 +64,9 @@ Renderer.prototype.error = function (message, container, options) {
})
}
$error.click(function (ev) {
if (errFile !== '' && errFile !== utils.fileNameFromKey(self.editor.getCacheFile()) && self.editor.hasFile(errFile)) {
if (errFile !== '' && errFile !== self.editor.getCacheFile() && self.editor.hasFile(errFile)) {
// Switch to file
self.editor.setCacheFile(utils.fileKey(errFile))
self.editor.setCacheFile(errFile)
self.updateFiles()
}
self.editor.handleErrorClick(errLine, errCol)
Expand Down Expand Up @@ -287,7 +287,7 @@ Renderer.prototype.contracts = function (data, source) {
var self = this

var getSource = function (contractName, source, data) {
var currentFile = utils.fileNameFromKey(self.editor.getCacheFile())
var currentFile = self.editor.getCacheFile()
return source.sources[currentFile]
}

Expand Down
28 changes: 24 additions & 4 deletions src/app/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ function Storage () {
}

this.get = function (name) {
return window.localStorage.getItem(name)
return window.localStorage.getItem('sol:' + name)
}

this.set = function (name, content) {
window.localStorage.setItem(name, content)
window.localStorage.setItem('sol:' + name, content)
}

this.keys = function () {
function safeKeys () {
// NOTE: this is a workaround for some browsers
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
}

this.keys = function () {
return safeKeys()
// filter any names not including sol:
.filter(function (item) { return item.indexOf('sol:', 0) === 0 })
// remove sol: from filename
.map(function (item) { return item.replace(/^sol:/, '') })
}

this.remove = function (name) {
window.localStorage.removeItem(name)
window.localStorage.removeItem('sol:' + name)
}

this.rename = function (originalName, newName) {
Expand All @@ -36,6 +44,18 @@ function Storage () {
}
this.set(filename, content)
}

// on startup, upgrade the old storage layout
safeKeys().forEach(function (name) {
if (name.indexOf('sol-cache-file-', 0) === 0) {
var content = window.localStorage.getItem(name)
window.localStorage.setItem(name.replace(/^sol-cache-file-/, 'sol:'), content)
window.localStorage.removeItem(name)
}
})

// remove obsolete key
window.localStorage.removeItem('editor-size-cache')
}

module.exports = Storage
21 changes: 0 additions & 21 deletions src/app/utils.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
'use strict'

var SOL_CACHE_FILE_PREFIX = 'sol-cache-file-'

function getCacheFilePrefix () {
return SOL_CACHE_FILE_PREFIX
}

function isCachedFile (name) {
return name.indexOf(getCacheFilePrefix(), 0) === 0
}

function fileKey (name) {
return getCacheFilePrefix() + name
}

function fileNameFromKey (key) {
return key.replace(getCacheFilePrefix(), '')
}

function errortype (message) {
return message.match(/^(.*:[0-9]*:[0-9]* )?Warning: /) ? 'warning' : 'error'
}

module.exports = {
isCachedFile: isCachedFile,
fileKey: fileKey,
fileNameFromKey: fileNameFromKey,
errortype: errortype
}

0 comments on commit 2064ac9

Please sign in to comment.