From 7c4e4c97e70b53055b907ebace7a6fdeca4601cf Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Wed, 24 Mar 2021 06:59:56 -0700 Subject: [PATCH] Cache identicons to avoid performance hit on app start --- ts/models/conversations.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index d78359be037..075215d9b48 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -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 > { @@ -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 { return { @@ -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. @@ -4973,6 +4977,27 @@ export class ConversationModel extends window.Backbone.Model< }); } + private async getIdenticon(): Promise { + 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;