47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
let statisticsLocalSessionKey = "";
|
|
let statisticsLocalSessionRef = "";
|
|
|
|
export function uuidv4() {
|
|
// copied from https://stackoverflow.com/a/2117523/669561
|
|
// @ts-ignore
|
|
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
|
|
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
|
|
);
|
|
}
|
|
|
|
export function getLocalSessionKey() {
|
|
return (
|
|
window.sessionStorage.getItem("statisticsLocalSessionKey") ||
|
|
statisticsLocalSessionKey ||
|
|
""
|
|
);
|
|
}
|
|
|
|
export function generateLocalSessionKey() {
|
|
let localSessionKey =
|
|
window.sessionStorage.getItem("statisticsLocalSessionKey") ||
|
|
statisticsLocalSessionKey;
|
|
if (!localSessionKey) {
|
|
localSessionKey = uuidv4();
|
|
}
|
|
statisticsLocalSessionKey = localSessionKey;
|
|
window.sessionStorage.setItem("statisticsLocalSessionKey", localSessionKey);
|
|
|
|
return localSessionKey;
|
|
}
|
|
|
|
export function getLocalSessionRef() {
|
|
return (
|
|
window.sessionStorage.getItem("statisticsLocalSessionRef") ||
|
|
statisticsLocalSessionRef ||
|
|
""
|
|
);
|
|
}
|
|
|
|
export function setLocalSessionRef(sessionRef: string) {
|
|
if (sessionRef) {
|
|
statisticsLocalSessionRef = sessionRef;
|
|
window.sessionStorage.setItem("statisticsLocalSessionRef", sessionRef);
|
|
}
|
|
}
|