Skip to content

Commit d6e8d33

Browse files
committed
performance optimation for onFolderDisplayed
1 parent bddd690 commit d6e8d33

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

src/background.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ let removeOnClassify = true; // Standardwert true
99
const maxTokenCount = 10000; // Maximale Anzahl der Tokens in der Datenbank.
1010
let tagKeyToNameMap = {}; // Mapping von Tag-Key zu Tag-Name
1111
let tagNameToKeyMap = {}; // Mapping von Tag-Name zu Tag-Key
12+
const lastCheckTimestamps = {}; // Stores the last check timestamp for each folder to optimize performance (in milliseconds)
13+
1214

1315
// translate-Funktion zur Nutzung von i18n
1416
function trans(messageName, placeholders = []) {
@@ -188,6 +190,14 @@ function initialize() {
188190
}
189191
});
190192

193+
// Sheduler for updateUsageData
194+
messenger.alarms.create("updateUsageDataAlarm", { periodInMinutes: 10 });
195+
messenger.alarms.onAlarm.addListener((alarm) => {
196+
if (alarm.name === "updateUsageDataAlarm") {
197+
updateUsageData();
198+
}
199+
});
200+
191201
})
192202
.catch((error) => {
193203
console.error("Error during initialization:", error);
@@ -441,9 +451,14 @@ function getMessageTags(messageId) {
441451
* @param {object} folder - Information about the displayed folder.
442452
*/
443453

454+
/**
455+
* Handles the display of a folder and checks for new emails.
456+
* @param {Object} tab - The mail tab object.
457+
* @param {Object} folder - The folder object.
458+
*/
444459
async function onFolderDisplayed(tab, folder) {
445-
updateUsageData();
446-
460+
const CHECK_INTERVAL = 5 * 60 * 1000; // 5 minutes
461+
console.debug("onFolderDisplayed")
447462
if (folder && folder.accountId && folder.path) {
448463
const accountId = folder.accountId;
449464

@@ -456,6 +471,16 @@ async function onFolderDisplayed(tab, folder) {
456471
try {
457472
const folderKey = `${accountId}:${folder.path}`;
458473

474+
// To optimize performance, skip the check if the last one occurred recently.
475+
const now = Date.now();
476+
// Check the last time this folder was processed
477+
const lastCheck = lastCheckTimestamps[folderKey] || 0;
478+
if (now - lastCheck < CHECK_INTERVAL) {
479+
return;
480+
}
481+
// Update the last check timestamp
482+
lastCheckTimestamps[folderKey] = now;
483+
459484
// Load the last processed date for this folder from storage
460485
let result = await messenger.storage.local.get("folderLastProcessed");
461486
let folderLastProcessed = result.folderLastProcessed || {};
@@ -553,6 +578,8 @@ async function onFolderDisplayed(tab, folder) {
553578

554579

555580

581+
582+
556583
// learnTagFromMail Funktion
557584
function learnTagFromMail(messageId, tagName, isPositive) {
558585
console.log(`Training with message ID: ${messageId} and tag: ${tagName}`);

src/bayes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function calculateBayesProbability(tokens, data, returnTokenContributions = fals
135135

136136
const probability = Math.exp(logProbPositive - logSumExp); // Normalisierte positive Wahrscheinlichkeit
137137

138-
console.log("Calculated probability:", probability);
138+
console.debug("Calculated probability:", probability);
139139

140140
// Rückgabe der Token-Beiträge, falls aktiviert
141141
if (returnTokenContributions) {

src/donation_handler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ async function verifyDonationCode(cryptedemail, code) {
4949
* Wenn die Checksum nicht mit der berechneten übereinstimmt, wird der usage_counter auf 30 gesetzt.
5050
*/
5151
async function updateUsageData() {
52-
console.debug(`updateUsageData`);
5352
const currentData = await messenger.storage.local.get(['donation_handler']);
5453
const donationHandler = currentData.donation_handler || {};
5554

@@ -58,7 +57,7 @@ async function updateUsageData() {
5857
const shortHash = await short_sha256(donationHandler.donation_mail);
5958
const isValid = await verifyDonationCode(shortHash, donationHandler.donation_key);
6059
if (isValid) {
61-
console.debug("Gültiger donation_key. updateUsageData.");
60+
console.debug("Valid donation_key found.");
6261
return;
6362
}
6463
}
@@ -89,6 +88,7 @@ async function updateUsageData() {
8988

9089
// Wenn die Checksum korrekt ist, prüfe, ob ein neuer Tag begonnen hat
9190
if (today != lastCheckDate) {
91+
console.debug(`updateUsageData (new day)`);
9292
const newUsageCounter = (donationHandler.usage_counter || 0) + 1;
9393
const newLastCheckDate = today;
9494

src/manifest.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "PrioMailbox",
4-
"version": "1.3.1",
4+
"version": "1.3.3",
55
"default_locale": "en",
66
"description": "PrioMailbox organizes your emails in Thunderbird with intelligent, trainable tags. Important messages are highlighted, while unimportant ones are hidden.",
77
"icons": {
@@ -23,7 +23,8 @@
2323
"menus",
2424
"notifications",
2525
"accountsRead",
26-
"accountsFolders"
26+
"accountsFolders",
27+
"alarms"
2728
],
2829
"browser_action": {
2930
"default_popup": "popup/popup.html",

src/utils.js

-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ function calculateKnownTokenTypesPercentage(tokens, tokenList) {
105105
const knownUnigramsPercentage = totalUnigrams > 0 ? (knownUnigrams / totalUnigrams) * 100 : 0;
106106
const knownBigramsPercentage = totalBigrams > 0 ? (knownBigrams / totalBigrams) * 100 : 0;
107107

108-
// Log the results for debugging
109-
console.log(`Known unigrams: ${knownUnigramsPercentage.toFixed(2)}% (Known: ${knownUnigrams}, Total: ${totalUnigrams})`);
110-
console.log(`Known bigrams: ${knownBigramsPercentage.toFixed(2)}% (Known: ${knownBigrams}, Total: ${totalBigrams})`);
111-
112-
// Return the percentages
113108
return {
114109
knownUnigramsPercentage,
115110
knownBigramsPercentage

0 commit comments

Comments
 (0)