Skip to content

Commit

Permalink
cache older messages per convo
Browse files Browse the repository at this point in the history
  • Loading branch information
ifedapoolarewaju committed Nov 5, 2019
1 parent fccb7f3 commit 92da201
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
7 changes: 4 additions & 3 deletions browser/js/funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand Down
2 changes: 1 addition & 1 deletion browser/js/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 4 additions & 3 deletions browser/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions browser/js/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
})
})

Expand Down

0 comments on commit 92da201

Please sign in to comment.