@nent/core
Version:
154 lines (148 loc) • 5.36 kB
JavaScript
/*!
* NENT 2022
*/
;
const mutex = require('./mutex-7ae5ebad.js');
const factory = require('./factory-0d7ddff9.js');
const memory = require('./memory-33fe6263.js');
const index = require('./index-96f3ab3f.js');
/* istanbul ignore file */
exports.VisitStrategy = void 0;
(function (VisitStrategy) {
VisitStrategy["once"] = "once";
VisitStrategy["always"] = "always";
VisitStrategy["optional"] = "optional";
})(exports.VisitStrategy || (exports.VisitStrategy = {}));
/* istanbul ignore file */
const store = index.createStore({
storageProvider: 'storage',
storedVisits: [],
sessionVisits: [],
});
const { state, onChange, reset, dispose } = store;
const visitKey = 'visits';
const sessionFallback = new memory.InMemoryProvider();
const storageFallback = new memory.InMemoryProvider();
function parseVisits(visits) {
return JSON.parse(visits || '[]');
}
function stringifyVisits(visits) {
return JSON.stringify(visits || '[]');
}
const sessionMutex = new mutex.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 factory.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 factory.getDataProvider('session')) ||
sessionFallback);
await provider.set(visitKey, stringifyVisits(visits));
});
}
const storageMutex = new mutex.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 factory.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 factory.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 == exports.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])]);
}
exports.getSessionVisits = getSessionVisits;
exports.getStoredVisits = getStoredVisits;
exports.getVisits = getVisits;
exports.hasVisited = hasVisited;
exports.markVisit = markVisit;
exports.recordVisit = recordVisit;