From 9df5608ac64451d7a1a66232591d0e720c8a58cd Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 18 Jan 2017 16:48:47 +0000 Subject: [PATCH 1/6] Add files API --- src/app/files.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/app/files.js diff --git a/src/app/files.js b/src/app/files.js new file mode 100644 index 0000000000..6bdbd808d1 --- /dev/null +++ b/src/app/files.js @@ -0,0 +1,91 @@ +'use strict' + +function Files (storage) { + var readonly = {} + + this.exists = function (path) { + // NOTE: ignore the config file + if (path === '.browser-solidity.json') { + return + } + + if (this.isReadOnly(path)) { + return true + } else { + return storage.exists(path) + } + } + + this.get = function (path) { + // NOTE: ignore the config file + if (path === '.browser-solidity.json') { + return + } + + if (this.isReadOnly(path)) { + return readonly[path] + } else { + return storage.get(path) + } + } + + this.set = function (path, content) { + // NOTE: ignore the config file + if (path === '.browser-solidity.json') { + return + } + + if (!this.isReadOnly(path)) { + storage.set(path, content) + } + } + + this.addReadOnly = function (path, content) { + if (!storage.exists(path)) { + readonly[path] = content + } + } + + this.isReadOnly = function (path) { + return !!readonly[path] + } + + this.remove = function (path) { + if (!this.exists(path)) { + return + } + + if (this.isReadOnly(path)) { + readonly[path] = undefined + } else { + storage.remove(path) + } + } + + this.rename = function (oldPath, newPath) { + if (!this.isReadOnly(oldPath) && storage.exists(oldPath)) { + storage.rename(oldPath, newPath) + } + } + + this.list = function () { + var files = {} + + // add r/w files to the list + storage.keys().forEach(function (path) { + // NOTE: as a temporary measure do not show the config file + if (path !== '.browser-solidity.json') { + files[path] = false + } + }) + + // add r/o files to the list + Object.keys(readonly).forEach(function (path) { + files[path] = true + }) + + return files + } +} + +module.exports = Files From c4e4d366293444f7af0c058a52b4be6fe8335561 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 18 Jan 2017 17:00:32 +0000 Subject: [PATCH 2/6] Add events to files API --- src/app/files.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/app/files.js b/src/app/files.js index 6bdbd808d1..760666634e 100644 --- a/src/app/files.js +++ b/src/app/files.js @@ -1,6 +1,10 @@ 'use strict' +var EventManager = require('../lib/eventManager') + function Files (storage) { + var event = new EventManager() + this.event = event var readonly = {} this.exists = function (path) { @@ -36,13 +40,20 @@ function Files (storage) { } if (!this.isReadOnly(path)) { + var exists = storage.exists(path) storage.set(path, content) + if (!exists) { + event.trigger('fileAdded', [path]) + } else { + event.trigger('fileChanged', [path]) + } } } this.addReadOnly = function (path, content) { if (!storage.exists(path)) { readonly[path] = content + event.trigger('fileAdded', [path]) } } @@ -60,11 +71,13 @@ function Files (storage) { } else { storage.remove(path) } + event.trigger('fileRemoved', [path]) } this.rename = function (oldPath, newPath) { if (!this.isReadOnly(oldPath) && storage.exists(oldPath)) { storage.rename(oldPath, newPath) + event.trigger('fileRenamed', [oldPath, newPath]) } } From 60fc07ee8fdbbbab82174f23aab30d2b7bf53c6f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 19 Jan 2017 14:36:10 +0000 Subject: [PATCH 3/6] Add boolean to set/remove/rename in storage --- src/app/storage.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app/storage.js b/src/app/storage.js index d0cca22776..6222843d28 100644 --- a/src/app/storage.js +++ b/src/app/storage.js @@ -10,7 +10,12 @@ function Storage () { } this.set = function (name, content) { - window.localStorage.setItem('sol:' + name, content) + try { + window.localStorage.setItem('sol:' + name, content) + } catch (exception) { + return false + } + return true } function safeKeys () { @@ -28,12 +33,16 @@ function Storage () { this.remove = function (name) { window.localStorage.removeItem('sol:' + name) + return true } this.rename = function (originalName, newName) { var content = this.get(originalName) - this.set(newName, content) + if (!this.set(newName, content)) { + return false + } this.remove(originalName) + return true } this.loadFile = function (filename, content) { From abeccd51e73e7d95e032e7e321aeac2f86e66127 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 19 Jan 2017 14:38:44 +0000 Subject: [PATCH 4/6] Include comprehensive error handling in files API (always return true/false) --- src/app/files.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/app/files.js b/src/app/files.js index 760666634e..afb29d856f 100644 --- a/src/app/files.js +++ b/src/app/files.js @@ -10,7 +10,7 @@ function Files (storage) { this.exists = function (path) { // NOTE: ignore the config file if (path === '.browser-solidity.json') { - return + return false } if (this.isReadOnly(path)) { @@ -23,7 +23,7 @@ function Files (storage) { this.get = function (path) { // NOTE: ignore the config file if (path === '.browser-solidity.json') { - return + return null } if (this.isReadOnly(path)) { @@ -36,25 +36,33 @@ function Files (storage) { this.set = function (path, content) { // NOTE: ignore the config file if (path === '.browser-solidity.json') { - return + return false } if (!this.isReadOnly(path)) { var exists = storage.exists(path) - storage.set(path, content) + if (!storage.set(path, content)) { + return false + } if (!exists) { event.trigger('fileAdded', [path]) } else { event.trigger('fileChanged', [path]) } + return true } + + return false } this.addReadOnly = function (path, content) { if (!storage.exists(path)) { readonly[path] = content event.trigger('fileAdded', [path]) + return true } + + return false } this.isReadOnly = function (path) { @@ -63,22 +71,29 @@ function Files (storage) { this.remove = function (path) { if (!this.exists(path)) { - return + return false } if (this.isReadOnly(path)) { readonly[path] = undefined } else { - storage.remove(path) + if (!storage.remove(path)) { + return false + } } event.trigger('fileRemoved', [path]) + return true } this.rename = function (oldPath, newPath) { if (!this.isReadOnly(oldPath) && storage.exists(oldPath)) { - storage.rename(oldPath, newPath) + if (!storage.rename(oldPath, newPath)) { + return false + } event.trigger('fileRenamed', [oldPath, newPath]) + return true } + return false } this.list = function () { From 124bb9880a236610e933bfa8f3d3bdc94cf6395f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 23 Jan 2017 11:10:00 +0000 Subject: [PATCH 5/6] Include flag in fileAdded event for readonly files --- src/app/files.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/files.js b/src/app/files.js index afb29d856f..945a77351f 100644 --- a/src/app/files.js +++ b/src/app/files.js @@ -45,7 +45,7 @@ function Files (storage) { return false } if (!exists) { - event.trigger('fileAdded', [path]) + event.trigger('fileAdded', [path, false]) } else { event.trigger('fileChanged', [path]) } @@ -58,7 +58,7 @@ function Files (storage) { this.addReadOnly = function (path, content) { if (!storage.exists(path)) { readonly[path] = content - event.trigger('fileAdded', [path]) + event.trigger('fileAdded', [path, true]) return true } From 20ce7788b82859352bd5c934048cc2ec1b36b70f Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 24 Jan 2017 11:30:05 +0000 Subject: [PATCH 6/6] Simplify files.get / files.exists --- src/app/files.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/app/files.js b/src/app/files.js index 945a77351f..3cae6ef0ad 100644 --- a/src/app/files.js +++ b/src/app/files.js @@ -13,11 +13,7 @@ function Files (storage) { return false } - if (this.isReadOnly(path)) { - return true - } else { - return storage.exists(path) - } + return this.isReadOnly(path) || storage.exists(path) } this.get = function (path) { @@ -26,11 +22,7 @@ function Files (storage) { return null } - if (this.isReadOnly(path)) { - return readonly[path] - } else { - return storage.get(path) - } + return readonly[path] || storage.get(path) } this.set = function (path, content) {