1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00
joplin/ReactNativeClient/src/models/setting.js

128 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-05-12 22:17:23 +02:00
import { BaseModel } from 'src/base-model.js';
import { Log } from 'src/log.js';
import { Database } from 'src/database.js';
class Setting extends BaseModel {
static tableName() {
return 'settings';
}
static defaultSetting(key) {
if (!(key in this.defaults_)) throw new Error('Unknown key: ' + key);
let output = Object.assign({}, this.defaults_[key]);
output.key = key;
return output;
}
2017-05-16 23:46:21 +02:00
static keys() {
if (this.keys_) return this.keys_;
this.keys_ = [];
for (let n in this.defaults_) {
if (!this.defaults_.hasOwnProperty(n)) continue;
this.keys_.push(n);
}
return this.keys_;
}
2017-05-12 22:17:23 +02:00
static load() {
this.cache_ = [];
2017-06-11 23:11:14 +02:00
return this.db().selectAll('SELECT * FROM settings').then((rows) => {
this.cache_ = rows;
2017-05-12 22:17:23 +02:00
});
}
static setValue(key, value) {
2017-06-19 20:58:49 +02:00
if (!this.cache_) throw new Error('Settings have not been initialized!');
2017-05-12 22:17:23 +02:00
for (let i = 0; i < this.cache_.length; i++) {
if (this.cache_[i].key == key) {
2017-05-19 21:12:09 +02:00
if (this.cache_[i].value === value) return;
2017-05-12 22:17:23 +02:00
this.cache_[i].value = value;
2017-05-19 21:12:09 +02:00
this.scheduleUpdate();
2017-05-12 22:17:23 +02:00
return;
}
}
let s = this.defaultSetting(key);
s.value = value;
this.cache_.push(s);
2017-05-19 21:12:09 +02:00
this.scheduleUpdate();
2017-05-12 22:17:23 +02:00
}
static value(key) {
2017-06-19 20:58:49 +02:00
if (!this.cache_) throw new Error('Settings have not been initialized!');
2017-05-12 22:17:23 +02:00
for (let i = 0; i < this.cache_.length; i++) {
if (this.cache_[i].key == key) {
return this.cache_[i].value;
}
}
let s = this.defaultSetting(key);
return s.value;
}
2017-05-16 23:46:21 +02:00
// Currently only supports objects with properties one level deep
static object(key) {
let output = {};
let keys = this.keys();
for (let i = 0; i < keys.length; i++) {
let k = keys[i].split('.');
if (k[0] == key) {
output[k[1]] = this.value(keys[i]);
}
}
return output;
}
// Currently only supports objects with properties one level deep
static setObject(key, object) {
for (let n in object) {
if (!object.hasOwnProperty(n)) continue;
this.setValue(key + '.' + n, object[n]);
}
}
2017-05-19 21:12:09 +02:00
static saveAll() {
if (!this.updateTimeoutId_) return Promise.resolve();
Log.info('Saving settings...');
clearTimeout(this.updateTimeoutId_);
this.updateTimeoutId_ = null;
2017-06-11 23:11:14 +02:00
let queries = [];
queries.push('DELETE FROM settings');
for (let i = 0; i < this.cache_.length; i++) {
queries.push(Database.insertQuery(this.tableName(), this.cache_[i]));
}
return BaseModel.db().transactionExecBatch(queries).then(() => {
2017-05-19 21:12:09 +02:00
Log.info('Settings have been saved.');
}).catch((error) => {
Log.warn('Could not save settings', error);
reject(error);
});
}
2017-05-12 22:17:23 +02:00
static scheduleUpdate() {
2017-05-19 21:12:09 +02:00
if (this.updateTimeoutId_) clearTimeout(this.updateTimeoutId_);
this.updateTimeoutId_ = setTimeout(() => {
this.saveAll();
2017-05-12 22:17:23 +02:00
}, 500);
}
}
2017-06-06 22:01:43 +02:00
Setting.defaults_ = {
'clientId': { value: '', type: 'string' },
'sessionId': { value: '', type: 'string' },
'user.email': { value: '', type: 'string' },
'user.session': { value: '', type: 'string' },
2017-06-12 23:56:27 +02:00
'sync.lastRevId': { value: 0, type: 'int' }, // DEPRECATED
'sync.lastUpdateTime': { value: 0, type: 'int' },
2017-06-19 20:58:49 +02:00
'sync.conflictFolderId': { value: '', type: 'string' },
2017-06-06 22:01:43 +02:00
};
2017-05-12 22:17:23 +02:00
export { Setting };