Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #81 from jmarino/clean-seenMessages
Browse files Browse the repository at this point in the history
Clean seenMessages variable as new messages are read
  • Loading branch information
denysdovhan authored Oct 18, 2018
2 parents fbdf9c4 + eab08be commit 7630808
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions app/renderer/unreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ function extractSender(el, message) {
}

function getUnreadMessages() {
// not inside the inbox
const isInbox = $('.hA [title=Inbox]');

if (!isInbox) {
return [];
}

return Array.from($$('.ss'))
.map((message) => {
const ancestorEl = ancestor(message, '.jS');
Expand All @@ -68,18 +61,33 @@ function getUnreadMessages() {
}

function checkUnreads(period = 2000) {
// skip if we're not inside the inbox
const isInbox = $('.hA [title=Inbox]');
if (!isInbox) {
setTimeout(checkUnreads, period);
return;
}

if (typeof checkUnreads.startingUp === 'undefined') {
checkUnreads.startingUp = true;
}

const unreads = getUnreadMessages();

ipc.send('update-unreads-count', unreads.length);

const startingUp = seenMessages.size === 0;
// mark all previously seen messages as false
seenMessages.forEach((value, key, map) => {
map.set(key, false);
});

unreads.filter(message => !seenMessages.has(keyByMessage(message))).forEach((message) => {
unreads.forEach((message) => {
const {
element, subject, sender, avatar,
} = message;
const key = keyByMessage(message);
// do not show the same notification every time on start up
if (!startingUp) {
if (!checkUnreads.startingUp && !seenMessages.has(key)) {
sendNotification({
title: sender,
body: subject,
Expand All @@ -90,9 +98,20 @@ function checkUnreads(period = 2000) {
});
}
// mark message as seen
seenMessages.set(keyByMessage(message), true);
seenMessages.set(key, true);
});

// clean up old entries in seenMessages
seenMessages.forEach((value, key, map) => {
if (value === false) {
map.delete(key);
}
});

if (checkUnreads.startingUp) {
checkUnreads.startingUp = false;
}

setTimeout(checkUnreads, period);
}

Expand Down

0 comments on commit 7630808

Please sign in to comment.