From 3a950547f39ee3be4f86010d22cf3f5c9d755186 Mon Sep 17 00:00:00 2001 From: Gung Wah <41157464+kresnaputra@users.noreply.github.com> Date: Wed, 24 Feb 2021 02:36:20 +0800 Subject: [PATCH] [CHORE] Remove InteractionManager blocks (#2906) * [FIX] Remove InteractionManager blocks * Minor fix Co-authored-by: Diego Mello --- app/lib/methods/getCustomEmojis.js | 39 +++++++------- app/lib/methods/getPermissions.js | 13 ++--- app/lib/methods/getRoles.js | 63 +++++++++++------------ app/lib/methods/getSlashCommands.js | 73 +++++++++++++-------------- app/lib/methods/loadThreadMessages.js | 67 ++++++++++++------------ app/views/ThreadMessagesView/index.js | 18 ++----- 6 files changed, 122 insertions(+), 151 deletions(-) diff --git a/app/lib/methods/getCustomEmojis.js b/app/lib/methods/getCustomEmojis.js index 0037168df..1df285d84 100644 --- a/app/lib/methods/getCustomEmojis.js +++ b/app/lib/methods/getCustomEmojis.js @@ -1,4 +1,3 @@ -import { InteractionManager } from 'react-native'; import lt from 'semver/functions/lt'; import orderBy from 'lodash/orderBy'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; @@ -95,18 +94,16 @@ export function getCustomEmojis() { // RC 0.61.0 const result = await this.sdk.get('emoji-custom'); - InteractionManager.runAfterInteractions(async() => { - let { emojis } = result; - emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince); - const changedEmojis = await updateEmojis({ update: emojis, allRecords }); + let { emojis } = result; + emojis = emojis.filter(emoji => !updatedSince || emoji._updatedAt > updatedSince); + const changedEmojis = await updateEmojis({ update: emojis, allRecords }); - // `setCustomEmojis` is fired on selectServer - // We run it again only if emojis were changed - if (changedEmojis) { - setCustomEmojis(); - } - return resolve(); - }); + // `setCustomEmojis` is fired on selectServer + // We run it again only if emojis were changed + if (changedEmojis) { + setCustomEmojis(); + } + return resolve(); } else { const params = {}; if (updatedSince) { @@ -120,17 +117,15 @@ export function getCustomEmojis() { return resolve(); } - InteractionManager.runAfterInteractions(async() => { - const { emojis } = result; - const { update, remove } = emojis; - const changedEmojis = await updateEmojis({ update, remove, allRecords }); + const { emojis } = result; + const { update, remove } = emojis; + const changedEmojis = await updateEmojis({ update, remove, allRecords }); - // `setCustomEmojis` is fired on selectServer - // We run it again only if emojis were changed - if (changedEmojis) { - setCustomEmojis(); - } - }); + // `setCustomEmojis` is fired on selectServer + // We run it again only if emojis were changed + if (changedEmojis) { + setCustomEmojis(); + } } } catch (e) { log(e); diff --git a/app/lib/methods/getPermissions.js b/app/lib/methods/getPermissions.js index 249441582..06f30e91a 100644 --- a/app/lib/methods/getPermissions.js +++ b/app/lib/methods/getPermissions.js @@ -1,4 +1,3 @@ -import { InteractionManager } from 'react-native'; import lt from 'semver/functions/lt'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import orderBy from 'lodash/orderBy'; @@ -85,10 +84,8 @@ export default function() { if (!result.success) { return resolve(); } - InteractionManager.runAfterInteractions(async() => { - await updatePermissions({ update: result.permissions, allRecords }); - return resolve(); - }); + await updatePermissions({ update: result.permissions, allRecords }); + return resolve(); } else { const params = {}; const updatedSince = await getUpdatedSince(allRecords); @@ -102,10 +99,8 @@ export default function() { return resolve(); } - InteractionManager.runAfterInteractions(async() => { - await updatePermissions({ update: result.update, remove: result.delete, allRecords }); - return resolve(); - }); + await updatePermissions({ update: result.update, remove: result.delete, allRecords }); + return resolve(); } } catch (e) { log(e); diff --git a/app/lib/methods/getRoles.js b/app/lib/methods/getRoles.js index 3856d64f4..72f01bc6d 100644 --- a/app/lib/methods/getRoles.js +++ b/app/lib/methods/getRoles.js @@ -1,4 +1,3 @@ -import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import database from '../database'; @@ -19,43 +18,41 @@ export default function() { const { roles } = result; if (roles && roles.length) { - InteractionManager.runAfterInteractions(async() => { - await db.action(async() => { - const rolesCollections = db.collections.get('roles'); - const allRolesRecords = await rolesCollections.query().fetch(); + await db.action(async() => { + const rolesCollections = db.collections.get('roles'); + const allRolesRecords = await rolesCollections.query().fetch(); - // filter roles - let rolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id)); - let rolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id)); + // filter roles + let rolesToCreate = roles.filter(i1 => !allRolesRecords.find(i2 => i1._id === i2.id)); + let rolesToUpdate = allRolesRecords.filter(i1 => roles.find(i2 => i1.id === i2._id)); - // Create - rolesToCreate = rolesToCreate.map(role => rolesCollections.prepareCreate(protectedFunction((r) => { - r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema); - Object.assign(r, role); - }))); + // Create + rolesToCreate = rolesToCreate.map(role => rolesCollections.prepareCreate(protectedFunction((r) => { + r._raw = sanitizedRaw({ id: role._id }, rolesCollections.schema); + Object.assign(r, role); + }))); - // Update - rolesToUpdate = rolesToUpdate.map((role) => { - const newRole = roles.find(r => r._id === role.id); - return role.prepareUpdate(protectedFunction((r) => { - Object.assign(r, newRole); - })); - }); - - const allRecords = [ - ...rolesToCreate, - ...rolesToUpdate - ]; - - try { - await db.batch(...allRecords); - } catch (e) { - log(e); - } - return allRecords.length; + // Update + rolesToUpdate = rolesToUpdate.map((role) => { + const newRole = roles.find(r => r._id === role.id); + return role.prepareUpdate(protectedFunction((r) => { + Object.assign(r, newRole); + })); }); - return resolve(); + + const allRecords = [ + ...rolesToCreate, + ...rolesToUpdate + ]; + + try { + await db.batch(...allRecords); + } catch (e) { + log(e); + } + return allRecords.length; }); + return resolve(); } } catch (e) { log(e); diff --git a/app/lib/methods/getSlashCommands.js b/app/lib/methods/getSlashCommands.js index 729e8b4a9..33c75f1c0 100644 --- a/app/lib/methods/getSlashCommands.js +++ b/app/lib/methods/getSlashCommands.js @@ -1,4 +1,3 @@ -import { InteractionManager } from 'react-native'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import database from '../database'; @@ -20,47 +19,45 @@ export default function() { const { commands } = result; if (commands && commands.length) { - InteractionManager.runAfterInteractions(async() => { - await db.action(async() => { - const slashCommandsCollection = db.collections.get('slash_commands'); - const allSlashCommandsRecords = await slashCommandsCollection.query().fetch(); + await db.action(async() => { + const slashCommandsCollection = db.collections.get('slash_commands'); + const allSlashCommandsRecords = await slashCommandsCollection.query().fetch(); - // filter slash commands - 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 slashCommandsToDelete = allSlashCommandsRecords - .filter(i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id)); + // filter slash commands + 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 slashCommandsToDelete = allSlashCommandsRecords + .filter(i1 => !slashCommandsToCreate.find(i2 => i2.command === i1.id) && !slashCommandsToUpdate.find(i2 => i2.id === i1.id)); - // Create - slashCommandsToCreate = slashCommandsToCreate.map(command => slashCommandsCollection.prepareCreate(protectedFunction((s) => { - s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema); - Object.assign(s, command); - }))); + // Create + slashCommandsToCreate = slashCommandsToCreate.map(command => slashCommandsCollection.prepareCreate(protectedFunction((s) => { + s._raw = sanitizedRaw({ id: command.command }, slashCommandsCollection.schema); + Object.assign(s, command); + }))); - // Update - slashCommandsToUpdate = slashCommandsToUpdate.map((command) => { - const newCommand = commands.find(s => s.command === command.id); - return command.prepareUpdate(protectedFunction((s) => { - 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; + // Update + slashCommandsToUpdate = slashCommandsToUpdate.map((command) => { + const newCommand = commands.find(s => s.command === command.id); + return command.prepareUpdate(protectedFunction((s) => { + 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; }); } } catch (e) { diff --git a/app/lib/methods/loadThreadMessages.js b/app/lib/methods/loadThreadMessages.js index 4dc8a05fd..a3cda71be 100644 --- a/app/lib/methods/loadThreadMessages.js +++ b/app/lib/methods/loadThreadMessages.js @@ -1,4 +1,3 @@ -import { InteractionManager } from 'react-native'; import { Q } from '@nozbe/watermelondb'; 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 }); if (data && data.length) { - InteractionManager.runAfterInteractions(async() => { - try { - data = data.map(m => buildMessage(m)); - data = await Encryption.decryptMessages(data); - const db = database.active; - const threadMessagesCollection = db.collections.get('thread_messages'); - const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch(); - let threadMessagesToCreate = data.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id)); - let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => data.find(i2 => i1.id === i2._id)); + try { + data = data.map(m => buildMessage(m)); + data = await Encryption.decryptMessages(data); + const db = database.active; + const threadMessagesCollection = db.collections.get('thread_messages'); + const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch(); + let threadMessagesToCreate = data.filter(i1 => !allThreadMessagesRecords.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) => { - tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema); - Object.assign(tm, threadMessage); - tm.subscription.id = rid; + threadMessagesToCreate = threadMessagesToCreate.map(threadMessage => threadMessagesCollection.prepareCreate(protectedFunction((tm) => { + tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema); + Object.assign(tm, threadMessage); + 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; 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; - delete threadMessage.tmid; - })); - }); - - await db.action(async() => { - await db.batch( - ...threadMessagesToCreate, - ...threadMessagesToUpdate - ); - }); - } catch (e) { - log(e); - } - return resolve(data); - }); + await db.action(async() => { + await db.batch( + ...threadMessagesToCreate, + ...threadMessagesToUpdate + ); + }); + } catch (e) { + log(e); + } + return resolve(data); } else { return resolve([]); } diff --git a/app/views/ThreadMessagesView/index.js b/app/views/ThreadMessagesView/index.js index d75328a99..bf51d594f 100644 --- a/app/views/ThreadMessagesView/index.js +++ b/app/views/ThreadMessagesView/index.js @@ -1,6 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { FlatList, InteractionManager } from 'react-native'; +import { FlatList } from 'react-native'; import { connect } from 'react-redux'; import { Q } from '@nozbe/watermelondb'; import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; @@ -73,9 +73,7 @@ class ThreadMessagesView extends React.Component { componentDidMount() { this.mounted = true; - this.mountInteraction = InteractionManager.runAfterInteractions(() => { - this.init(); - }); + this.init(); } componentDidUpdate(prevProps) { @@ -89,12 +87,6 @@ class ThreadMessagesView extends React.Component { componentWillUnmount() { 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) { this.subSubscription.unsubscribe(); } @@ -330,10 +322,8 @@ class ThreadMessagesView extends React.Component { rid: this.rid, updatedSince: updatedSince.toISOString() }); if (result.success && result.threads) { - this.syncInteraction = InteractionManager.runAfterInteractions(() => { - const { update, remove } = result.threads; - this.updateThreads({ update, remove, lastThreadSync: updatedSince }); - }); + const { update, remove } = result.threads; + this.updateThreads({ update, remove, lastThreadSync: updatedSince }); } this.setState({ loading: false