fix localstorage

This commit is contained in:
Toshit Chawda 2024-07-13 11:49:45 -07:00
parent 29bbda0700
commit 80abf239b4
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D

View file

@ -1,56 +1,56 @@
import IDBMapSync from "@webreflection/idb-map/sync";
const store = new IDBMapSync(window.__location.host, {
prefix: "Storage",
durability: "relaxed"
});
await store.sync();
function storageProxy(scope: Storage): Storage {
const store = new IDBMapSync(window.__location.host);
return new Proxy(scope, {
get(target, prop) {
switch (prop) {
case "getItem":
return (key: string) => {
return store.get(key);
}
return new Proxy(scope, {
get(target, prop) {
switch (prop) {
case "getItem":
return (key: string) => {
return store.get(key);
}
case "setItem":
return (key: string, value: string) => {
store.set(key, value);
}
case "setItem":
return (key: string, value: string) => {
store.set(key, value);
store.sync();
}
case "removeItem":
return (key: string) => {
store.delete(key);
}
case "removeItem":
return (key: string) => {
store.delete(key);
store.sync();
}
case "clear":
return () => {
store.clear();
}
case "clear":
return () => {
store.clear();
store.sync();
}
case "key":
return (index: number) => {
store.keys()[index];
}
case "key":
return (index: number) => {
store.keys()[index];
}
case "length":
return store.size;
default:
return store.get(prop);
}
},
case "length":
return store.size;
default:
return store.get(prop);
}
},
set(target, prop, value) {
store.set(prop, value);
store.sync();
},
defineProperty(target, property, attributes) {
store.set(property as string, attributes.value);
return true;
},
})
defineProperty(target, property, attributes) {
target.setItem(property as string, attributes.value);
return true;
},
})
}
const localStorageProxy = storageProxy(window.localStorage);
@ -60,4 +60,4 @@ delete window.localStorage;
delete window.sessionStorage;
window.localStorage = localStorageProxy;
window.sessionStorage = sessionStorageProxy;
window.sessionStorage = sessionStorageProxy;