doc.yeswiki.pro/node_modules/@vueuse/integrations/useCookies.mjs

77 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-05-20 19:37:42 +03:00
import { tryOnScopeDispose } from '@vueuse/shared';
import { ref } from 'vue-demi';
import Cookie from 'universal-cookie';
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
function createCookies(req) {
const universalCookie = new Cookie(req ? req.headers.cookie : null);
return (dependencies, { doNotParse = false, autoUpdateDependencies = false } = {}) => useCookies(dependencies, { doNotParse, autoUpdateDependencies }, universalCookie);
}
function useCookies(dependencies, { doNotParse = false, autoUpdateDependencies = false } = {}, cookies = new Cookie()) {
const watchingDependencies = autoUpdateDependencies ? [...dependencies || []] : dependencies;
let previousCookies = cookies.getAll({ doNotParse: true });
const touches = ref(0);
const onChange = () => {
const newCookies = cookies.getAll({ doNotParse: true });
if (shouldUpdate(
watchingDependencies || null,
newCookies,
previousCookies
))
touches.value++;
previousCookies = newCookies;
};
cookies.addChangeListener(onChange);
tryOnScopeDispose(() => {
cookies.removeChangeListener(onChange);
});
return {
/**
* Reactive get cookie by name. If **autoUpdateDependencies = true** then it will update watching dependencies
*/
get: (...args) => {
if (autoUpdateDependencies && watchingDependencies && !watchingDependencies.includes(args[0]))
watchingDependencies.push(args[0]);
touches.value;
return cookies.get(args[0], __spreadValues({ doNotParse }, args[1]));
},
/**
* Reactive get all cookies
*/
getAll: (...args) => {
touches.value;
return cookies.getAll(__spreadValues({ doNotParse }, args[0]));
},
set: (...args) => cookies.set(...args),
remove: (...args) => cookies.remove(...args),
addChangeListener: (...args) => cookies.addChangeListener(...args),
removeChangeListener: (...args) => cookies.removeChangeListener(...args)
};
}
function shouldUpdate(dependencies, newCookies, oldCookies) {
if (!dependencies)
return true;
for (const dependency of dependencies) {
if (newCookies[dependency] !== oldCookies[dependency])
return true;
}
return false;
}
export { createCookies, useCookies };