2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-07 07:34:22 +00:00

Do not reuse Promise instance in loadIcon in core

This commit is contained in:
Vjacheslav Trushkin 2022-03-16 19:32:27 +02:00
parent 41afe33976
commit 32819127dd

View File

@ -357,15 +357,10 @@ export const loadIcons: IconifyLoadIcons = (
/**
* Cache for loadIcon promises
*/
type LoadIconResult = Promise<Required<IconifyIcon>>;
const iconsQueue = Object.create(null) as Record<string, LoadIconResult>;
export const loadIcon = (icon: IconifyIconName | string): LoadIconResult => {
if (typeof icon === 'string' && iconsQueue[icon] !== void 0) {
return iconsQueue[icon];
}
const result: LoadIconResult = new Promise((fulfill, reject) => {
export const loadIcon = (
icon: IconifyIconName | string
): Promise<Required<IconifyIcon>> => {
return new Promise((fulfill, reject) => {
const iconObj = typeof icon === 'string' ? stringToIcon(icon) : icon;
loadIcons([iconObj || icon], (loaded) => {
if (loaded.length && iconObj) {
@ -380,9 +375,4 @@ export const loadIcon = (icon: IconifyIconName | string): LoadIconResult => {
reject(icon);
});
});
if (typeof icon === 'string') {
iconsQueue[icon] = result;
}
return result;
};