1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

All: Better handling of null values in settings

This commit is contained in:
Laurent Cozic 2018-01-07 19:20:10 +00:00
parent 8de5b4219d
commit adc5885980
4 changed files with 23 additions and 6 deletions

View File

@ -90,8 +90,8 @@ android {
applicationId "net.cozic.joplin" applicationId "net.cozic.joplin"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 22 targetSdkVersion 22
versionCode 84 versionCode 85
versionName "0.10.69" versionName "0.10.70"
ndk { ndk {
abiFilters "armeabi-v7a", "x86" abiFilters "armeabi-v7a", "x86"
} }

View File

@ -242,7 +242,9 @@ class Setting extends BaseModel {
if (md.type == Setting.TYPE_BOOL) return value ? '1' : '0'; if (md.type == Setting.TYPE_BOOL) return value ? '1' : '0';
if (md.type == Setting.TYPE_ARRAY) return value ? JSON.stringify(value) : '[]'; if (md.type == Setting.TYPE_ARRAY) return value ? JSON.stringify(value) : '[]';
if (md.type == Setting.TYPE_OBJECT) return value ? JSON.stringify(value) : '{}'; if (md.type == Setting.TYPE_OBJECT) return value ? JSON.stringify(value) : '{}';
return value; if (md.type == Setting.TYPE_STRING) return value ? value + '' : '';
throw new Error('Unhandled value type: ' + md.type);
} }
static formatValue(key, value) { static formatValue(key, value) {
@ -274,7 +276,12 @@ class Setting extends BaseModel {
return {}; return {};
} }
return value; if (md.type === Setting.TYPE_STRING) {
if (!value) return '';
return value + '';
}
throw new Error('Unhandled value type: ' + md.type);
} }
static value(key) { static value(key) {
@ -283,6 +290,7 @@ class Setting extends BaseModel {
// and object and change a key, the objects will be detected as equal. By returning a copy // and object and change a key, the objects will be detected as equal. By returning a copy
// we avoid this problem. // we avoid this problem.
function copyIfNeeded(value) { function copyIfNeeded(value) {
if (value === null || value === undefined) return value;
if (Array.isArray(value)) return value.slice(); if (Array.isArray(value)) return value.slice();
if (typeof value === 'object') return Object.assign({}, value); if (typeof value === 'object') return Object.assign({}, value);
return value; return value;

View File

@ -80,7 +80,17 @@ reg.scheduleSync = async (delay = null) => {
const contextKey = 'sync.' + syncTargetId + '.context'; const contextKey = 'sync.' + syncTargetId + '.context';
let context = Setting.value(contextKey); let context = Setting.value(contextKey);
try {
context = context ? JSON.parse(context) : {}; context = context ? JSON.parse(context) : {};
} catch (error) {
// Clearing the context is inefficient since it means all items are going to be re-downloaded
// however it won't result in duplicate items since the synchroniser is going to compare each
// item to the current state.
reg.logger().warn('Could not parse JSON sync context ' + contextKey + ':', context);
reg.logger().info('Clearing context and starting from scratch');
context = null;
}
try { try {
reg.logger().info('Starting scheduled sync'); reg.logger().info('Starting scheduled sync');
let newContext = await sync.start({ context: context }); let newContext = await sync.start({ context: context });

View File

@ -166,7 +166,6 @@ class Synchronizer {
let error = new Error(_('Synchronisation is already in progress. State: %s', this.state())); let error = new Error(_('Synchronisation is already in progress. State: %s', this.state()));
error.code = 'alreadyStarted'; error.code = 'alreadyStarted';
throw error; throw error;
return;
} }
this.state_ = 'in_progress'; this.state_ = 'in_progress';