2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-06 07:20:40 +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 { 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;
}

View File

@ -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;
}

View File

@ -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 {

View File

@ -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

View File

@ -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<string, typeof localStorage>): 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] = [];
}
}

View File

@ -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

View File

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