Skip to content

Commit

Permalink
sync storage
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalnjs committed Oct 11, 2024
1 parent 24c1a79 commit 8a4c4bb
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/modules/storage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class Storage {
constructor(id) {
this.id = id;
this.syncWithCookie();
}

set(key, value) {
let temp = this.object;
temp[key] = value;
localStorage.setItem(this.id, JSON.stringify(temp));
setCookie(this.id, JSON.stringify(temp), 7);
return this;
}

Expand All @@ -22,16 +24,46 @@ class Storage {
let temp = this.object;
delete temp[key];
localStorage.setItem(this.id, JSON.stringify(temp));
setCookie(this.id, JSON.stringify(temp), 7);
return this;
}

obliterate() {
localStorage.removeItem(this.id);
setCookie(this.id, "", -1);
}

get object() {
return JSON.parse(localStorage.getItem(this.id)) || {};
}

syncWithCookie() {
const cookieData = getCookie(this.id);
if (cookieData) {
localStorage.setItem(this.id, cookieData);
}
}
}

function setCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/; domain=.vssfalcons.com";
}

function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

export default new Storage("virtual-checker-2");
export default new Storage("virtual-falcons-2");

0 comments on commit 8a4c4bb

Please sign in to comment.