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

chore: limits for browser storage

This commit is contained in:
Vjacheslav Trushkin 2023-08-01 21:42:56 +03:00
parent 617f394b30
commit 172d09d40f
2 changed files with 14 additions and 4 deletions

View File

@ -9,3 +9,6 @@ export const browserCacheVersionKey = browserCachePrefix + '-version';
// Cache expiration // Cache expiration
export const browserStorageHour = 3600000; export const browserStorageHour = 3600000;
export const browserStorageCacheExpiration = 168; // In hours export const browserStorageCacheExpiration = 168; // In hours
// Maximum number of stored items
export const browserStorageLimit = 50;

View File

@ -1,6 +1,10 @@
import type { IconifyJSON } from '@iconify/types'; import type { IconifyJSON } from '@iconify/types';
import type { IconStorage } from '../storage/storage'; import type { IconStorage } from '../storage/storage';
import { browserCachePrefix, browserStorageHour } from './config'; import {
browserCachePrefix,
browserStorageHour,
browserStorageLimit,
} from './config';
import { import {
getBrowserStorageItemsCount, getBrowserStorageItemsCount,
setBrowserStorageItemsCount, setBrowserStorageItemsCount,
@ -13,7 +17,7 @@ import {
import { iterateBrowserStorage } from './foreach'; import { iterateBrowserStorage } from './foreach';
import { getBrowserStorage } from './global'; import { getBrowserStorage } from './global';
import { initBrowserStorage } from './index'; import { initBrowserStorage } from './index';
import { setStoredItem } from './item'; import { getStoredItem, setStoredItem } from './item';
import type { import type {
BrowserStorageInstance, BrowserStorageInstance,
BrowserStorageItem, BrowserStorageItem,
@ -81,9 +85,12 @@ export function storeInBrowserStorage(storage: IconStorage, data: IconifyJSON) {
// Remove item from set // Remove item from set
set.delete((index = Array.from(set).shift() as number)); set.delete((index = Array.from(set).shift() as number));
} else { } else {
// Create new index // Append new item, unless exceeded storage limit
index = getBrowserStorageItemsCount(func); index = getBrowserStorageItemsCount(func);
if (!setBrowserStorageItemsCount(func, index + 1)) { if (
index >= browserStorageLimit ||
!setBrowserStorageItemsCount(func, index + 1)
) {
return; return;
} }
} }