Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 8645b0a

Browse files
author
the-djmaze
committed
Speedup identicons by caching them
1 parent 490d808 commit 8645b0a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

plugins/avatars/avatars.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
queue = [],
3939
avatars = new Map,
4040
ncAvatars = new Map,
41+
identicons = new Map,
4142
templateId = 'MailMessageView',
4243
b64 = data => btoa(unescape(encodeURIComponent(data))),
4344
b64url = data => b64(data).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''),
@@ -78,15 +79,22 @@
7879
(from.name?.split(/[^\p{L}]+/gu) || []).reduce((a, s) => a + (s[0] || ''), '')
7980
.slice(0,2)
8081
.toUpperCase(),
81-
setIdenticon = (msg, fn) => hash(msg.from[0].email).then(hash => {
82-
const uri = 'data:image/svg+xml;base64,' + b64(window.identiconSvg(
83-
hash,
84-
fromChars(msg.from[0]),
85-
window.getComputedStyle(getEl('rl-app'), null).getPropertyValue('font-family')
86-
));
87-
// avatars.set(getAvatarUid(msg), uri);
88-
fn(uri);
89-
}),
82+
setIdenticon = (msg, fn) => {
83+
const from = email(msg);
84+
if (identicons.get(from)) {
85+
fn(identicons.get(from));
86+
} else {
87+
hash(from).then(hash => {
88+
const uri = 'data:image/svg+xml;base64,' + b64(window.identiconSvg(
89+
hash,
90+
fromChars(msg.from[0]),
91+
window.getComputedStyle(getEl('rl-app'), null).getPropertyValue('font-family')
92+
));
93+
identicons.set(email(msg), uri);
94+
fn(uri);
95+
});
96+
}
97+
},
9098

9199
addQueue = (msg, fn) => {
92100
if (msg.from?.[0]) {

0 commit comments

Comments
 (0)