mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-15 07:20:02 -04:00
re-implement idbmap for storage proxy
This commit is contained in:
parent
453f15f796
commit
efa30f6d84
3 changed files with 17 additions and 21 deletions
|
@ -43,7 +43,7 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@mercuryworkshop/bare-mux": "^2.0.1",
|
"@mercuryworkshop/bare-mux": "^2.0.1",
|
||||||
"@webreflection/idb-map": "^0.1.3",
|
"@webreflection/idb-map": "^0.3.1",
|
||||||
"astravel": "^0.6.1",
|
"astravel": "^0.6.1",
|
||||||
"astring": "^1.8.6",
|
"astring": "^1.8.6",
|
||||||
"meriyah": "^4.4.2"
|
"meriyah": "^4.4.2"
|
||||||
|
|
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
|
@ -12,8 +12,8 @@ importers:
|
||||||
specifier: ^2.0.1
|
specifier: ^2.0.1
|
||||||
version: 2.0.1
|
version: 2.0.1
|
||||||
'@webreflection/idb-map':
|
'@webreflection/idb-map':
|
||||||
specifier: ^0.1.3
|
specifier: ^0.3.1
|
||||||
version: 0.1.3
|
version: 0.3.1
|
||||||
astravel:
|
astravel:
|
||||||
specifier: ^0.6.1
|
specifier: ^0.6.1
|
||||||
version: 0.6.1
|
version: 0.6.1
|
||||||
|
@ -477,8 +477,8 @@ packages:
|
||||||
'@ungap/structured-clone@1.2.0':
|
'@ungap/structured-clone@1.2.0':
|
||||||
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
|
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
|
||||||
|
|
||||||
'@webreflection/idb-map@0.1.3':
|
'@webreflection/idb-map@0.3.1':
|
||||||
resolution: {integrity: sha512-7lTEpXDgpy9xueW4NSNkBHgqedz/dlGgtwTZa4fRMKWJKcnC9cORVlXwV7qNyYYKarT8gpB1+wv5UNBYhjgc8w==}
|
resolution: {integrity: sha512-lRCanqwR7tHHFohJHAMSMEZnoNPvgjcKr0f5e4y+lTJA+fctT61EZ+f5pT5/+8+wlSsMAvXjzfKRLT6o9aqxbA==}
|
||||||
|
|
||||||
abort-controller@3.0.0:
|
abort-controller@3.0.0:
|
||||||
resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}
|
resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}
|
||||||
|
@ -1764,7 +1764,7 @@ snapshots:
|
||||||
|
|
||||||
'@ungap/structured-clone@1.2.0': {}
|
'@ungap/structured-clone@1.2.0': {}
|
||||||
|
|
||||||
'@webreflection/idb-map@0.1.3': {}
|
'@webreflection/idb-map@0.3.1': {}
|
||||||
|
|
||||||
abort-controller@3.0.0:
|
abort-controller@3.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|
|
@ -1,44 +1,40 @@
|
||||||
// import IDBMap from "@webreflection/idb-map";
|
import IDBMapSync from "@webreflection/idb-map/sync";
|
||||||
|
|
||||||
// this will be converted to use IDB later but i can't figure out how to make it work synchronously
|
|
||||||
|
|
||||||
function filterStorage(scope: Storage) {
|
|
||||||
return Object.keys(scope).filter((key) => key.startsWith(window.__location.host));
|
|
||||||
}
|
|
||||||
|
|
||||||
function storageProxy(scope: Storage): Storage {
|
function storageProxy(scope: Storage): Storage {
|
||||||
// const store = new IDBMap(window.__location.host);
|
const store = new IDBMapSync(window.__location.host);
|
||||||
|
|
||||||
return new Proxy(scope, {
|
return new Proxy(scope, {
|
||||||
get(target, prop) {
|
async get(target, prop) {
|
||||||
|
await store.sync();
|
||||||
|
|
||||||
switch (prop) {
|
switch (prop) {
|
||||||
case "getItem":
|
case "getItem":
|
||||||
return (key: string) => {
|
return (key: string) => {
|
||||||
return target.getItem(window.__location.host + "@" + key);
|
return store.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
case "setItem":
|
case "setItem":
|
||||||
return (key: string, value: string) => {
|
return (key: string, value: string) => {
|
||||||
target.setItem(window.__location.host + "@" + key, value);
|
store.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
case "removeItem":
|
case "removeItem":
|
||||||
return (key: string) => {
|
return (key: string) => {
|
||||||
target.removeItem(window.__location.host + "@" + key);
|
store.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
case "clear":
|
case "clear":
|
||||||
return () => {
|
return () => {
|
||||||
filterStorage(target).forEach((key) => target.removeItem(key));
|
store.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
case "key":
|
case "key":
|
||||||
return (index: number) => {
|
return (index: number) => {
|
||||||
return target[filterStorage(target)[index]];
|
store.keys()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
case "length":
|
case "length":
|
||||||
return filterStorage(target).length;
|
return store.size;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue