Skip to content

Commit

Permalink
await in load icon and return cIcon
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Oct 20, 2023
1 parent 95c0244 commit 3cec734
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/Core/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ class Label extends THREE.Object3D {
}
style.applyToHTML(this.content)
.then((icon) => {
if (icon) {
icon.addEventListener('loaded', () => {
this.icon = this.content.getElementsByClassName('itowns-icon')[0];
}, { once: true });
if (icon) { // Not sure if that test is needed...
this.icon = icon;
}
});
}
Expand Down
20 changes: 14 additions & 6 deletions src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,17 +945,25 @@ class Style {
}

cIcon.style['z-index'] = -1;
domElement.appendChild(cIcon);
const event = new Event('loaded');
icon.dispatchEvent(event);
return cIcon;
};

const loadIcon = icon => new Promise((resolve, reject) => {
icon.onload = () => {
const cIcon = addIcon();
resolve(cIcon);
};
icon.onerror = reject;
});

let cIcon;
if (icon.complete) {
addIcon();
cIcon = addIcon();
} else {
icon.addEventListener('load', addIcon, { once: true });
cIcon = await loadIcon(icon);
}
return icon;
domElement.appendChild(cIcon);
return cIcon;
}

/**
Expand Down

0 comments on commit 3cec734

Please sign in to comment.