mirror of
https://github.com/nasa/openmct.git
synced 2025-02-07 19:40:28 +00:00
[Persistence] Update code style
Update code style for local storage based persistence.
This commit is contained in:
parent
36e38b9454
commit
048eb20450
@ -21,66 +21,75 @@
|
|||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
/*global define,localStorage*/
|
/*global define,localStorage*/
|
||||||
/**
|
|
||||||
* Stubbed implementation of a persistence provider,
|
|
||||||
* to permit objects to be created, saved, etc.
|
|
||||||
*/
|
|
||||||
define(
|
define(
|
||||||
[],
|
[],
|
||||||
function () {
|
function () {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function BrowserPersistenceProvider($q, SPACE) {
|
/**
|
||||||
var spaces = SPACE ? [SPACE] : [],
|
* The LocalStoragePersistenceProvider reads and writes JSON documents
|
||||||
promises = {
|
* (more specifically, domain object models) to/from the browser's
|
||||||
as: function (value) {
|
* local storage.
|
||||||
return $q.when(value);
|
* @memberof platform/persistence/local
|
||||||
|
* @constructor
|
||||||
|
* @implements {PersistenceService}
|
||||||
|
* @param q Angular's $q, for promises
|
||||||
|
* @param $interval Angular's $interval service
|
||||||
|
* @param {string} space the name of the persistence space being served
|
||||||
|
*/
|
||||||
|
function LocalStoragePersistenceProvider($q, space) {
|
||||||
|
this.$q = $q;
|
||||||
|
this.space = space;
|
||||||
|
this.spaces = space ? [space] : [];
|
||||||
}
|
}
|
||||||
},
|
|
||||||
provider;
|
|
||||||
|
|
||||||
function setValue(key, value) {
|
/**
|
||||||
|
* Set a value in local storage.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
LocalStoragePersistenceProvider.prototype.setValue = function (key, value) {
|
||||||
localStorage[key] = JSON.stringify(value);
|
localStorage[key] = JSON.stringify(value);
|
||||||
}
|
|
||||||
|
|
||||||
function getValue(key) {
|
|
||||||
if (localStorage[key]) {
|
|
||||||
return JSON.parse(localStorage[key]);
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
provider = {
|
|
||||||
listSpaces: function () {
|
|
||||||
return promises.as(spaces);
|
|
||||||
},
|
|
||||||
listObjects: function (space) {
|
|
||||||
var space_obj = getValue(space);
|
|
||||||
return promises.as(Object.keys(space_obj));
|
|
||||||
},
|
|
||||||
createObject: function (space, key, value) {
|
|
||||||
var space_obj = getValue(space);
|
|
||||||
space_obj[key] = value;
|
|
||||||
setValue(space, space_obj);
|
|
||||||
return promises.as(true);
|
|
||||||
},
|
|
||||||
readObject: function (space, key) {
|
|
||||||
var space_obj = getValue(space);
|
|
||||||
return promises.as(space_obj[key]);
|
|
||||||
},
|
|
||||||
deleteObject: function (space, key, value) {
|
|
||||||
var space_obj = getValue(space);
|
|
||||||
delete space_obj[key];
|
|
||||||
return promises.as(true);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
provider.updateObject = provider.createObject;
|
/**
|
||||||
|
* Get a value from local storage.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
LocalStoragePersistenceProvider.prototype.getValue = function (key) {
|
||||||
|
return localStorage[key] ? JSON.parse(localStorage[key]) : {};
|
||||||
|
};
|
||||||
|
|
||||||
return provider;
|
LocalStoragePersistenceProvider.prototype.listSpaces = function () {
|
||||||
|
return this.$q.when(this.spaces);
|
||||||
|
};
|
||||||
|
|
||||||
}
|
LocalStoragePersistenceProvider.prototype.listObjects = function (space) {
|
||||||
|
return this.$q.when(Object.keys(this.getValue(space)));
|
||||||
|
};
|
||||||
|
|
||||||
return BrowserPersistenceProvider;
|
LocalStoragePersistenceProvider.prototype.createObject = function (space, key, value) {
|
||||||
|
var spaceObj = this.getValue(space);
|
||||||
|
spaceObj[key] = value;
|
||||||
|
this.setValue(space, spaceObj);
|
||||||
|
return this.$q.when(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
LocalStoragePersistenceProvider.prototype.readObject = function (space, key) {
|
||||||
|
var spaceObj = this.getValue(space);
|
||||||
|
return this.$q.when(spaceObj[key]);
|
||||||
|
};
|
||||||
|
|
||||||
|
LocalStoragePersistenceProvider.prototype.deleteObject = function (space, key, value) {
|
||||||
|
var spaceObj = this.getValue(space);
|
||||||
|
delete spaceObj[key];
|
||||||
|
this.setValue(space, spaceObj);
|
||||||
|
return this.$q.when(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
LocalStoragePersistenceProvider.prototype.updateObject =
|
||||||
|
LocalStoragePersistenceProvider.prototype.createObject;
|
||||||
|
|
||||||
|
return LocalStoragePersistenceProvider;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user