diff --git a/src/platform/web/ui/avatar.js b/src/platform/web/ui/avatar.js index 715a4890..0a865963 100644 --- a/src/platform/web/ui/avatar.js +++ b/src/platform/web/ui/avatar.js @@ -44,7 +44,7 @@ export class AvatarView extends BaseUpdateView { return false; } - setAvatarError() { + _setAvatarError() { this._avatarError = true; this.update(this.value); } @@ -57,6 +57,20 @@ export class AvatarView extends BaseUpdateView { return false; } + _addListenersToAvatar(image) { + const handleAvatarError = (e) => { + const image = e.target; + image.removeEventListener("load", removeErrorHandler); + this._setAvatarError(); + }; + const removeErrorHandler = (e) => { + const image = e.target; + image.removeEventListener("error", handleAvatarError); + }; + image?.addEventListener("error", handleAvatarError); + image?.addEventListener("load", removeErrorHandler); + } + _avatarLetterChanged() { if (this.value.avatarLetter !== this._avatarLetter) { this._avatarLetter = this.value.avatarLetter; @@ -71,7 +85,7 @@ export class AvatarView extends BaseUpdateView { this._avatarTitleChanged(); this._root = renderStaticAvatar(this.value, this._size); const image = this._root.firstChild; - image?.addEventListener("error", () => this.setAvatarError()); + this._addListenersToAvatar(image); // takes care of update being called when needed super.mount(options); return this._root;