From 92da20157e39d039597fc6e411a09e9484939cf3 Mon Sep 17 00:00:00 2001 From: ifedapoolarewaju Date: Tue, 5 Nov 2019 10:07:44 +0100 Subject: [PATCH] cache older messages per convo --- browser/js/funcs.js | 7 ++++--- browser/js/globals.js | 2 +- browser/js/index.js | 7 ++++--- browser/js/renderers.js | 6 ++---- main/main.js | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/browser/js/funcs.js b/browser/js/funcs.js index 50b73a3..b78ba76 100644 --- a/browser/js/funcs.js +++ b/browser/js/funcs.js @@ -79,15 +79,16 @@ function loadOlderMsgsOnScrollTop(chatId) { let msgContainer = document.querySelector(CHAT_WINDOW_SELECTOR); msgContainer.onscroll = (e) => { if (e.target.scrollTop < 200 && !window.gettingOlderMessages && window.currentChatId == chatId) { - ipcRenderer.send('getOlderMessages', window.currentChatId); + ipcRenderer.send('getOlderMessages', chatId); window.gettingOlderMessages = true; window.olderMessagesChatId = window.currentChatId; } } } -function canRenderOlderMessages() { - return window.olderMessagesChatId === window.currentChatId; +function canRenderOlderMessages(chatId) { + chatId = chatId || window.olderMessagesChatId; + return chatId === window.currentChatId; } function getMsgPreview (chat_) { diff --git a/browser/js/globals.js b/browser/js/globals.js index 838de55..c0175a1 100644 --- a/browser/js/globals.js +++ b/browser/js/globals.js @@ -19,6 +19,6 @@ window.loggedInUser = null; window.shouldSendSeenFlags = true; window.isWindowFocused = true; window.shouldNotify = true; -window.olderMessages = []; +window.olderMessages = {}; window.gettingOlderMessages = false; window.olderMessagesChatId = null; diff --git a/browser/js/index.js b/browser/js/index.js index b58bd4b..6cea70a 100644 --- a/browser/js/index.js +++ b/browser/js/index.js @@ -64,9 +64,10 @@ document.addEventListener('DOMContentLoaded', () => { if (isNewMessage && isCurrentChat(chat_) && !window.gettingOlderMessages) renderChat(chat_); }); - ipcRenderer.on('olderMessages', (_, messages) => { - if (canRenderOlderMessages()) { - window.olderMessages = window.olderMessages.concat(messages); + ipcRenderer.on('olderMessages', (_, {chatId, messages}) => { + const previous = window.olderMessages[chatId] || []; + window.olderMessages[chatId] = previous.concat(messages); + if (canRenderOlderMessages(chatId)) { renderOlderMessages(messages); } // reset the value only after all is done. So don't move this up diff --git a/browser/js/renderers.js b/browser/js/renderers.js index 433df39..eb2c51a 100644 --- a/browser/js/renderers.js +++ b/browser/js/renderers.js @@ -322,10 +322,8 @@ function renderChat (chat_, loadingMore) { } renderChatHeader(chat_); var messages = chat_.items.slice().reverse(); - if (canRenderOlderMessages()) { - // load older messages if they exist too - messages = window.olderMessages.slice().reverse().concat(messages); - } + // load older messages if they exist too + messages = (window.olderMessages[chat_.id] || []).slice().reverse().concat(messages); messages.forEach((message) => { var div = renderMessage(message, getMsgDirection(message), message._params.created, message._params.type diff --git a/main/main.js b/main/main.js index bcd208a..e31de6a 100644 --- a/main/main.js +++ b/main/main.js @@ -224,7 +224,7 @@ electron.ipcMain.on('getOlderMessages', (_, id) => { instagram.getOlderMessages(session, messagesThread, id) .then((data) => { messagesThread = data.thread - mainWindow.webContents.send('olderMessages', data.messages) + mainWindow.webContents.send('olderMessages', {chatId: id, messages: data.messages}) }) })