[CHORE] Remove InteractionManager blocks (#2906)

* [FIX] Remove InteractionManager blocks

* Minor fix

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Gung Wah 2021-02-24 02:36:20 +08:00 committed by GitHub
parent 120f50d5d2
commit 3a950547f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 122 additions and 151 deletions

View File

@ -1,4 +1,3 @@
import { InteractionManager } from 'react-native';
import lt from 'semver/functions/lt'; import lt from 'semver/functions/lt';
import orderBy from 'lodash/orderBy'; import orderBy from 'lodash/orderBy';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
@ -95,18 +94,16 @@ export function getCustomEmojis() {
// RC 0.61.0 // RC 0.61.0
const result = await this.sdk.get('emoji-custom'); const result = await this.sdk.get('emoji-custom');
InteractionManager.runAfterInteractions(async() => { let { emojis } = result;
let { emojis } = result; emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince);
emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince); const changedEmojis = await updateEmojis({ update: emojis, allRecords });
const changedEmojis = await updateEmojis({ update: emojis, allRecords });
// `setCustomEmojis` is fired on selectServer // `setCustomEmojis` is fired on selectServer
// We run it again only if emojis were changed // We run it again only if emojis were changed
if (changedEmojis) { if (changedEmojis) {
setCustomEmojis(); setCustomEmojis();
} }
return resolve(); return resolve();
});
} else { } else {
const params = {}; const params = {};
if (updatedSince) { if (updatedSince) {
@ -120,17 +117,15 @@ export function getCustomEmojis() {
return resolve(); return resolve();
} }
InteractionManager.runAfterInteractions(async() => { const { emojis } = result;
const { emojis } = result; const { update, remove } = emojis;
const { update, remove } = emojis; const changedEmojis = await updateEmojis({ update, remove, allRecords });
const changedEmojis = await updateEmojis({ update, remove, allRecords });
// `setCustomEmojis` is fired on selectServer // `setCustomEmojis` is fired on selectServer
// We run it again only if emojis were changed // We run it again only if emojis were changed
if (changedEmojis) { if (changedEmojis) {
setCustomEmojis(); setCustomEmojis();
} }
});
} }
} catch (e) { } catch (e) {
log(e); log(e);

View File

@ -1,4 +1,3 @@
import { InteractionManager } from 'react-native';
import lt from 'semver/functions/lt'; import lt from 'semver/functions/lt';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import orderBy from 'lodash/orderBy'; import orderBy from 'lodash/orderBy';
@ -85,10 +84,8 @@ export default function() {
if (!result.success) { if (!result.success) {
return resolve(); return resolve();
} }
InteractionManager.runAfterInteractions(async() => { await updatePermissions({ update: result.permissions, allRecords });
await updatePermissions({ update: result.permissions, allRecords }); return resolve();
return resolve();
});
} else { } else {
const params = {}; const params = {};
const updatedSince = await getUpdatedSince(allRecords); const updatedSince = await getUpdatedSince(allRecords);
@ -102,10 +99,8 @@ export default function() {
return resolve(); return resolve();
} }
InteractionManager.runAfterInteractions(async() => { await updatePermissions({ update: result.update, remove: result.delete, allRecords });
await updatePermissions({ update: result.update, remove: result.delete, allRecords }); return resolve();
return resolve();
});
} }
} catch (e) { } catch (e) {
log(e); log(e);

View File

@ -1,4 +1,3 @@
import { InteractionManager } from 'react-native';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import database from '../database'; import database from '../database';
@ -19,43 +18,41 @@ export default function() {
const { roles } = result; const { roles } = result;
if (roles && roles.length) { if (roles && roles.length) {
InteractionManager.runAfterInteractions(async() => { await db.action(async() => {
await db.action(async() => { const rolesCollections = db.collections.get('roles');
const rolesCollections = db.collections.get('roles'); const allRolesRecords = await rolesCollections.query().fetch();
const allRolesRecords = await rolesCollections.query().fetch();
// filter roles // filter roles
let rolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id)); let rolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id));
let rolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id)); let rolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id));
// Create // Create
rolesToCreate = rolesToCreate.map(role => rolesCollections.prepareCreate(protectedFunction((r) => { rolesToCreate = rolesToCreate.map(role => rolesCollections.prepareCreate(protectedFunction((r) => {
r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema); r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema);
Object.assign(r, role); Object.assign(r, role);
}))); })));
// Update // Update
rolesToUpdate = rolesToUpdate.map((role) => { rolesToUpdate = rolesToUpdate.map((role) => {
const newRole = roles.find(r => r._id === role.id); const newRole = roles.find(r => r._id === role.id);
return role.prepareUpdate(protectedFunction((r) => { return role.prepareUpdate(protectedFunction((r) => {
Object.assign(r, newRole); Object.assign(r, newRole);
})); }));
});
const allRecords = [
...rolesToCreate,
...rolesToUpdate
];
try {
await db.batch(...allRecords);
} catch (e) {
log(e);
}
return allRecords.length;
}); });
return resolve();
const allRecords = [
...rolesToCreate,
...rolesToUpdate
];
try {
await db.batch(...allRecords);
} catch (e) {
log(e);
}
return allRecords.length;
}); });
return resolve();
} }
} catch (e) { } catch (e) {
log(e); log(e);

View File

@ -1,4 +1,3 @@
import { InteractionManager } from 'react-native';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
import database from '../database'; import database from '../database';
@ -20,47 +19,45 @@ export default function() {
const { commands } = result; const { commands } = result;
if (commands && commands.length) { if (commands && commands.length) {
InteractionManager.runAfterInteractions(async() => { await db.action(async() => {
await db.action(async() => { const slashCommandsCollection = db.collections.get('slash_commands');
const slashCommandsCollection = db.collections.get('slash_commands'); const allSlashCommandsRecords = await slashCommandsCollection.query().fetch();
const allSlashCommandsRecords = await slashCommandsCollection.query().fetch();
// filter slash commands // filter slash commands
let slashCommandsToCreate = commands.filter(i1 => !allSlashCommandsRecords.find(i2 => i1.command === i2.id)); let slashCommandsToCreate = commands.filter(i1 => !allSlashCommandsRecords.find(i2 => i1.command === i2.id));
let slashCommandsToUpdate = allSlashCommandsRecords.filter(i1 => commands.find(i2 => i1.id === i2.command)); let slashCommandsToUpdate = allSlashCommandsRecords.filter(i1 => commands.find(i2 => i1.id === i2.command));
let slashCommandsToDelete = allSlashCommandsRecords let slashCommandsToDelete = allSlashCommandsRecords
.filter(i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id)); .filter(i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id));
// Create // Create
slashCommandsToCreate = slashCommandsToCreate.map(command => slashCommandsCollection.prepareCreate(protectedFunction((s) => { slashCommandsToCreate = slashCommandsToCreate.map(command => slashCommandsCollection.prepareCreate(protectedFunction((s) => {
s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema); s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema);
Object.assign(s, command); Object.assign(s, command);
}))); })));
// Update // Update
slashCommandsToUpdate = slashCommandsToUpdate.map((command) => { slashCommandsToUpdate = slashCommandsToUpdate.map((command) => {
const newCommand = commands.find(s => s.command === command.id); const newCommand = commands.find(s => s.command === command.id);
return command.prepareUpdate(protectedFunction((s) => { return command.prepareUpdate(protectedFunction((s) => {
Object.assign(s, newCommand); Object.assign(s, newCommand);
})); }));
});
// Delete
slashCommandsToDelete = slashCommandsToDelete.map(command => command.prepareDestroyPermanently());
const allRecords = [
...slashCommandsToCreate,
...slashCommandsToUpdate,
...slashCommandsToDelete
];
try {
await db.batch(...allRecords);
} catch (e) {
log(e);
}
return allRecords.length;
}); });
// Delete
slashCommandsToDelete = slashCommandsToDelete.map(command => command.prepareDestroyPermanently());
const allRecords = [
...slashCommandsToCreate,
...slashCommandsToUpdate,
...slashCommandsToDelete
];
try {
await db.batch(...allRecords);
} catch (e) {
log(e);
}
return allRecords.length;
}); });
} }
} catch (e) { } catch (e) {

View File

@ -1,4 +1,3 @@
import { InteractionManager } from 'react-native';
import { Q } from '@nozbe/watermelondb'; import { Q } from '@nozbe/watermelondb';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
@ -30,44 +29,42 @@ export default function loadThreadMessages({ tmid, rid, offset = 0 }) {
let data = await load.call(this, { tmid, offset }); let data = await load.call(this, { tmid, offset });
if (data && data.length) { if (data && data.length) {
InteractionManager.runAfterInteractions(async() => { try {
try { data = data.map(m => buildMessage(m));
data = data.map(m => buildMessage(m)); data = await Encryption.decryptMessages(data);
data = await Encryption.decryptMessages(data); const db = database.active;
const db = database.active; const threadMessagesCollection = db.collections.get('thread_messages');
const threadMessagesCollection = db.collections.get('thread_messages'); const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch();
const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch(); let threadMessagesToCreate = data.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id));
let threadMessagesToCreate = data.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id)); let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => data.find(i2 => i1.id === i2._id));
let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => data.find(i2 => i1.id === i2._id));
threadMessagesToCreate = threadMessagesToCreate.map(threadMessage => threadMessagesCollection.prepareCreate(protectedFunction((tm) => { threadMessagesToCreate = threadMessagesToCreate.map(threadMessage => threadMessagesCollection.prepareCreate(protectedFunction((tm) => {
tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema); tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema);
Object.assign(tm, threadMessage); Object.assign(tm, threadMessage);
tm.subscription.id = rid; tm.subscription.id = rid;
tm.rid = threadMessage.tmid;
delete threadMessage.tmid;
})));
threadMessagesToUpdate = threadMessagesToUpdate.map((threadMessage) => {
const newThreadMessage = data.find(t => t._id === threadMessage.id);
return threadMessage.prepareUpdate(protectedFunction((tm) => {
Object.assign(tm, newThreadMessage);
tm.rid = threadMessage.tmid; tm.rid = threadMessage.tmid;
delete threadMessage.tmid; delete threadMessage.tmid;
}))); }));
});
threadMessagesToUpdate = threadMessagesToUpdate.map((threadMessage) => { await db.action(async() => {
const newThreadMessage = data.find(t => t._id === threadMessage.id); await db.batch(
return threadMessage.prepareUpdate(protectedFunction((tm) => { ...threadMessagesToCreate,
Object.assign(tm, newThreadMessage); ...threadMessagesToUpdate
tm.rid = threadMessage.tmid; );
delete threadMessage.tmid; });
})); } catch (e) {
}); log(e);
}
await db.action(async() => { return resolve(data);
await db.batch(
...threadMessagesToCreate,
...threadMessagesToUpdate
);
});
} catch (e) {
log(e);
}
return resolve(data);
});
} else { } else {
return resolve([]); return resolve([]);
} }

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { FlatList, InteractionManager } from 'react-native'; import { FlatList } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Q } from '@nozbe/watermelondb'; import { Q } from '@nozbe/watermelondb';
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
@ -73,9 +73,7 @@ class ThreadMessagesView extends React.Component {
componentDidMount() { componentDidMount() {
this.mounted = true; this.mounted = true;
this.mountInteraction = InteractionManager.runAfterInteractions(() => { this.init();
this.init();
});
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
@ -89,12 +87,6 @@ class ThreadMessagesView extends React.Component {
componentWillUnmount() { componentWillUnmount() {
console.countReset(`${ this.constructor.name }.render calls`); console.countReset(`${ this.constructor.name }.render calls`);
if (this.mountInteraction && this.mountInteraction.cancel) {
this.mountInteraction.cancel();
}
if (this.syncInteraction && this.syncInteraction.cancel) {
this.syncInteraction.cancel();
}
if (this.subSubscription && this.subSubscription.unsubscribe) { if (this.subSubscription && this.subSubscription.unsubscribe) {
this.subSubscription.unsubscribe(); this.subSubscription.unsubscribe();
} }
@ -330,10 +322,8 @@ class ThreadMessagesView extends React.Component {
rid: this.rid, updatedSince: updatedSince.toISOString() rid: this.rid, updatedSince: updatedSince.toISOString()
}); });
if (result.success && result.threads) { if (result.success && result.threads) {
this.syncInteraction = InteractionManager.runAfterInteractions(() => { const { update, remove } = result.threads;
const { update, remove } = result.threads; this.updateThreads({ update, remove, lastThreadSync: updatedSince });
this.updateThreads({ update, remove, lastThreadSync: updatedSince });
});
} }
this.setState({ this.setState({
loading: false loading: false