Skip to content

Commit

Permalink
Cache identicons to avoid performance hit on app start
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Mar 24, 2021
1 parent 919259c commit 7c4e4c9
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions ts/models/conversations.ts
Expand Up @@ -88,6 +88,12 @@ type CustomError = Error & {
number?: string;
};

type CachedIdenticon = {
readonly url: string;
readonly content: string;
readonly color: ColorType;
};

export class ConversationModel extends window.Backbone.Model<
ConversationAttributesType
> {
Expand Down Expand Up @@ -139,6 +145,8 @@ export class ConversationModel extends window.Backbone.Model<

private cachedLatestGroupCallEraId?: string;

private cachedIdenticon?: CachedIdenticon;

// eslint-disable-next-line class-methods-use-this
defaults(): Partial<ConversationAttributesType> {
return {
Expand Down Expand Up @@ -4947,11 +4955,7 @@ export class ConversationModel extends window.Backbone.Model<
if (avatar && avatar.path) {
notificationIconUrl = getAbsoluteAttachmentPath(avatar.path);
} else if (this.isPrivate()) {
notificationIconUrl = await new window.Whisper.IdenticonSVGView({
color: this.getColor(),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
content: this.getInitials(this.get('name')!) || '#',
}).getDataUrl();
notificationIconUrl = await this.getIdenticon();
} else {
// Not technically needed, but helps us be explicit: we don't show an icon for a
// group that doesn't have an icon.
Expand All @@ -4973,6 +4977,27 @@ export class ConversationModel extends window.Backbone.Model<
});
}

private async getIdenticon(): Promise<string> {
const color = this.getColor();
const name = this.get('name');

const content = (name && this.getInitials(name)) || '#';

const cached = this.cachedIdenticon;
if (cached && cached.content === content && cached.color === color) {
return cached.url;
}

const fresh = await new window.Whisper.IdenticonSVGView({
color,
content,
}).getDataUrl();

this.cachedIdenticon = { content, color, url: fresh };

return fresh;
}

notifyTyping(options: {
isTyping: boolean;
senderId: string;
Expand Down

0 comments on commit 7c4e4c9

Please sign in to comment.