Cache identicons to avoid performance hit on app start
This commit is contained in:
parent
919259c960
commit
7c4e4c97e7
1 changed files with 30 additions and 5 deletions
|
@ -88,6 +88,12 @@ type CustomError = Error & {
|
||||||
number?: string;
|
number?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type CachedIdenticon = {
|
||||||
|
readonly url: string;
|
||||||
|
readonly content: string;
|
||||||
|
readonly color: ColorType;
|
||||||
|
};
|
||||||
|
|
||||||
export class ConversationModel extends window.Backbone.Model<
|
export class ConversationModel extends window.Backbone.Model<
|
||||||
ConversationAttributesType
|
ConversationAttributesType
|
||||||
> {
|
> {
|
||||||
|
@ -139,6 +145,8 @@ export class ConversationModel extends window.Backbone.Model<
|
||||||
|
|
||||||
private cachedLatestGroupCallEraId?: string;
|
private cachedLatestGroupCallEraId?: string;
|
||||||
|
|
||||||
|
private cachedIdenticon?: CachedIdenticon;
|
||||||
|
|
||||||
// eslint-disable-next-line class-methods-use-this
|
// eslint-disable-next-line class-methods-use-this
|
||||||
defaults(): Partial<ConversationAttributesType> {
|
defaults(): Partial<ConversationAttributesType> {
|
||||||
return {
|
return {
|
||||||
|
@ -4947,11 +4955,7 @@ export class ConversationModel extends window.Backbone.Model<
|
||||||
if (avatar && avatar.path) {
|
if (avatar && avatar.path) {
|
||||||
notificationIconUrl = getAbsoluteAttachmentPath(avatar.path);
|
notificationIconUrl = getAbsoluteAttachmentPath(avatar.path);
|
||||||
} else if (this.isPrivate()) {
|
} else if (this.isPrivate()) {
|
||||||
notificationIconUrl = await new window.Whisper.IdenticonSVGView({
|
notificationIconUrl = await this.getIdenticon();
|
||||||
color: this.getColor(),
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
content: this.getInitials(this.get('name')!) || '#',
|
|
||||||
}).getDataUrl();
|
|
||||||
} else {
|
} else {
|
||||||
// Not technically needed, but helps us be explicit: we don't show an icon for a
|
// Not technically needed, but helps us be explicit: we don't show an icon for a
|
||||||
// group that doesn't have an icon.
|
// group that doesn't have an icon.
|
||||||
|
@ -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: {
|
notifyTyping(options: {
|
||||||
isTyping: boolean;
|
isTyping: boolean;
|
||||||
senderId: string;
|
senderId: string;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue