@algolia/cache-browser-local-storage
Advanced tools
@@ -5,2 +5,14 @@ 'use strict'; | ||
| function yieldToMain() { | ||
| // eslint-disable-next-line no-undef | ||
| const g = typeof globalThis !== 'undefined' ? globalThis : undefined; | ||
| if (g && g.scheduler && g.scheduler.yield) { | ||
| return g.scheduler.yield().catch((error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Failed to yield to main: ', error); | ||
| return new Promise(resolve => setTimeout(resolve, 0)); | ||
| }); | ||
| } | ||
| return new Promise(resolve => setTimeout(resolve, 0)); | ||
| } | ||
| function createBrowserLocalStorageCache(options) { | ||
@@ -22,17 +34,15 @@ const namespaceKey = `algoliasearch-client-js-${options.key}`; | ||
| }; | ||
| const removeOutdatedCacheItems = () => { | ||
| const getFilteredNamespace = () => { | ||
| const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null; | ||
| const namespace = getNamespace(); | ||
| const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => { | ||
| return cacheItem.timestamp !== undefined; | ||
| const currentTime = new Date().getTime(); | ||
| return Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => { | ||
| if (!cacheItem || cacheItem.timestamp === undefined) { | ||
| return false; | ||
| } | ||
| if (!timeToLive) { | ||
| return true; | ||
| } | ||
| return cacheItem.timestamp + timeToLive >= currentTime; | ||
| })); | ||
| setNamespace(filteredNamespaceWithoutOldFormattedCacheItems); | ||
| if (!timeToLive) | ||
| return; | ||
| const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => { | ||
| const currentTimestamp = new Date().getTime(); | ||
| const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp; | ||
| return !isExpired; | ||
| })); | ||
| setNamespace(filteredNamespaceWithoutExpiredItems); | ||
| }; | ||
@@ -43,18 +53,16 @@ return { | ||
| }) { | ||
| return Promise.resolve() | ||
| .then(() => { | ||
| removeOutdatedCacheItems(); | ||
| return yieldToMain().then(() => { | ||
| const namespace = getFilteredNamespace(); | ||
| const keyAsString = JSON.stringify(key); | ||
| return getNamespace()[keyAsString]; | ||
| }) | ||
| .then(value => { | ||
| return Promise.all([value ? value.value : defaultValue(), value !== undefined]); | ||
| }) | ||
| .then(([value, exists]) => { | ||
| return Promise.all([value, exists || events.miss(value)]); | ||
| }) | ||
| .then(([value]) => value); | ||
| const cachedItem = namespace[keyAsString]; | ||
| setNamespace(namespace); | ||
| if (cachedItem) { | ||
| return cachedItem.value; | ||
| } | ||
| // eslint-disable-next-line promise/no-nesting | ||
| return defaultValue().then((value) => events.miss(value).then(() => value)); | ||
| }); | ||
| }, | ||
| set(key, value) { | ||
| return Promise.resolve().then(() => { | ||
| return yieldToMain().then(() => { | ||
| const namespace = getNamespace(); | ||
@@ -71,3 +79,3 @@ // eslint-disable-next-line functional/immutable-data | ||
| delete(key) { | ||
| return Promise.resolve().then(() => { | ||
| return yieldToMain().then(() => { | ||
| const namespace = getNamespace(); | ||
@@ -74,0 +82,0 @@ // eslint-disable-next-line functional/immutable-data |
@@ -0,1 +1,13 @@ | ||
| function yieldToMain() { | ||
| // eslint-disable-next-line no-undef | ||
| const g = typeof globalThis !== 'undefined' ? globalThis : undefined; | ||
| if (g && g.scheduler && g.scheduler.yield) { | ||
| return g.scheduler.yield().catch((error) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Failed to yield to main: ', error); | ||
| return new Promise(resolve => setTimeout(resolve, 0)); | ||
| }); | ||
| } | ||
| return new Promise(resolve => setTimeout(resolve, 0)); | ||
| } | ||
| function createBrowserLocalStorageCache(options) { | ||
@@ -17,17 +29,15 @@ const namespaceKey = `algoliasearch-client-js-${options.key}`; | ||
| }; | ||
| const removeOutdatedCacheItems = () => { | ||
| const getFilteredNamespace = () => { | ||
| const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null; | ||
| const namespace = getNamespace(); | ||
| const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => { | ||
| return cacheItem.timestamp !== undefined; | ||
| const currentTime = new Date().getTime(); | ||
| return Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => { | ||
| if (!cacheItem || cacheItem.timestamp === undefined) { | ||
| return false; | ||
| } | ||
| if (!timeToLive) { | ||
| return true; | ||
| } | ||
| return cacheItem.timestamp + timeToLive >= currentTime; | ||
| })); | ||
| setNamespace(filteredNamespaceWithoutOldFormattedCacheItems); | ||
| if (!timeToLive) | ||
| return; | ||
| const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => { | ||
| const currentTimestamp = new Date().getTime(); | ||
| const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp; | ||
| return !isExpired; | ||
| })); | ||
| setNamespace(filteredNamespaceWithoutExpiredItems); | ||
| }; | ||
@@ -38,18 +48,16 @@ return { | ||
| }) { | ||
| return Promise.resolve() | ||
| .then(() => { | ||
| removeOutdatedCacheItems(); | ||
| return yieldToMain().then(() => { | ||
| const namespace = getFilteredNamespace(); | ||
| const keyAsString = JSON.stringify(key); | ||
| return getNamespace()[keyAsString]; | ||
| }) | ||
| .then(value => { | ||
| return Promise.all([value ? value.value : defaultValue(), value !== undefined]); | ||
| }) | ||
| .then(([value, exists]) => { | ||
| return Promise.all([value, exists || events.miss(value)]); | ||
| }) | ||
| .then(([value]) => value); | ||
| const cachedItem = namespace[keyAsString]; | ||
| setNamespace(namespace); | ||
| if (cachedItem) { | ||
| return cachedItem.value; | ||
| } | ||
| // eslint-disable-next-line promise/no-nesting | ||
| return defaultValue().then((value) => events.miss(value).then(() => value)); | ||
| }); | ||
| }, | ||
| set(key, value) { | ||
| return Promise.resolve().then(() => { | ||
| return yieldToMain().then(() => { | ||
| const namespace = getNamespace(); | ||
@@ -66,3 +74,3 @@ // eslint-disable-next-line functional/immutable-data | ||
| delete(key) { | ||
| return Promise.resolve().then(() => { | ||
| return yieldToMain().then(() => { | ||
| const namespace = getNamespace(); | ||
@@ -69,0 +77,0 @@ // eslint-disable-next-line functional/immutable-data |
+2
-2
| { | ||
| "name": "@algolia/cache-browser-local-storage", | ||
| "version": "4.26.0", | ||
| "version": "4.27.0", | ||
| "private": false, | ||
@@ -20,4 +20,4 @@ "description": "Promise-based cache library for browser using local storage.", | ||
| "dependencies": { | ||
| "@algolia/cache-common": "4.26.0" | ||
| "@algolia/cache-common": "4.27.0" | ||
| } | ||
| } |
Unstable ownership
Supply chain riskA new collaborator has begun publishing package versions. Package stability and security risk may be elevated.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8449
3.63%203
8.56%1
Infinity%+ Added
- Removed
Updated