[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 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);

View File

@ -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);

View File

@ -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);

View File

@ -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) {

View File

@ -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([]);
}

View File

@ -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