nightTab/js/data.js

65 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-12-26 08:45:53 +01:00
var data = (function() {
var saveName = "nitghTab";
var set = function(key, data) {
localStorage.setItem(key, data);
};
var get = function(key) {
return localStorage.getItem(key);
};
var remove = function(key) {
2018-12-26 08:45:53 +01:00
localStorage.removeItem(key);
};
var save = function() {
2019-06-17 11:23:57 +02:00
var data = {
version: version.get(),
state: state.get(),
bookmarks: bookmarks.get()
2019-06-17 01:08:35 +02:00
};
2019-06-17 11:23:57 +02:00
set(saveName, JSON.stringify(data));
2018-12-26 08:45:53 +01:00
};
var wipe = function() {
remove(saveName);
};
var load = function() {
2019-05-26 10:09:15 +02:00
return JSON.parse(get(saveName));
2018-12-26 08:45:53 +01:00
};
2019-06-17 01:08:35 +02:00
var restore = function() {
var data = load();
2019-01-06 16:47:08 +01:00
if (data) {
2019-06-17 11:23:57 +02:00
if (!("version" in data) || data.version != version.get()) {
console.log("data version " + data.version + " found less than current");
data = update.run(data);
set(saveName, JSON.stringify(data));
} else {
console.log("data version " + version.get() + " no need to run update");
2019-01-06 16:47:08 +01:00
};
2019-06-17 11:23:57 +02:00
} else {
console.log("no data found to load");
2019-01-06 16:47:08 +01:00
};
};
var init = function() {
2019-06-17 01:08:35 +02:00
restore();
2019-01-06 16:47:08 +01:00
};
2018-12-26 08:45:53 +01:00
return {
2019-01-06 16:47:08 +01:00
init: init,
2018-12-26 08:45:53 +01:00
save: save,
remove: remove,
2018-12-26 08:45:53 +01:00
set: set,
get: get,
2019-01-06 16:47:08 +01:00
load: load,
wipe: wipe,
restore: restore
2018-12-26 08:45:53 +01:00
};
})();