mirror of https://github.com/keeweb/keeweb.git
Added callbacks to settings store
This commit is contained in:
parent
ab243dda4d
commit
3f15c4a6b8
|
@ -1,48 +1,47 @@
|
|||
'use strict';
|
||||
|
||||
const Launcher = require('./launcher');
|
||||
const Launcher = false; // require('./launcher');
|
||||
const StringUtil = require('../util/string-util');
|
||||
const Logger = require('../util/logger');
|
||||
|
||||
const logger = new Logger('settings');
|
||||
|
||||
// TODO async
|
||||
const SettingsStore = {
|
||||
|
||||
useFileStore: function() {
|
||||
return Launcher && Launcher.platform() !== 'android';
|
||||
},
|
||||
|
||||
fileName: function(key) {
|
||||
return key + '.json';
|
||||
return `${key}.json`;
|
||||
},
|
||||
|
||||
load: function(key) {
|
||||
load: function(key, callback, error) {
|
||||
try {
|
||||
if (this.useFileStore()) {
|
||||
if (Launcher) {
|
||||
const settingsFile = Launcher.getUserDataPath(this.fileName(key));
|
||||
if (Launcher.fileExists(settingsFile)) {
|
||||
return JSON.parse(Launcher.readFile(settingsFile));
|
||||
}
|
||||
Launcher.fileExists(settingsFile, exists => {
|
||||
if (exists) {
|
||||
Launcher.readFile(settingsFile, data => {
|
||||
callback(JSON.parse(data));
|
||||
}, error);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const data = localStorage[StringUtil.camelCase(key)];
|
||||
return data ? JSON.parse(data) : undefined;
|
||||
callback(data ? JSON.parse(data) : undefined);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('Error loading ' + key, e);
|
||||
logger.error(`Error loading ${key}`, e);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
save: function(key, data) {
|
||||
save: function(key, data, callback, error) {
|
||||
try {
|
||||
if (this.useFileStore()) {
|
||||
Launcher.writeFile(Launcher.getUserDataPath(this.fileName(key)), JSON.stringify(data));
|
||||
if (Launcher) {
|
||||
const settingsFile = Launcher.getUserDataPath(this.fileName(key));
|
||||
Launcher.writeFile(settingsFile, JSON.stringify(data), callback, error);
|
||||
} else if (typeof localStorage !== 'undefined') {
|
||||
localStorage[StringUtil.camelCase(key)] = JSON.stringify(data);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('Error saving ' + key, e);
|
||||
logger.error(`Error saving ${key}`, e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue