From d2956c379e9a7307cf06d71d2f1b383d658946ba Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 29 Jun 2022 13:41:56 +0300 Subject: [PATCH] Browser storage optimisations --- packages/core/package.json | 4 ++ packages/core/src/browser-storage/count.ts | 15 ++++--- packages/core/src/browser-storage/foreach.ts | 42 +++++++------------ packages/core/src/browser-storage/item.ts | 42 +++++++++++++++++++ packages/core/src/browser-storage/store.ts | 43 +++++++++----------- 5 files changed, 87 insertions(+), 59 deletions(-) create mode 100644 packages/core/src/browser-storage/item.ts diff --git a/packages/core/package.json b/packages/core/package.json index 64b993b..0480152 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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" diff --git a/packages/core/src/browser-storage/count.ts b/packages/core/src/browser-storage/count.ts index f744488..f24fbce 100644 --- a/packages/core/src/browser-storage/count.ts +++ b/packages/core/src/browser-storage/count.ts @@ -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 + ); } diff --git a/packages/core/src/browser-storage/foreach.ts b/packages/core/src/browser-storage/foreach.ts index 93eb4bb..5027953 100644 --- a/packages/core/src/browser-storage/foreach.ts +++ b/packages/core/src/browser-storage/foreach.ts @@ -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); diff --git a/packages/core/src/browser-storage/item.ts b/packages/core/src/browser-storage/item.ts new file mode 100644 index 0000000..5877e49 --- /dev/null +++ b/packages/core/src/browser-storage/item.ts @@ -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) { + // + } +} diff --git a/packages/core/src/browser-storage/store.ts b/packages/core/src/browser-storage/store.ts index 163aef4..17dd654 100644 --- a/packages/core/src/browser-storage/store.ts +++ b/packages/core/src/browser-storage/store.ts @@ -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