2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

Use Set to mark icons as missing

This commit is contained in:
Vjacheslav Trushkin 2022-06-28 20:20:22 +03:00
parent 59bdbe89e0
commit 7ca4026eb6
7 changed files with 17 additions and 13 deletions

View File

@ -70,7 +70,7 @@ export function scanDOM(rootNode?: ObservedNode, addTempNode = false): void {
};
}
if (storage.missing[name]) {
if (storage.missing.has(name)) {
return {
status: 'missing',
};

View File

@ -109,7 +109,7 @@ export function updateCallbacks(provider: string, prefix: string): void {
prefix,
name,
});
} else if (storage.missing[name] !== void 0) {
} else if (storage.missing.has(name)) {
// Missing
icons.missing.push({
provider,

View File

@ -186,9 +186,8 @@ function loadNewIcons(provider: string, prefix: string, icons: string[]): void {
}
// Not found: mark as missing
const t = Date.now();
item.icons.forEach((name) => {
storage.missing[name] = t;
storage.missing.add(name);
});
} else {
// Add icons to storage

View File

@ -72,7 +72,7 @@ export function sortIcons(icons: IconifyIconName[]): SortedIcons {
let list;
if (localStorage.icons[name] !== void 0) {
list = result.loaded;
} else if (prefix === '' || localStorage.missing[name] !== void 0) {
} else if (prefix === '' || localStorage.missing.has(name)) {
// Mark icons without prefix as missing because they cannot be loaded from API
list = result.missing;
} else {

View File

@ -69,7 +69,8 @@ export function getIconData(
const storage = getStorage(icon.provider, icon.prefix);
const iconName = icon.name;
return (
storage.icons[iconName] || (storage.missing[iconName] ? null : void 0)
storage.icons[iconName] ||
(storage.missing.has(iconName) ? null : void 0)
);
}

View File

@ -7,16 +7,21 @@ import { quicklyValidateIconSet } from '@iconify/utils/lib/icon-set/validate-bas
/**
* List of icons
*/
type IconRecords = Record<string, FullIconifyIcon | null>;
type IconRecords = Record<string, FullIconifyIcon>;
/**
* Storage type
*/
export interface IconStorage {
// Provider and prefix
provider: string;
prefix: string;
// List of available icons
icons: IconRecords;
missing: Record<string, number>;
// List of missing icons
missing: Set<string>;
}
/**
@ -35,7 +40,7 @@ export function newStorage(provider: string, prefix: string): IconStorage {
provider,
prefix,
icons: Object.create(null) as IconStorage['icons'],
missing: Object.create(null) as IconStorage['missing'],
missing: new Set(),
};
}
@ -63,12 +68,11 @@ export function addIconSet(storage: IconStorage, data: IconifyJSON): string[] {
return [];
}
const t = Date.now();
return parseIconSet(data, (name, icon: FullIconifyIcon | null) => {
if (icon) {
storage.icons[name] = icon;
} else {
storage.missing[name] = t;
storage.missing.add(name);
}
});
}
@ -100,7 +104,7 @@ export function addIconToStorage(
* Check if icon exists
*/
export function iconExists(storage: IconStorage, name: string): boolean {
return storage.icons[name] !== void 0;
return !!storage.icons[name];
}
/**

View File

@ -37,7 +37,7 @@ describe('Testing storage', () => {
});
// Mark 'not-really-missing' as missing
storage.missing['not-really-missing'] = Date.now();
storage.missing.add('not-really-missing');
// Add invalid icon
addIconToStorage(storage, 'invalid', {} as unknown as IconifyIcon);