diff --git a/packages/core/src/browser-storage/count.ts b/packages/core/src/browser-storage/count.ts index 8a19e7c..1181881 100644 --- a/packages/core/src/browser-storage/count.ts +++ b/packages/core/src/browser-storage/count.ts @@ -1,15 +1,15 @@ import { browserCacheCountKey } from './config'; import { browserStorageItemsCount } from './data'; -import type { BrowserStorageConfig } from './types'; +import type { BrowserStorageType } from './types'; /** * Change current count for storage */ export function setBrowserStorageItemsCount( storage: typeof localStorage, - key: keyof BrowserStorageConfig, + key: BrowserStorageType, value: number -): boolean { +): true | undefined { try { storage.setItem(browserCacheCountKey, value.toString()); browserStorageItemsCount[key] = value; @@ -17,7 +17,6 @@ export function setBrowserStorageItemsCount( } catch (err) { // } - return false; } /** @@ -26,10 +25,5 @@ export function setBrowserStorageItemsCount( export function getBrowserStorageItemsCount( storage: typeof localStorage ): number { - const count = storage.getItem(browserCacheCountKey); - if (count) { - const total = parseInt(count); - return total ? total : 0; - } - return 0; + return parseInt(storage.getItem(browserCacheCountKey) as string) || 0; } diff --git a/packages/core/src/browser-storage/functions.ts b/packages/core/src/browser-storage/functions.ts index 5eaa65d..91f96c1 100644 --- a/packages/core/src/browser-storage/functions.ts +++ b/packages/core/src/browser-storage/functions.ts @@ -1,9 +1,10 @@ import { browserStorageConfig } from './data'; +import type { BrowserStorageType } from './types'; /** * Cache types */ -export type IconifyBrowserCacheType = 'local' | 'session' | 'all'; +export type IconifyBrowserCacheType = BrowserStorageType | 'all'; /** * Toggle cache @@ -20,8 +21,7 @@ export function toggleBrowserCache( case 'all': for (const key in browserStorageConfig) { - browserStorageConfig[key as keyof typeof browserStorageConfig] = - value; + browserStorageConfig[key as BrowserStorageType] = value; } break; } diff --git a/packages/core/src/browser-storage/global.ts b/packages/core/src/browser-storage/global.ts index d5f8a03..436d6a2 100644 --- a/packages/core/src/browser-storage/global.ts +++ b/packages/core/src/browser-storage/global.ts @@ -1,5 +1,5 @@ import { browserStorageConfig } from './data'; -import type { BrowserStorageConfig } from './types'; +import type { BrowserStorageType } from './types'; /** * Fake window for unit testing @@ -13,7 +13,7 @@ let _window: FakeWindow = * Get browser storage */ export function getBrowserStorage( - key: keyof BrowserStorageConfig + key: BrowserStorageType ): typeof localStorage | undefined { const attr = key + 'Storage'; try { diff --git a/packages/core/src/browser-storage/index.ts b/packages/core/src/browser-storage/index.ts index cd4684b..f08a7ad 100644 --- a/packages/core/src/browser-storage/index.ts +++ b/packages/core/src/browser-storage/index.ts @@ -113,7 +113,7 @@ export function initBrowserStorage() { if (!getItem(i)) { // Remove item if (i === total - 1) { - // Last item - reduce country + // Last item - reduce count total--; } else { // Mark as empty diff --git a/packages/core/src/browser-storage/mock.ts b/packages/core/src/browser-storage/mock.ts index 5aebc60..b5f553b 100644 --- a/packages/core/src/browser-storage/mock.ts +++ b/packages/core/src/browser-storage/mock.ts @@ -5,6 +5,7 @@ import { browserStorageEmptyItems, setBrowserStorageStatus, } from './data'; +import type { BrowserStorageType } from './types'; /** * Get next icon set prefix for testing @@ -99,9 +100,8 @@ export function reset(fakeWindow: Record): void { // Reset all data setBrowserStorageStatus(false); for (const key in browserStorageConfig) { - const attr = key as unknown as keyof typeof browserStorageConfig; - browserStorageConfig[attr] = true; - browserStorageItemsCount[attr] = 0; - browserStorageEmptyItems[attr] = []; + browserStorageConfig[key as BrowserStorageType] = true; + browserStorageItemsCount[key as BrowserStorageType] = 0; + browserStorageEmptyItems[key as BrowserStorageType] = []; } } diff --git a/packages/core/src/browser-storage/store.ts b/packages/core/src/browser-storage/store.ts index 45e0b1e..76b8d65 100644 --- a/packages/core/src/browser-storage/store.ts +++ b/packages/core/src/browser-storage/store.ts @@ -9,7 +9,7 @@ import { } from './data'; import { getBrowserStorage } from './global'; import { initBrowserStorage } from './index'; -import type { BrowserStorageConfig, BrowserStorageItem } from './types'; +import type { BrowserStorageItem, BrowserStorageType } from './types'; /** * Function to cache icons @@ -18,15 +18,18 @@ export function storeInBrowserStorage(provider: string, data: IconifyJSON) { if (!browserStorageStatus) { initBrowserStorage(); } + if (browserStorageStatus === 'loading') { + return; + } - function store(key: keyof BrowserStorageConfig): boolean { + function store(key: BrowserStorageType): true | undefined { if (!browserStorageConfig[key]) { - return false; + return; } const func = getBrowserStorage(key); if (!func) { - return false; + return; } // Get item index @@ -35,7 +38,7 @@ export function storeInBrowserStorage(provider: string, data: IconifyJSON) { // Create new index index = browserStorageItemsCount[key]; if (!setBrowserStorageItemsCount(func, key, index + 1)) { - return false; + return; } } @@ -50,10 +53,10 @@ export function storeInBrowserStorage(provider: string, data: IconifyJSON) { browserCachePrefix + index.toString(), JSON.stringify(item) ); + return true; } catch (err) { - return false; + // } - return true; } // Do not store empty sets diff --git a/packages/core/src/browser-storage/types.ts b/packages/core/src/browser-storage/types.ts index 896944b..303820e 100644 --- a/packages/core/src/browser-storage/types.ts +++ b/packages/core/src/browser-storage/types.ts @@ -1,19 +1,16 @@ import type { IconifyJSON } from '@iconify/types'; -// Mixin for config for various types -export interface BrowserStorageType { - local: T; - session: T; -} +// Storage types +export type BrowserStorageType = 'local' | 'session'; // Config -export type BrowserStorageConfig = BrowserStorageType; +export type BrowserStorageConfig = Record; // Number of items -export type BrowserStorageCount = BrowserStorageType; +export type BrowserStorageCount = Record; // List of empty items, for use later -export type BrowserStorageEmptyList = BrowserStorageType; +export type BrowserStorageEmptyList = Record; // Stored item export interface BrowserStorageItem {