mirror of
https://github.com/iconify/iconify.git
synced 2025-01-09 00:10:47 +00:00
Browser storage optimisations
This commit is contained in:
parent
2adcb7368b
commit
d2956c379e
@ -93,6 +93,10 @@
|
|||||||
"require": "./lib/browser-storage/index.cjs",
|
"require": "./lib/browser-storage/index.cjs",
|
||||||
"import": "./lib/browser-storage/index.mjs"
|
"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": {
|
"./lib/browser-storage/mock": {
|
||||||
"require": "./lib/browser-storage/mock.cjs",
|
"require": "./lib/browser-storage/mock.cjs",
|
||||||
"import": "./lib/browser-storage/mock.mjs"
|
"import": "./lib/browser-storage/mock.mjs"
|
||||||
|
@ -1,18 +1,15 @@
|
|||||||
import { browserCacheCountKey } from './config';
|
import { browserCacheCountKey } from './config';
|
||||||
|
import { getStoredItem, setStoredItem } from './item';
|
||||||
|
import type { BrowserStorageInstance } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change current count for storage
|
* Change current count for storage
|
||||||
*/
|
*/
|
||||||
export function setBrowserStorageItemsCount(
|
export function setBrowserStorageItemsCount(
|
||||||
storage: typeof localStorage,
|
storage: BrowserStorageInstance,
|
||||||
value: number
|
value: number
|
||||||
): true | undefined {
|
): true | undefined {
|
||||||
try {
|
return setStoredItem(storage, browserCacheCountKey, value.toString());
|
||||||
storage.setItem(browserCacheCountKey, value.toString());
|
|
||||||
return true;
|
|
||||||
} catch (err) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -21,5 +18,7 @@ export function setBrowserStorageItemsCount(
|
|||||||
export function getBrowserStorageItemsCount(
|
export function getBrowserStorageItemsCount(
|
||||||
storage: typeof localStorage
|
storage: typeof localStorage
|
||||||
): number {
|
): number {
|
||||||
return parseInt(storage.getItem(browserCacheCountKey) as string) || 0;
|
return (
|
||||||
|
parseInt(getStoredItem(storage, browserCacheCountKey) as string) || 0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
} from './count';
|
} from './count';
|
||||||
import { browserStorageEmptyItems } from './data';
|
import { browserStorageEmptyItems } from './data';
|
||||||
import { getBrowserStorage } from './global';
|
import { getBrowserStorage } from './global';
|
||||||
|
import { getStoredItem, removeStoredItem, setStoredItem } from './item';
|
||||||
import type { BrowserStorageConfig, BrowserStorageItem } from './types';
|
import type { BrowserStorageConfig, BrowserStorageItem } from './types';
|
||||||
|
|
||||||
// Result of callback. false = delete item
|
// Result of callback. false = delete item
|
||||||
@ -34,35 +35,22 @@ export function iterateBrowserStorage(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
// Get version
|
// Get version
|
||||||
const version = func.getItem(browserCacheVersionKey);
|
const version = getStoredItem(func, browserCacheVersionKey);
|
||||||
if (version !== browserCacheVersion) {
|
if (version !== browserCacheVersion) {
|
||||||
if (version) {
|
if (version) {
|
||||||
// Version is set, but invalid - remove old entries
|
// Version is set, but invalid - remove old entries
|
||||||
try {
|
|
||||||
const total = getBrowserStorageItemsCount(func);
|
const total = getBrowserStorageItemsCount(func);
|
||||||
for (let i = 0; i < total; i++) {
|
for (let i = 0; i < total; i++) {
|
||||||
func.removeItem(browserCachePrefix + i.toString());
|
removeStoredItem(func, browserCachePrefix + i.toString());
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty data
|
// Empty data
|
||||||
try {
|
setStoredItem(func, browserCacheVersionKey, browserCacheVersion);
|
||||||
func.setItem(browserCacheVersionKey, browserCacheVersion);
|
|
||||||
} catch (err) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
setBrowserStorageItemsCount(func, 0);
|
setBrowserStorageItemsCount(func, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
// Failed
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Minimum time
|
// Minimum time
|
||||||
const minTime =
|
const minTime =
|
||||||
@ -72,7 +60,7 @@ export function iterateBrowserStorage(
|
|||||||
// Parse item
|
// Parse item
|
||||||
const parseItem = (index: number): true | undefined => {
|
const parseItem = (index: number): true | undefined => {
|
||||||
const name = browserCachePrefix + index.toString();
|
const name = browserCachePrefix + index.toString();
|
||||||
const item = func.getItem(name);
|
const item = getStoredItem(func, name);
|
||||||
|
|
||||||
if (typeof item !== 'string') {
|
if (typeof item !== 'string') {
|
||||||
// Does not exist
|
// Does not exist
|
||||||
@ -100,7 +88,7 @@ export function iterateBrowserStorage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove item
|
// Remove item
|
||||||
func.removeItem(name);
|
removeStoredItem(func, name);
|
||||||
};
|
};
|
||||||
|
|
||||||
let total = getBrowserStorageItemsCount(func);
|
let total = getBrowserStorageItemsCount(func);
|
||||||
|
42
packages/core/src/browser-storage/item.ts
Normal file
42
packages/core/src/browser-storage/item.ts
Normal 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) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,12 @@ import {
|
|||||||
} from './data';
|
} from './data';
|
||||||
import { getBrowserStorage } from './global';
|
import { getBrowserStorage } from './global';
|
||||||
import { initBrowserStorage } from './index';
|
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
|
* Function to cache icons
|
||||||
@ -23,12 +28,8 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function store(key: BrowserStorageType): true | undefined {
|
function store(key: BrowserStorageType): true | undefined {
|
||||||
if (!browserStorageConfig[key]) {
|
let func: BrowserStorageInstance | undefined;
|
||||||
return;
|
if (!browserStorageConfig[key] || !(func = getBrowserStorage(key))) {
|
||||||
}
|
|
||||||
|
|
||||||
const func = getBrowserStorage(key);
|
|
||||||
if (!func) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,8 +38,7 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
|
|||||||
let index: number;
|
let index: number;
|
||||||
if (set.size) {
|
if (set.size) {
|
||||||
// Remove item from set
|
// Remove item from set
|
||||||
index = Array.from(set).shift() as number;
|
set.delete((index = Array.from(set).shift() as number));
|
||||||
set.delete(index);
|
|
||||||
} else {
|
} else {
|
||||||
// Create new index
|
// Create new index
|
||||||
index = getBrowserStorageItemsCount(func);
|
index = getBrowserStorageItemsCount(func);
|
||||||
@ -48,22 +48,17 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create and save item
|
// Create and save item
|
||||||
try {
|
|
||||||
const item: BrowserStorageItem = {
|
const item: BrowserStorageItem = {
|
||||||
cached: Math.floor(Date.now() / browserStorageHour),
|
cached: Math.floor(Date.now() / browserStorageHour),
|
||||||
provider: storage.provider,
|
provider: storage.provider,
|
||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
func.setItem(
|
|
||||||
|
return setStoredItem(
|
||||||
|
func,
|
||||||
browserCachePrefix + index.toString(),
|
browserCachePrefix + index.toString(),
|
||||||
JSON.stringify(item)
|
JSON.stringify(item)
|
||||||
);
|
);
|
||||||
} catch (err) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Success
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not store empty sets
|
// Do not store empty sets
|
||||||
|
Loading…
Reference in New Issue
Block a user