2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-07 15:44:05 +00:00

Restructure browser storage code

This commit is contained in:
Vjacheslav Trushkin 2022-06-28 21:16:08 +03:00
parent 7ca4026eb6
commit a4240f122a
10 changed files with 269 additions and 235 deletions

View File

@ -61,6 +61,10 @@
"require": "./lib/browser-storage/config.cjs", "require": "./lib/browser-storage/config.cjs",
"import": "./lib/browser-storage/config.mjs" "import": "./lib/browser-storage/config.mjs"
}, },
"./lib/browser-storage/data": {
"require": "./lib/browser-storage/data.cjs",
"import": "./lib/browser-storage/data.mjs"
},
"./lib/browser-storage/functions": { "./lib/browser-storage/functions": {
"require": "./lib/browser-storage/functions.cjs", "require": "./lib/browser-storage/functions.cjs",
"import": "./lib/browser-storage/functions.mjs" "import": "./lib/browser-storage/functions.mjs"
@ -77,6 +81,10 @@
"require": "./lib/browser-storage/mock.cjs", "require": "./lib/browser-storage/mock.cjs",
"import": "./lib/browser-storage/mock.mjs" "import": "./lib/browser-storage/mock.mjs"
}, },
"./lib/browser-storage/types": {
"require": "./lib/browser-storage/types.cjs",
"import": "./lib/browser-storage/types.mjs"
},
"./lib/builder/functions": { "./lib/builder/functions": {
"require": "./lib/builder/functions.cjs", "require": "./lib/builder/functions.cjs",
"import": "./lib/builder/functions.mjs" "import": "./lib/builder/functions.mjs"

View File

@ -5,3 +5,7 @@ export const browserCacheVersion = 'iconify2';
export const browserCachePrefix = 'iconify'; export const browserCachePrefix = 'iconify';
export const browserCacheCountKey = browserCachePrefix + '-count'; export const browserCacheCountKey = browserCachePrefix + '-count';
export const browserCacheVersionKey = browserCachePrefix + '-version'; export const browserCacheVersionKey = browserCachePrefix + '-version';
// Cache expiration
export const browserStorageHour = 3600000;
export const browserStorageCacheExpiration = 168; // In hours

View File

@ -0,0 +1,29 @@
import type {
BrowserStorageConfig,
BrowserStorageCount,
BrowserStorageEmptyList,
} from './types';
/**
* Storage configuration
*/
export const browserStorageConfig: BrowserStorageConfig = {
local: true,
session: true,
};
/**
* Items counter
*/
export const browserStorageItemsCount: BrowserStorageCount = {
local: 0,
session: 0,
};
/**
* List of empty items
*/
export const browserStorageEmptyItems: BrowserStorageEmptyList = {
local: [],
session: [],
};

View File

@ -1,4 +1,4 @@
import { config } from './index'; import { browserStorageConfig } from './data';
/** /**
* Cache types * Cache types
@ -15,12 +15,13 @@ export function toggleBrowserCache(
switch (storage) { switch (storage) {
case 'local': case 'local':
case 'session': case 'session':
config[storage] = value; browserStorageConfig[storage] = value;
break; break;
case 'all': case 'all':
for (const key in config) { for (const key in browserStorageConfig) {
config[key as keyof typeof config] = value; browserStorageConfig[key as keyof typeof browserStorageConfig] =
value;
} }
break; break;
} }

View File

@ -6,58 +6,21 @@ import {
browserCachePrefix, browserCachePrefix,
browserCacheVersion, browserCacheVersion,
browserCacheVersionKey, browserCacheVersionKey,
browserStorageCacheExpiration,
browserStorageHour,
} from './config'; } from './config';
import {
interface StorageType<T> { browserStorageConfig,
local: T; browserStorageEmptyItems,
session: T; browserStorageItemsCount,
} } from './data';
import type { BrowserStorageConfig, BrowserStorageItem } from './types';
type StorageConfig = StorageType<boolean>;
type StorageCount = StorageType<number>;
type StorageEmptyList = StorageType<number[]>;
export interface StoredItem {
cached: number;
provider: string;
data: IconifyJSON;
}
/**
* Cache expiration
*/
const hour = 3600000;
const cacheExpiration = 168; // In hours
/**
* Storage configuration
*/
export const config: StorageConfig = {
local: true,
session: true,
};
/** /**
* Flag to check if storage has been loaded * Flag to check if storage has been loaded
*/ */
let loaded = false; let loaded = false;
/**
* Items counter
*/
export const count: StorageCount = {
local: 0,
session: 0,
};
/**
* List of empty items
*/
export const emptyList: StorageEmptyList = {
local: [],
session: [],
};
/** /**
* Fake window for unit testing * Fake window for unit testing
*/ */
@ -75,7 +38,9 @@ export function mock(fakeWindow: FakeWindow): void {
* *
* @param key * @param key
*/ */
function getGlobal(key: keyof StorageConfig): typeof localStorage | null { function getGlobal(
key: keyof BrowserStorageConfig
): typeof localStorage | null {
const attr = key + 'Storage'; const attr = key + 'Storage';
try { try {
if ( if (
@ -90,7 +55,7 @@ function getGlobal(key: keyof StorageConfig): typeof localStorage | null {
} }
// Failed - mark as disabled // Failed - mark as disabled
config[key] = false; browserStorageConfig[key] = false;
return null; return null;
} }
@ -99,12 +64,12 @@ function getGlobal(key: keyof StorageConfig): typeof localStorage | null {
*/ */
function setCount( function setCount(
storage: typeof localStorage, storage: typeof localStorage,
key: keyof StorageConfig, key: keyof BrowserStorageConfig,
value: number value: number
): boolean { ): boolean {
try { try {
storage.setItem(browserCacheCountKey, value.toString()); storage.setItem(browserCacheCountKey, value.toString());
count[key] = value; browserStorageItemsCount[key] = value;
return true; return true;
} catch (err) { } catch (err) {
// //
@ -134,7 +99,7 @@ function getCount(storage: typeof localStorage): number {
*/ */
function initCache( function initCache(
storage: typeof localStorage, storage: typeof localStorage,
key: keyof StorageConfig key: keyof BrowserStorageConfig
): void { ): void {
try { try {
storage.setItem(browserCacheVersionKey, browserCacheVersion); storage.setItem(browserCacheVersionKey, browserCacheVersion);
@ -170,10 +135,12 @@ export const loadCache: LoadIconsCache = (): void => {
loaded = true; loaded = true;
// Minimum time // Minimum time
const minTime = Math.floor(Date.now() / hour) - cacheExpiration; const minTime =
Math.floor(Date.now() / browserStorageHour) -
browserStorageCacheExpiration;
// Load data from storage // Load data from storage
function load(key: keyof StorageConfig): void { function load(key: keyof BrowserStorageConfig): void {
const func = getGlobal(key); const func = getGlobal(key);
if (!func) { if (!func) {
return; return;
@ -193,7 +160,7 @@ export const loadCache: LoadIconsCache = (): void => {
let valid = true; let valid = true;
try { try {
// Parse, check time stamp // Parse, check time stamp
const data = JSON.parse(item) as StoredItem; const data = JSON.parse(item) as BrowserStorageItem;
if ( if (
typeof data !== 'object' || typeof data !== 'object' ||
typeof data.cached !== 'number' || typeof data.cached !== 'number' ||
@ -243,7 +210,7 @@ export const loadCache: LoadIconsCache = (): void => {
total--; total--;
} else { } else {
// Mark as empty // Mark as empty
emptyList[key].push(i); browserStorageEmptyItems[key].push(i);
} }
} }
} }
@ -255,8 +222,8 @@ export const loadCache: LoadIconsCache = (): void => {
} }
} }
for (const key in config) { for (const key in browserStorageConfig) {
load(key as keyof StorageConfig); load(key as keyof BrowserStorageConfig);
} }
}; };
@ -271,8 +238,8 @@ export const storeCache: CacheIcons = (
loadCache(); loadCache();
} }
function store(key: keyof StorageConfig): boolean { function store(key: keyof BrowserStorageConfig): boolean {
if (!config[key]) { if (!browserStorageConfig[key]) {
return false; return false;
} }
@ -282,10 +249,10 @@ export const storeCache: CacheIcons = (
} }
// Get item index // Get item index
let index = emptyList[key].shift(); let index = browserStorageEmptyItems[key].shift();
if (index === void 0) { if (index === void 0) {
// Create new index // Create new index
index = count[key]; index = browserStorageItemsCount[key];
if (!setCount(func, key, index + 1)) { if (!setCount(func, key, index + 1)) {
return false; return false;
} }
@ -293,8 +260,8 @@ export const storeCache: CacheIcons = (
// Create and save item // Create and save item
try { try {
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data, data,
}; };

View File

@ -1,4 +1,9 @@
import { mock, count, config, emptyList } from './index'; import { mock } from './index';
import {
browserStorageItemsCount,
browserStorageConfig,
browserStorageEmptyItems,
} from './data';
/** /**
* Get next icon set prefix for testing * Get next icon set prefix for testing
@ -8,12 +13,6 @@ export function nextPrefix(): string {
return 'fake-storage-' + (prefixCounter++).toString(); return 'fake-storage-' + (prefixCounter++).toString();
} }
/**
* Cache expiration
*/
export const hour = 3600000;
export const cacheExpiration = 168; // In hours
/** /**
* Storage class * Storage class
*/ */
@ -97,10 +96,10 @@ export function reset(fakeWindow: Record<string, typeof localStorage>): void {
mock(fakeWindow); mock(fakeWindow);
// Reset all data // Reset all data
for (const key in config) { for (const key in browserStorageConfig) {
const attr = key as unknown as keyof typeof config; const attr = key as unknown as keyof typeof browserStorageConfig;
config[attr] = true; browserStorageConfig[attr] = true;
count[attr] = 0; browserStorageItemsCount[attr] = 0;
emptyList[attr] = []; browserStorageEmptyItems[attr] = [];
} }
} }

View File

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

View File

@ -1,4 +1,8 @@
import { count, config, loadCache } from '../../lib/browser-storage'; import { loadCache } from '../../lib/browser-storage';
import {
browserStorageItemsCount,
browserStorageConfig,
} from '../../lib/browser-storage/data';
import { import {
browserCacheCountKey, browserCacheCountKey,
browserCachePrefix, browserCachePrefix,
@ -14,11 +18,11 @@ describe('Testing mocked localStorage', () => {
reset({}); reset({});
// Config before tests // Config before tests
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -27,13 +31,13 @@ describe('Testing mocked localStorage', () => {
loadCache(); loadCache();
// Everything should be disabled // Everything should be disabled
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: false, local: false,
session: false, session: false,
}); });
// Nothing should have loaded // Nothing should have loaded
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -45,11 +49,11 @@ describe('Testing mocked localStorage', () => {
}); });
// Config before tests // Config before tests
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -58,13 +62,13 @@ describe('Testing mocked localStorage', () => {
loadCache(); loadCache();
// sessionStorage should be disabled // sessionStorage should be disabled
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
// Nothing should have loaded // Nothing should have loaded
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -104,11 +108,11 @@ describe('Testing mocked localStorage', () => {
}); });
// Config before tests // Config before tests
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -117,13 +121,13 @@ describe('Testing mocked localStorage', () => {
loadCache(); loadCache();
// Everything should be disabled because read-only mock throws errors // Everything should be disabled because read-only mock throws errors
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: false, local: false,
session: false, session: false,
}); });
// Nothing should have loaded // Nothing should have loaded
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -158,11 +162,11 @@ describe('Testing mocked localStorage', () => {
}); });
// Config before tests // Config before tests
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -171,13 +175,13 @@ describe('Testing mocked localStorage', () => {
loadCache(); loadCache();
// sessionStorage should be disabled // sessionStorage should be disabled
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
// One item should be in localStorage // One item should be in localStorage
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 1, local: 1,
session: 0, session: 0,
}); });
@ -190,11 +194,11 @@ describe('Testing mocked localStorage', () => {
}); });
// Config before tests // Config before tests
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
@ -203,13 +207,13 @@ describe('Testing mocked localStorage', () => {
loadCache(); loadCache();
// Everything should be working // Everything should be working
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
// Empty storage // Empty storage
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });

View File

@ -1,19 +1,20 @@
import type { IconifyJSON } from '@iconify/types'; import type { IconifyJSON } from '@iconify/types';
import type { StoredItem } from '../../lib/browser-storage'; import type { BrowserStorageItem } from '../../lib/browser-storage/types';
import { loadCache, count, config, emptyList } from '../../lib/browser-storage'; import { loadCache } from '../../lib/browser-storage';
import { getStorage, iconExists } from '../../lib/storage/storage';
import { import {
nextPrefix, browserStorageItemsCount,
createCache, browserStorageConfig,
reset, browserStorageEmptyItems,
hour, } from '../../lib/browser-storage/data';
cacheExpiration, import { getStorage, iconExists } from '../../lib/storage/storage';
} from '../../lib/browser-storage/mock'; import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock';
import { import {
browserCacheCountKey, browserCacheCountKey,
browserCachePrefix, browserCachePrefix,
browserCacheVersion, browserCacheVersion,
browserCacheVersionKey, browserCacheVersionKey,
browserStorageHour,
browserStorageCacheExpiration,
} from '../../lib/browser-storage/config'; } from '../../lib/browser-storage/config';
describe('Testing loading from localStorage', () => { describe('Testing loading from localStorage', () => {
@ -27,8 +28,8 @@ describe('Testing loading from localStorage', () => {
cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1'); cache.setItem(browserCacheCountKey, '1');
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -57,15 +58,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons, 'foo')).toBe(true); expect(iconExists(icons, 'foo')).toBe(true);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 1, local: 1,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -80,8 +81,8 @@ describe('Testing loading from localStorage', () => {
cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1'); cache.setItem(browserCacheCountKey, '1');
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -115,15 +116,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons2, 'foo')).toBe(false); expect(iconExists(icons2, 'foo')).toBe(false);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 1, local: 1,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -137,9 +138,12 @@ describe('Testing loading from localStorage', () => {
cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '1'); cache.setItem(browserCacheCountKey, '1');
const item: StoredItem = { const item: BrowserStorageItem = {
// Expiration date // Expiration date
cached: Math.floor(Date.now() / hour) - cacheExpiration - 1, cached:
Math.floor(Date.now() / browserStorageHour) -
browserStorageCacheExpiration -
1,
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -168,15 +172,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons, 'foo')).toBe(false); expect(iconExists(icons, 'foo')).toBe(false);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -192,7 +196,7 @@ describe('Testing loading from localStorage', () => {
cache.setItem( cache.setItem(
browserCachePrefix + '0', browserCachePrefix + '0',
JSON.stringify({ JSON.stringify({
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -222,15 +226,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons, 'foo')).toBe(false); expect(iconExists(icons, 'foo')).toBe(false);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -244,8 +248,8 @@ describe('Testing loading from localStorage', () => {
cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '0'); // Should be at least "1" cache.setItem(browserCacheCountKey, '0'); // Should be at least "1"
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -274,15 +278,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons, 'foo')).toBe(false); expect(iconExists(icons, 'foo')).toBe(false);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -296,8 +300,8 @@ describe('Testing loading from localStorage', () => {
cache.setItem(browserCacheVersionKey, browserCacheVersion); cache.setItem(browserCacheVersionKey, browserCacheVersion);
cache.setItem(browserCacheCountKey, '5'); cache.setItem(browserCacheCountKey, '5');
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -326,15 +330,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons, 'foo')).toBe(true); expect(iconExists(icons, 'foo')).toBe(true);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 1, local: 1,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -349,8 +353,8 @@ describe('Testing loading from localStorage', () => {
cache.setItem(browserCacheCountKey, '5'); cache.setItem(browserCacheCountKey, '5');
// Missing: 0, 2, 3 // Missing: 0, 2, 3
const item1: StoredItem = { const item1: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -361,8 +365,8 @@ describe('Testing loading from localStorage', () => {
}, },
}, },
}; };
const item4: StoredItem = { const item4: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: { data: {
prefix: prefix, prefix: prefix,
@ -395,15 +399,15 @@ describe('Testing loading from localStorage', () => {
expect(iconExists(icons, 'foo4')).toBe(true); expect(iconExists(icons, 'foo4')).toBe(true);
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 5, local: 5,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [3, 2, 0], // reserse order local: [3, 2, 0], // reserse order
session: [], session: [],
}); });
@ -423,7 +427,7 @@ describe('Testing loading from localStorage', () => {
// Create 5 items // Create 5 items
const icons: IconifyJSON[] = []; const icons: IconifyJSON[] = [];
const items: StoredItem[] = []; const items: BrowserStorageItem[] = [];
for (let i = 0; i < 6; i++) { for (let i = 0; i < 6; i++) {
const icon: IconifyJSON = { const icon: IconifyJSON = {
@ -434,8 +438,8 @@ describe('Testing loading from localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -482,15 +486,15 @@ describe('Testing loading from localStorage', () => {
} }
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 6, local: 6,
session: 3, session: 3,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [4, 2, 0], local: [4, 2, 0],
session: [1], session: [1],
}); });

View File

@ -1,25 +1,20 @@
import type { IconifyJSON } from '@iconify/types'; import type { IconifyJSON } from '@iconify/types';
import type { StoredItem } from '../../lib/browser-storage'; import type { BrowserStorageItem } from '../../lib/browser-storage/types';
import { loadCache, storeCache } from '../../lib/browser-storage';
import { import {
loadCache, browserStorageItemsCount,
storeCache, browserStorageConfig,
count, browserStorageEmptyItems,
config, } from '../../lib/browser-storage/data';
emptyList,
} from '../../lib/browser-storage';
import { getStorage, iconExists } from '../../lib/storage/storage'; import { getStorage, iconExists } from '../../lib/storage/storage';
import { import { nextPrefix, createCache, reset } from '../../lib/browser-storage/mock';
nextPrefix,
createCache,
reset,
hour,
cacheExpiration,
} from '../../lib/browser-storage/mock';
import { import {
browserCacheCountKey, browserCacheCountKey,
browserCachePrefix, browserCachePrefix,
browserCacheVersion, browserCacheVersion,
browserCacheVersionKey, browserCacheVersionKey,
browserStorageHour,
browserStorageCacheExpiration,
} from '../../lib/browser-storage/config'; } from '../../lib/browser-storage/config';
describe('Testing saving to localStorage', () => { describe('Testing saving to localStorage', () => {
@ -38,8 +33,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -61,15 +56,15 @@ describe('Testing saving to localStorage', () => {
// Check data that should have been updated because storeCache() // Check data that should have been updated because storeCache()
// should call load function before first execution // should call load function before first execution
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 1, local: 1,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -95,8 +90,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item0: StoredItem = { const item0: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon0, data: icon0,
}; };
@ -108,8 +103,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item1: StoredItem = { const item1: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon1, data: icon1,
}; };
@ -125,15 +120,15 @@ describe('Testing saving to localStorage', () => {
// Check data that should have been updated because storeCache() // Check data that should have been updated because storeCache()
// should call load function before first execution // should call load function before first execution
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 2, local: 2,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -162,8 +157,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item0: StoredItem = { const item0: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon0, data: icon0,
}; };
@ -175,8 +170,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item1: StoredItem = { const item1: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon1, data: icon1,
}; };
@ -195,15 +190,15 @@ describe('Testing saving to localStorage', () => {
loadCache(); loadCache();
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 2, local: 2,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [0], local: [0],
session: [], session: [],
}); });
@ -212,11 +207,11 @@ describe('Testing saving to localStorage', () => {
storeCache(provider, icon0); storeCache(provider, icon0);
// Check data // Check data
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 2, local: 2,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -238,7 +233,7 @@ describe('Testing saving to localStorage', () => {
// Add icon sets // Add icon sets
const icons: IconifyJSON[] = []; const icons: IconifyJSON[] = [];
const items: StoredItem[] = []; const items: BrowserStorageItem[] = [];
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
const icon: IconifyJSON = { const icon: IconifyJSON = {
prefix: prefix, prefix: prefix,
@ -248,20 +243,20 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
// Make items 2 and 4 expire // Make items 2 and 4 expire
if (i === 2 || i === 4) { if (i === 2 || i === 4) {
item.cached -= cacheExpiration + 1; item.cached -= browserStorageCacheExpiration + 1;
} }
// Change expiration for items 6 and 8 to almost expire // Change expiration for items 6 and 8 to almost expire
if (i === 6 || i === 8) { if (i === 6 || i === 8) {
item.cached -= cacheExpiration - 1; item.cached -= browserStorageCacheExpiration - 1;
} }
icons.push(icon); icons.push(icon);
@ -288,15 +283,15 @@ describe('Testing saving to localStorage', () => {
loadCache(); loadCache();
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: false, local: false,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 9, // item 9 was missing session: 9, // item 9 was missing
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
// mix of expired and skipped items // mix of expired and skipped items
// reverse order, 9 should not be there because it is last item // reverse order, 9 should not be there because it is last item
@ -321,11 +316,11 @@ describe('Testing saving to localStorage', () => {
// Add item 5 // Add item 5
storeCache(provider, icons[5]); storeCache(provider, icons[5]);
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 9, session: 9,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [4, 2, 1], session: [4, 2, 1],
}); });
@ -336,11 +331,11 @@ describe('Testing saving to localStorage', () => {
list.slice(0).forEach((index) => { list.slice(0).forEach((index) => {
expect(list.shift()).toBe(index); expect(list.shift()).toBe(index);
storeCache(provider, icons[index]); storeCache(provider, icons[index]);
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 9, session: 9,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: list, session: list,
}); });
@ -349,11 +344,11 @@ describe('Testing saving to localStorage', () => {
// Add item 10 // Add item 10
storeCache(provider, icons[10]); storeCache(provider, icons[10]);
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 10, session: 10,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -361,11 +356,11 @@ describe('Testing saving to localStorage', () => {
// Add item 11 // Add item 11
storeCache(provider, icons[11]); storeCache(provider, icons[11]);
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 11, session: 11,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -405,15 +400,15 @@ describe('Testing saving to localStorage', () => {
// Load cache // Load cache
loadCache(); loadCache();
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 0, local: 0,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -427,8 +422,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -441,15 +436,15 @@ describe('Testing saving to localStorage', () => {
// Check data that should have been updated because storeCache() // Check data that should have been updated because storeCache()
// should call load function before first execution // should call load function before first execution
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: false, session: false,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 1, local: 1,
session: 0, session: 0,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -479,8 +474,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -502,8 +497,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -523,25 +518,25 @@ describe('Testing saving to localStorage', () => {
loadCache(); loadCache();
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 3, local: 3,
session: 4, session: 4,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
// Check icon storage // Check icon storage
const iconsStorage = getStorage(provider, prefix); const iconsStorage = getStorage(provider, prefix);
for (let i = 0; i < count.local; i++) { for (let i = 0; i < browserStorageItemsCount.local; i++) {
expect(iconExists(iconsStorage, 'foo' + i.toString())).toBe(true); expect(iconExists(iconsStorage, 'foo' + i.toString())).toBe(true);
} }
for (let i = 0; i < count.session; i++) { for (let i = 0; i < browserStorageItemsCount.session; i++) {
expect(iconExists(iconsStorage, 'bar' + i.toString())).toBe(true); expect(iconExists(iconsStorage, 'bar' + i.toString())).toBe(true);
} }
@ -554,19 +549,19 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
storeCache(provider, icon); storeCache(provider, icon);
// Check data // Check data
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 4, // +1 local: 4, // +1
session: 4, session: 4,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
@ -594,8 +589,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -617,8 +612,8 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
@ -638,25 +633,25 @@ describe('Testing saving to localStorage', () => {
loadCache(); loadCache();
// Check data // Check data
expect(config).toEqual({ expect(browserStorageConfig).toEqual({
local: true, local: true,
session: true, session: true,
}); });
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 3, local: 3,
session: 4, session: 4,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });
// Check icon storage // Check icon storage
const iconsStorage = getStorage(provider, prefix); const iconsStorage = getStorage(provider, prefix);
for (let i = 0; i < count.local; i++) { for (let i = 0; i < browserStorageItemsCount.local; i++) {
expect(iconExists(iconsStorage, 'foo' + i.toString())).toBe(true); expect(iconExists(iconsStorage, 'foo' + i.toString())).toBe(true);
} }
for (let i = 0; i < count.session; i++) { for (let i = 0; i < browserStorageItemsCount.session; i++) {
expect(iconExists(iconsStorage, 'bar' + i.toString())).toBe(true); expect(iconExists(iconsStorage, 'bar' + i.toString())).toBe(true);
} }
@ -672,19 +667,19 @@ describe('Testing saving to localStorage', () => {
}, },
}, },
}; };
const item: StoredItem = { const item: BrowserStorageItem = {
cached: Math.floor(Date.now() / hour), cached: Math.floor(Date.now() / browserStorageHour),
provider, provider,
data: icon, data: icon,
}; };
storeCache(provider, icon); storeCache(provider, icon);
// Check data // Check data
expect(count).toEqual({ expect(browserStorageItemsCount).toEqual({
local: 3, local: 3,
session: 5, session: 5,
}); });
expect(emptyList).toEqual({ expect(browserStorageEmptyItems).toEqual({
local: [], local: [],
session: [], session: [],
}); });