debounce calls to write to db last message

pull/1459/head
Audric Ackermann 4 years ago
parent c7d6e940ce
commit 2b92386399

@ -22,7 +22,7 @@
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
const { Contact, Message, PhoneNumber } = window.Signal.Types; const { Contact, Conversation, Message, PhoneNumber } = window.Signal.Types;
const { const {
upgradeMessageSchema, upgradeMessageSchema,
loadAttachmentData, loadAttachmentData,
@ -58,25 +58,21 @@
// our first save to the database. Or first fetch from the database. // our first save to the database. Or first fetch from the database.
this.initialPromise = Promise.resolve(); this.initialPromise = Promise.resolve();
this.contactCollection = this.getContactCollection();
this.messageCollection = new Whisper.MessageCollection([], { this.messageCollection = new Whisper.MessageCollection([], {
conversation: this, conversation: this,
}); });
this.throttledBumpTyping = _.throttle(this.bumpTyping, 300); this.throttledBumpTyping = _.throttle(this.bumpTyping, 300);
const debouncedUpdateLastMessage = _.debounce( this.updateLastMessage = _.throttle(
this.updateLastMessage.bind(this), this.bouncyUpdateLastMessage.bind(this),
200 1000
); );
this.listenTo( // this.listenTo(
this.messageCollection, // this.messageCollection,
'add remove destroy', // 'add remove destroy',
debouncedUpdateLastMessage // debouncedUpdateLastMessage
); // );
this.on('newmessage', this.onNewMessage);
// Listening for out-of-band data updates // Listening for out-of-band data updates
this.on('updateMessage', this.updateAndMerge);
this.on('delivered', this.updateAndMerge); this.on('delivered', this.updateAndMerge);
this.on('read', this.updateAndMerge); this.on('read', this.updateAndMerge);
this.on('expiration-change', this.updateAndMerge); this.on('expiration-change', this.updateAndMerge);
@ -289,7 +285,6 @@
existing.merge(message.attributes); existing.merge(message.attributes);
}; };
await this.inProgressFetch;
mergeMessage(); mergeMessage();
}, },
@ -311,11 +306,6 @@
existing.trigger('expired'); existing.trigger('expired');
}; };
// If a fetch is in progress, then we need to wait until that's complete to
// do this removal. Otherwise we could remove from messageCollection, then
// the async database fetch could include the removed message.
await this.inProgressFetch;
removeMessage(); removeMessage();
}, },
@ -348,26 +338,6 @@
await model.setServerTimestamp(serverTimestamp); await model.setServerTimestamp(serverTimestamp);
return undefined; return undefined;
}, },
async onNewMessage(message) {
await this.updateLastMessage();
// Clear typing indicator for a given contact if we receive a message from them
const identifier = message.get
? `${message.get('source')}.${message.get('sourceDevice')}`
: `${message.source}.${message.sourceDevice}`;
this.clearContactTypingTimer(identifier);
const model = this.addSingleMessage(message);
getMessageController().register(model.id, model);
window.Whisper.events.trigger('messageAdded', {
conversationKey: this.id,
messageModel: model,
});
this.commit();
},
addSingleMessage(message, setToExpire = true) { addSingleMessage(message, setToExpire = true) {
const model = this.messageCollection.add(message, { merge: true }); const model = this.messageCollection.add(message, { merge: true });
if (setToExpire) { if (setToExpire) {
@ -407,7 +377,6 @@
unreadCount: this.get('unreadCount') || 0, unreadCount: this.get('unreadCount') || 0,
mentionedUs: this.get('mentionedUs') || false, mentionedUs: this.get('mentionedUs') || false,
isBlocked: this.isBlocked(), isBlocked: this.isBlocked(),
primaryDevice: this.id,
phoneNumber: format(this.id, { phoneNumber: format(this.id, {
ourRegionCode: regionCode, ourRegionCode: regionCode,
}), }),

@ -34,7 +34,6 @@ export type ConversationListItemProps = {
avatarPath?: string; avatarPath?: string;
isMe: boolean; isMe: boolean;
isPublic?: boolean; isPublic?: boolean;
primaryDevice?: string;
lastUpdated: number; lastUpdated: number;
unreadCount: number; unreadCount: number;

@ -66,7 +66,6 @@ export type ConversationType = {
mentionedUs: boolean; mentionedUs: boolean;
isSelected: boolean; isSelected: boolean;
isTyping: boolean; isTyping: boolean;
primaryDevice: string;
isBlocked: boolean; isBlocked: boolean;
isKickedFromGroup: boolean; isKickedFromGroup: boolean;
left: boolean; left: boolean;

@ -125,7 +125,7 @@ export const _getLeftPaneLists = (
}; };
} }
const isBlocked = const isBlocked =
BlockedNumberController.isBlocked(conversation.primaryDevice) || BlockedNumberController.isBlocked(conversation.id) ||
BlockedNumberController.isGroupBlocked(conversation.id); BlockedNumberController.isGroupBlocked(conversation.id);
if (isBlocked) { if (isBlocked) {

@ -18,7 +18,6 @@ describe('state/selectors/conversations', () => {
name: 'No timestamp', name: 'No timestamp',
timestamp: 0, timestamp: 0,
phoneNumber: 'notused', phoneNumber: 'notused',
primaryDevice: 'id1',
type: 'direct', type: 'direct',
isMe: false, isMe: false,
@ -37,7 +36,6 @@ describe('state/selectors/conversations', () => {
name: 'B', name: 'B',
timestamp: 20, timestamp: 20,
phoneNumber: 'notused', phoneNumber: 'notused',
primaryDevice: 'id2',
type: 'direct', type: 'direct',
isMe: false, isMe: false,
@ -56,7 +54,6 @@ describe('state/selectors/conversations', () => {
name: 'C', name: 'C',
timestamp: 20, timestamp: 20,
phoneNumber: 'notused', phoneNumber: 'notused',
primaryDevice: 'id3',
type: 'direct', type: 'direct',
isMe: false, isMe: false,
@ -75,7 +72,6 @@ describe('state/selectors/conversations', () => {
name: 'Á', name: 'Á',
timestamp: 20, timestamp: 20,
phoneNumber: 'notused', phoneNumber: 'notused',
primaryDevice: 'id4',
type: 'direct', type: 'direct',
isMe: false, isMe: false,
lastUpdated: Date.now(), lastUpdated: Date.now(),
@ -93,7 +89,6 @@ describe('state/selectors/conversations', () => {
name: 'First!', name: 'First!',
timestamp: 30, timestamp: 30,
phoneNumber: 'notused', phoneNumber: 'notused',
primaryDevice: 'id5',
type: 'direct', type: 'direct',
isMe: false, isMe: false,
lastUpdated: Date.now(), lastUpdated: Date.now(),

Loading…
Cancel
Save