From f3b02a6a88b9e942f9f0199a0e0e9c9e82ebed17 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Thu, 9 Jul 2020 10:33:04 -0300 Subject: [PATCH] [FIX] getSettings not catching errors (#2271) --- app/lib/methods/getSettings.js | 72 +++++++++++++++++----------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 7a6e351cf..e635807e9 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -1,4 +1,3 @@ -import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { Q } from '@nozbe/watermelondb'; @@ -132,48 +131,47 @@ export default async function() { const filteredSettingsIds = filteredSettings.map(s => s._id); reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings))); - InteractionManager.runAfterInteractions(async() => { - // filter server info - const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id)); - const iconSetting = data.find(item => item._id === 'Assets_favicon_512'); - await serverInfoUpdate(serverInfo, iconSetting); - await db.action(async() => { - const settingsCollection = db.collections.get('settings'); - const allSettingsRecords = await settingsCollection - .query(Q.where('id', Q.oneOf(filteredSettingsIds))) - .fetch(); + // filter server info + const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id)); + const iconSetting = data.find(item => item._id === 'Assets_favicon_512'); + await serverInfoUpdate(serverInfo, iconSetting); - // filter settings - let settingsToCreate = filteredSettings.filter(i1 => !allSettingsRecords.find(i2 => i1._id === i2.id)); - let settingsToUpdate = allSettingsRecords.filter(i1 => filteredSettings.find(i2 => i1.id === i2._id)); + await db.action(async() => { + const settingsCollection = db.collections.get('settings'); + const allSettingsRecords = await settingsCollection + .query(Q.where('id', Q.oneOf(filteredSettingsIds))) + .fetch(); - // Create - settingsToCreate = settingsToCreate.map(setting => settingsCollection.prepareCreate(protectedFunction((s) => { - s._raw = sanitizedRaw({ id: setting._id }, settingsCollection.schema); - Object.assign(s, setting); - }))); + // filter settings + let settingsToCreate = filteredSettings.filter(i1 => !allSettingsRecords.find(i2 => i1._id === i2.id)); + let settingsToUpdate = allSettingsRecords.filter(i1 => filteredSettings.find(i2 => i1.id === i2._id)); - // Update - settingsToUpdate = settingsToUpdate.map((setting) => { - const newSetting = filteredSettings.find(s => s._id === setting.id); - return setting.prepareUpdate(protectedFunction((s) => { - Object.assign(s, newSetting); - })); - }); + // Create + settingsToCreate = settingsToCreate.map(setting => settingsCollection.prepareCreate(protectedFunction((s) => { + s._raw = sanitizedRaw({ id: setting._id }, settingsCollection.schema); + Object.assign(s, setting); + }))); - const allRecords = [ - ...settingsToCreate, - ...settingsToUpdate - ]; - - try { - await db.batch(...allRecords); - } catch (e) { - log(e); - } - return allRecords.length; + // Update + settingsToUpdate = settingsToUpdate.map((setting) => { + const newSetting = filteredSettings.find(s => s._id === setting.id); + return setting.prepareUpdate(protectedFunction((s) => { + Object.assign(s, newSetting); + })); }); + + const allRecords = [ + ...settingsToCreate, + ...settingsToUpdate + ]; + + try { + await db.batch(...allRecords); + } catch (e) { + log(e); + } + return allRecords.length; }); } catch (e) { log(e);