@nent/core
Version:
147 lines (142 loc) • 5.21 kB
JavaScript
/*!
* NENT 2022
*/
import { M as Mutex } from './mutex-e5645c85.js';
import { g as getDataProvider } from './factory-acbf0d3d.js';
import { I as InMemoryProvider } from './memory-0d63dacd.js';
import { c as createStore } from './index-4bfabbbd.js';
/* istanbul ignore file */
var VisitStrategy;
(function (VisitStrategy) {
VisitStrategy["once"] = "once";
VisitStrategy["always"] = "always";
VisitStrategy["optional"] = "optional";
})(VisitStrategy || (VisitStrategy = {}));
/* istanbul ignore file */
const store = createStore({
storageProvider: 'storage',
storedVisits: [],
sessionVisits: [],
});
const { state, onChange, reset, dispose } = store;
const visitKey = 'visits';
const sessionFallback = new InMemoryProvider();
const storageFallback = new InMemoryProvider();
function parseVisits(visits) {
return JSON.parse(visits || '[]');
}
function stringifyVisits(visits) {
return JSON.stringify(visits || '[]');
}
const sessionMutex = new Mutex();
/**
* It gets the session visits from the session data provider, or the session fallback if the session
* data provider is not available
* @returns An array of visits.
*/
async function getSessionVisits() {
return await sessionMutex.dispatch(async () => {
var provider = (await getDataProvider('session')) || sessionFallback;
const visits = await provider.get(visitKey);
return visits ? parseVisits(visits) : [];
});
}
/**
* It takes an array of strings, and then it sets the session visits to that array of strings
* @param {string[]} visits - string[] - An array of strings representing the visits to be stored.
* @returns A promise that resolves to the result of the dispatch function.
*/
async function setSessionVisits(visits) {
return await sessionMutex.dispatch(async () => {
const provider = ((await getDataProvider('session')) ||
sessionFallback);
await provider.set(visitKey, stringifyVisits(visits));
});
}
const storageMutex = new Mutex();
/**
* It gets the visits from the storage provider, and if there's no storage provider, it uses the
* fallback
* @returns An array of objects.
*/
async function getStoredVisits() {
return await storageMutex.dispatch(async () => {
var provider = (await getDataProvider(state.storageProvider)) ||
storageFallback;
const visits = await provider.get(visitKey);
return visits ? parseVisits(visits) : [];
});
}
/**
* It takes an array of strings, and stores them in the browser's storage
* @param {string[]} visits - string[] - An array of strings representing the visits to be stored.
* @returns A promise that resolves to the result of the async function.
*/
async function setStoredVisits(visits) {
return await storageMutex.dispatch(async () => {
var provider = ((await getDataProvider(state.storageProvider)) || storageFallback);
await provider.set(visitKey, stringifyVisits(visits));
});
}
/**
* It returns true if the given URL is in the list of visits, and false otherwise
* @param {string} url - The URL of the page you want to check.
* @returns A boolean value.
*/
async function hasVisited(url) {
const sessionVisits = await getSessionVisits();
const storageVisits = await getStoredVisits();
const visits = [...sessionVisits, ...storageVisits];
return visits.includes(url);
}
/**
* "If the visit strategy is once, store the visit, otherwise mark the visit."
*
* The function is written in TypeScript, which is a superset of JavaScript. TypeScript is a typed
* language, which means that it has a type system. The type system is used to catch errors at compile
* time
* @param {VisitStrategy} visit - VisitStrategy - this is the visit strategy that the user has chosen.
* @param {string} url - The URL to visit
*/
async function recordVisit(visit, url) {
if (visit == VisitStrategy.once) {
await storeVisit(url);
}
else {
await markVisit(url);
}
}
/**
* It gets the session visits and stored visits, then combines them into a single array, removing
* duplicates
* @returns An array of unique visits.
*/
async function getVisits() {
const sessionVisits = await getSessionVisits();
const storedVisits = await getStoredVisits();
return [...new Set([...sessionVisits, ...storedVisits])];
}
/**
* It gets the current session's visits, checks if the current page is already in the list, and if not,
* adds it to the list
* @param {string} url - The URL of the page that was visited.
* @returns An array of unique URLs that have been visited in the current session.
*/
async function markVisit(url) {
const sessionVisits = await getSessionVisits();
if (sessionVisits.includes(url))
return;
await setSessionVisits([...new Set([...sessionVisits, url])]);
}
/**
* It stores a visit to a URL in local storage
* @param {string} url - The URL of the page that was visited.
* @returns An array of unique urls
*/
async function storeVisit(url) {
const storedVisits = await getStoredVisits();
if (storedVisits.includes(url))
return;
await setStoredVisits([...new Set([...storedVisits, url])]);
}
export { VisitStrategy as V, getStoredVisits as a, getVisits as b, getSessionVisits as g, hasVisited as h, markVisit as m, recordVisit as r };