2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-07 15:44:05 +00:00

Browser storage fixes

This commit is contained in:
Vjacheslav Trushkin 2022-06-28 23:53:58 +03:00
parent 882be420f9
commit 9f71691fd2
7 changed files with 29 additions and 35 deletions

View File

@ -1,15 +1,15 @@
import { browserCacheCountKey } from './config'; import { browserCacheCountKey } from './config';
import { browserStorageItemsCount } from './data'; import { browserStorageItemsCount } from './data';
import type { BrowserStorageConfig } from './types'; import type { BrowserStorageType } from './types';
/** /**
* Change current count for storage * Change current count for storage
*/ */
export function setBrowserStorageItemsCount( export function setBrowserStorageItemsCount(
storage: typeof localStorage, storage: typeof localStorage,
key: keyof BrowserStorageConfig, key: BrowserStorageType,
value: number value: number
): boolean { ): true | undefined {
try { try {
storage.setItem(browserCacheCountKey, value.toString()); storage.setItem(browserCacheCountKey, value.toString());
browserStorageItemsCount[key] = value; browserStorageItemsCount[key] = value;
@ -17,7 +17,6 @@ export function setBrowserStorageItemsCount(
} catch (err) { } catch (err) {
// //
} }
return false;
} }
/** /**
@ -26,10 +25,5 @@ export function setBrowserStorageItemsCount(
export function getBrowserStorageItemsCount( export function getBrowserStorageItemsCount(
storage: typeof localStorage storage: typeof localStorage
): number { ): number {
const count = storage.getItem(browserCacheCountKey); return parseInt(storage.getItem(browserCacheCountKey) as string) || 0;
if (count) {
const total = parseInt(count);
return total ? total : 0;
}
return 0;
} }

View File

@ -1,9 +1,10 @@
import { browserStorageConfig } from './data'; import { browserStorageConfig } from './data';
import type { BrowserStorageType } from './types';
/** /**
* Cache types * Cache types
*/ */
export type IconifyBrowserCacheType = 'local' | 'session' | 'all'; export type IconifyBrowserCacheType = BrowserStorageType | 'all';
/** /**
* Toggle cache * Toggle cache
@ -20,8 +21,7 @@ export function toggleBrowserCache(
case 'all': case 'all':
for (const key in browserStorageConfig) { for (const key in browserStorageConfig) {
browserStorageConfig[key as keyof typeof browserStorageConfig] = browserStorageConfig[key as BrowserStorageType] = value;
value;
} }
break; break;
} }

View File

@ -1,5 +1,5 @@
import { browserStorageConfig } from './data'; import { browserStorageConfig } from './data';
import type { BrowserStorageConfig } from './types'; import type { BrowserStorageType } from './types';
/** /**
* Fake window for unit testing * Fake window for unit testing
@ -13,7 +13,7 @@ let _window: FakeWindow =
* Get browser storage * Get browser storage
*/ */
export function getBrowserStorage( export function getBrowserStorage(
key: keyof BrowserStorageConfig key: BrowserStorageType
): typeof localStorage | undefined { ): typeof localStorage | undefined {
const attr = key + 'Storage'; const attr = key + 'Storage';
try { try {

View File

@ -113,7 +113,7 @@ export function initBrowserStorage() {
if (!getItem(i)) { if (!getItem(i)) {
// Remove item // Remove item
if (i === total - 1) { if (i === total - 1) {
// Last item - reduce country // Last item - reduce count
total--; total--;
} else { } else {
// Mark as empty // Mark as empty

View File

@ -5,6 +5,7 @@ import {
browserStorageEmptyItems, browserStorageEmptyItems,
setBrowserStorageStatus, setBrowserStorageStatus,
} from './data'; } from './data';
import type { BrowserStorageType } from './types';
/** /**
* Get next icon set prefix for testing * Get next icon set prefix for testing
@ -99,9 +100,8 @@ export function reset(fakeWindow: Record<string, typeof localStorage>): void {
// Reset all data // Reset all data
setBrowserStorageStatus(false); setBrowserStorageStatus(false);
for (const key in browserStorageConfig) { for (const key in browserStorageConfig) {
const attr = key as unknown as keyof typeof browserStorageConfig; browserStorageConfig[key as BrowserStorageType] = true;
browserStorageConfig[attr] = true; browserStorageItemsCount[key as BrowserStorageType] = 0;
browserStorageItemsCount[attr] = 0; browserStorageEmptyItems[key as BrowserStorageType] = [];
browserStorageEmptyItems[attr] = [];
} }
} }

View File

@ -9,7 +9,7 @@ import {
} from './data'; } from './data';
import { getBrowserStorage } from './global'; import { getBrowserStorage } from './global';
import { initBrowserStorage } from './index'; import { initBrowserStorage } from './index';
import type { BrowserStorageConfig, BrowserStorageItem } from './types'; import type { BrowserStorageItem, BrowserStorageType } from './types';
/** /**
* Function to cache icons * Function to cache icons
@ -18,15 +18,18 @@ export function storeInBrowserStorage(provider: string, data: IconifyJSON) {
if (!browserStorageStatus) { if (!browserStorageStatus) {
initBrowserStorage(); initBrowserStorage();
} }
if (browserStorageStatus === 'loading') {
return;
}
function store(key: keyof BrowserStorageConfig): boolean { function store(key: BrowserStorageType): true | undefined {
if (!browserStorageConfig[key]) { if (!browserStorageConfig[key]) {
return false; return;
} }
const func = getBrowserStorage(key); const func = getBrowserStorage(key);
if (!func) { if (!func) {
return false; return;
} }
// Get item index // Get item index
@ -35,7 +38,7 @@ export function storeInBrowserStorage(provider: string, data: IconifyJSON) {
// Create new index // Create new index
index = browserStorageItemsCount[key]; index = browserStorageItemsCount[key];
if (!setBrowserStorageItemsCount(func, key, index + 1)) { if (!setBrowserStorageItemsCount(func, key, index + 1)) {
return false; return;
} }
} }
@ -50,10 +53,10 @@ export function storeInBrowserStorage(provider: string, data: IconifyJSON) {
browserCachePrefix + index.toString(), browserCachePrefix + index.toString(),
JSON.stringify(item) JSON.stringify(item)
); );
return true;
} catch (err) { } catch (err) {
return false; //
} }
return true;
} }
// Do not store empty sets // Do not store empty sets

View File

@ -1,19 +1,16 @@
import type { IconifyJSON } from '@iconify/types'; import type { IconifyJSON } from '@iconify/types';
// Mixin for config for various types // Storage types
export interface BrowserStorageType<T> { export type BrowserStorageType = 'local' | 'session';
local: T;
session: T;
}
// Config // Config
export type BrowserStorageConfig = BrowserStorageType<boolean>; export type BrowserStorageConfig = Record<BrowserStorageType, boolean>;
// Number of items // Number of items
export type BrowserStorageCount = BrowserStorageType<number>; export type BrowserStorageCount = Record<BrowserStorageType, number>;
// List of empty items, for use later // List of empty items, for use later
export type BrowserStorageEmptyList = BrowserStorageType<number[]>; export type BrowserStorageEmptyList = Record<BrowserStorageType, number[]>;
// Stored item // Stored item
export interface BrowserStorageItem { export interface BrowserStorageItem {