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

Browser storage optimisations

This commit is contained in:
Vjacheslav Trushkin 2022-06-29 13:41:56 +03:00
parent 2adcb7368b
commit d2956c379e
5 changed files with 87 additions and 59 deletions

View File

@ -93,6 +93,10 @@
"require": "./lib/browser-storage/index.cjs",
"import": "./lib/browser-storage/index.mjs"
},
"./lib/browser-storage/item": {
"require": "./lib/browser-storage/item.cjs",
"import": "./lib/browser-storage/item.mjs"
},
"./lib/browser-storage/mock": {
"require": "./lib/browser-storage/mock.cjs",
"import": "./lib/browser-storage/mock.mjs"

View File

@ -1,18 +1,15 @@
import { browserCacheCountKey } from './config';
import { getStoredItem, setStoredItem } from './item';
import type { BrowserStorageInstance } from './types';
/**
* Change current count for storage
*/
export function setBrowserStorageItemsCount(
storage: typeof localStorage,
storage: BrowserStorageInstance,
value: number
): true | undefined {
try {
storage.setItem(browserCacheCountKey, value.toString());
return true;
} catch (err) {
//
}
return setStoredItem(storage, browserCacheCountKey, value.toString());
}
/**
@ -21,5 +18,7 @@ export function setBrowserStorageItemsCount(
export function getBrowserStorageItemsCount(
storage: typeof localStorage
): number {
return parseInt(storage.getItem(browserCacheCountKey) as string) || 0;
return (
parseInt(getStoredItem(storage, browserCacheCountKey) as string) || 0
);
}

View File

@ -11,6 +11,7 @@ import {
} from './count';
import { browserStorageEmptyItems } from './data';
import { getBrowserStorage } from './global';
import { getStoredItem, removeStoredItem, setStoredItem } from './item';
import type { BrowserStorageConfig, BrowserStorageItem } from './types';
// Result of callback. false = delete item
@ -34,33 +35,20 @@ export function iterateBrowserStorage(
return;
}
try {
// Get version
const version = func.getItem(browserCacheVersionKey);
if (version !== browserCacheVersion) {
if (version) {
// Version is set, but invalid - remove old entries
try {
const total = getBrowserStorageItemsCount(func);
for (let i = 0; i < total; i++) {
func.removeItem(browserCachePrefix + i.toString());
}
} catch (err) {
//
}
// Get version
const version = getStoredItem(func, browserCacheVersionKey);
if (version !== browserCacheVersion) {
if (version) {
// Version is set, but invalid - remove old entries
const total = getBrowserStorageItemsCount(func);
for (let i = 0; i < total; i++) {
removeStoredItem(func, browserCachePrefix + i.toString());
}
// Empty data
try {
func.setItem(browserCacheVersionKey, browserCacheVersion);
} catch (err) {
//
}
setBrowserStorageItemsCount(func, 0);
return;
}
} catch (err) {
// Failed
// Empty data
setStoredItem(func, browserCacheVersionKey, browserCacheVersion);
setBrowserStorageItemsCount(func, 0);
return;
}
@ -72,7 +60,7 @@ export function iterateBrowserStorage(
// Parse item
const parseItem = (index: number): true | undefined => {
const name = browserCachePrefix + index.toString();
const item = func.getItem(name);
const item = getStoredItem(func, name);
if (typeof item !== 'string') {
// Does not exist
@ -100,7 +88,7 @@ export function iterateBrowserStorage(
}
// Remove item
func.removeItem(name);
removeStoredItem(func, name);
};
let total = getBrowserStorageItemsCount(func);

View File

@ -0,0 +1,42 @@
import type { BrowserStorageInstance } from './types';
/**
* Get stored item with try..catch
*/
export function getStoredItem(
func: BrowserStorageInstance,
key: string
): string | null | undefined {
try {
return func.getItem(key);
} catch (err) {
//
}
}
/**
* Store item with try..catch
*/
export function setStoredItem(
func: BrowserStorageInstance,
key: string,
value: string
): true | undefined {
try {
func.setItem(key, value);
return true;
} catch (err) {
//
}
}
/**
* Remove item with try..catch
*/
export function removeStoredItem(func: BrowserStorageInstance, key: string) {
try {
func.removeItem(key);
} catch (err) {
//
}
}

View File

@ -12,7 +12,12 @@ import {
} from './data';
import { getBrowserStorage } from './global';
import { initBrowserStorage } from './index';
import type { BrowserStorageItem, BrowserStorageType } from './types';
import { setStoredItem } from './item';
import type {
BrowserStorageInstance,
BrowserStorageItem,
BrowserStorageType,
} from './types';
/**
* Function to cache icons
@ -23,12 +28,8 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
}
function store(key: BrowserStorageType): true | undefined {
if (!browserStorageConfig[key]) {
return;
}
const func = getBrowserStorage(key);
if (!func) {
let func: BrowserStorageInstance | undefined;
if (!browserStorageConfig[key] || !(func = getBrowserStorage(key))) {
return;
}
@ -37,8 +38,7 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
let index: number;
if (set.size) {
// Remove item from set
index = Array.from(set).shift() as number;
set.delete(index);
set.delete((index = Array.from(set).shift() as number));
} else {
// Create new index
index = getBrowserStorageItemsCount(func);
@ -48,22 +48,17 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
}
// Create and save item
try {
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider: storage.provider,
data,
};
func.setItem(
browserCachePrefix + index.toString(),
JSON.stringify(item)
);
} catch (err) {
return;
}
const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / browserStorageHour),
provider: storage.provider,
data,
};
// Success
return true;
return setStoredItem(
func,
browserCachePrefix + index.toString(),
JSON.stringify(item)
);
}
// Do not store empty sets