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;
}
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;