Add and remove opposing event listeners

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-06-29 15:38:58 +05:30
parent 5f1346568d
commit 8b6ff533e8

View File

@ -44,7 +44,7 @@ export class AvatarView extends BaseUpdateView {
return false; return false;
} }
setAvatarError() { _setAvatarError() {
this._avatarError = true; this._avatarError = true;
this.update(this.value); this.update(this.value);
} }
@ -57,6 +57,20 @@ export class AvatarView extends BaseUpdateView {
return false; 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() { _avatarLetterChanged() {
if (this.value.avatarLetter !== this._avatarLetter) { if (this.value.avatarLetter !== this._avatarLetter) {
this._avatarLetter = this.value.avatarLetter; this._avatarLetter = this.value.avatarLetter;
@ -71,7 +85,7 @@ export class AvatarView extends BaseUpdateView {
this._avatarTitleChanged(); this._avatarTitleChanged();
this._root = renderStaticAvatar(this.value, this._size); this._root = renderStaticAvatar(this.value, this._size);
const image = this._root.firstChild; const image = this._root.firstChild;
image?.addEventListener("error", () => this.setAvatarError()); this._addListenersToAvatar(image);
// takes care of update being called when needed // takes care of update being called when needed
super.mount(options); super.mount(options);
return this._root; return this._root;