[FIX] getSettings not catching errors (#2271)
This commit is contained in:
parent
aef7125887
commit
f3b02a6a88
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue