Skip to content

Commit

Permalink
Merge pull request #411 from simianhacker/fix/395
Browse files Browse the repository at this point in the history
Split storage service in to two new services sessionStorage and localStorage
  • Loading branch information
spenceralger committed Sep 26, 2014
2 parents 1ae0293 + b1fbe37 commit b58bf21
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/kibana/components/courier/data_source/doc_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ define(function (require) {

var inherits = require('lodash').inherits;

return function DocSourceFactory(Private, Promise, es, storage) {
return function DocSourceFactory(Private, Promise, es, sessionStorage) {
var sendToEs = Private(require('components/courier/data_source/_doc_send_to_es'));
var SourceAbstract = Private(require('components/courier/data_source/_abstract'));

Expand Down Expand Up @@ -123,7 +123,7 @@ define(function (require) {
var key = this._versionKey();
if (!key) return;

var v = storage.get(key);
var v = sessionStorage.get(key);
this._version = v ? _.parseInt(v) : void 0;
return this._version;
};
Expand All @@ -139,7 +139,7 @@ define(function (require) {
var key = this._versionKey();
if (!key) return;
this._version = version;
storage.set(key, version);
sessionStorage.set(key, version);
};

/**
Expand All @@ -148,7 +148,7 @@ define(function (require) {
DocSource.prototype._clearVersion = function () {
var key = this._versionKey();
if (!key) return;
storage.remove(key);
sessionStorage.remove(key);
};

return DocSource;
Expand Down
6 changes: 3 additions & 3 deletions src/kibana/components/persisted_log/persisted_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ define(function (require) {
var _ = require('lodash');

modules.get('kibana/persisted_log')
.factory('PersistedLog', function ($window, storage) {
.factory('PersistedLog', function ($window, localStorage) {
function PersistedLog(name, options) {
options = options || {};
this.name = name;
this.maxLength = options.maxLength || 0;
this.filterDuplicates = options.filterDuplicates || false;
this.items = storage.get(this.name) || [];
this.items = localStorage.get(this.name) || [];
}

PersistedLog.prototype.add = function (val) {
Expand All @@ -35,7 +35,7 @@ define(function (require) {
}

// persist the stack
storage.set(this.name, stack);
localStorage.set(this.name, stack);
return this.items = stack;
};

Expand Down
61 changes: 33 additions & 28 deletions src/kibana/components/storage/storage.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
define(function (require) {
var modules = require('modules');

modules.get('kibana/storage')
.service('storage', function ($window) {
function Storage() {
var self = this;
self.store = $window.localStorage;
function Storage(store) {
var self = this;
self.store = store;

self.get = function (key) {
try {
return JSON.parse(self.store.getItem(key));
} catch (e) {
return null;
}
};

self.get = function (key) {
try {
return JSON.parse(self.store.getItem(key));
} catch (e) {
return null;
}
};
self.set = function (key, value) {
try {
return self.store.setItem(key, JSON.stringify(value));
} catch (e) {
return false;
}
};

self.set = function (key, value) {
try {
return self.store.setItem(key, JSON.stringify(value));
} catch (e) {
return false;
}
};
self.remove = function (key) {
return self.store.removeItem(key);
};

self.remove = function (key) {
return self.store.removeItem(key);
};
self.clear = function () {
return self.store.clear();
};
}

self.clear = function () {
return self.store.clear();
};
}
var createService = function (type) {
return function ($window) {
return new Storage($window[type]);
};
};

return new Storage();
});
modules.get('kibana/storage')
.service('localStorage', createService('localStorage'))
.service('sessionStorage', createService('sessionStorage'));
});
6 changes: 3 additions & 3 deletions src/kibana/controllers/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ define(function (require) {
config.init()
]).then(function () {
$scope.setupComplete = true;
$injector.invoke(function ($rootScope, courier, config, configFile, storage, $timeout, $location, timefilter, globalState) {
$injector.invoke(function ($rootScope, courier, config, configFile, sessionStorage, $timeout, $location, timefilter, globalState) {

$rootScope.globalState = globalState;

// get/set last path for an app
var lastPathFor = function (app, path) {
var key = 'lastPath:' + app.id;
if (path === void 0) {
app.lastPath = storage.get(key) || '/' + app.id;
app.lastPath = sessionStorage.get(key) || '/' + app.id;
return app.lastPath;
} else {
app.lastPath = path;
return storage.set(key, path);
return sessionStorage.set(key, path);
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/unit/specs/services/persisted_log.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ define(function (require) {
function init() {
module('kibana/persisted_log', function ($provide) {
// mock storage service
$provide.service('storage', function () {
$provide.service('localStorage', function () {
this.get = sinon.stub();
this.set = sinon.stub();
this.remove = sinon.spy();
Expand All @@ -27,7 +27,7 @@ define(function (require) {
});

inject(function ($injector) {
storage = $injector.get('storage');
storage = $injector.get('localStorage');
PersistedLog = $injector.get('PersistedLog');
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/specs/services/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define(function (require) {
});

inject(function ($injector) {
storage = $injector.get('storage');
storage = $injector.get('localStorage');
$window = $injector.get('$window');
});
}
Expand Down

0 comments on commit b58bf21

Please sign in to comment.