delete useless globals

This commit is contained in:
velzie 2024-07-13 22:24:12 -04:00
parent 4ce4226afa
commit 7f0663425a
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
5 changed files with 30 additions and 31 deletions

View file

@ -15,8 +15,6 @@ import "./scope.ts";
declare global {
interface Window {
__location: Location;
__window: Window;
//@ts-ignore scope function cant be typed
__s: any;
}

View file

@ -11,24 +11,20 @@ function urlLocation() {
return loc;
}
export function LocationProxy() {
const loc = urlLocation();
const loc = urlLocation();
export const locationProxy = new Proxy(window.location, {
get(target, prop) {
return loc[prop];
},
return new Proxy(window.location, {
get(target, prop) {
return loc[prop];
},
set(obj, prop, value) {
if (prop === "href") {
location.href = encodeUrl(value);
} else {
loc[prop] = value;
}
return true;
set(obj, prop, value) {
if (prop === "href") {
location.href = encodeUrl(value);
} else {
loc[prop] = value;
}
})
}
window.__location = LocationProxy();
return true;
}
})

View file

@ -1,8 +1,11 @@
import { locationProxy } from "./location";
import { windowProxy } from "./window";
function scope(identifier: any) {
if (identifier instanceof Window) {
return window.__window;
return windowProxy;
} else if (identifier instanceof Location) {
return window.__location;
return locationProxy;
}
return identifier;

View file

@ -1,5 +1,7 @@
import IDBMapSync from "@webreflection/idb-map/sync";
const store = new IDBMapSync(window.__location.host, {
import { locationProxy } from "./location";
const store = new IDBMapSync(locationProxy.host, {
prefix: "Storage",
durability: "relaxed"
});

View file

@ -1,10 +1,12 @@
const windowProxy = new Proxy(window, {
import { locationProxy } from "./location";
export const windowProxy = new Proxy(window, {
get(target, prop) {
const propIsString = typeof prop === "string";
if (propIsString && prop === "location") {
return target.__location;
return locationProxy;
} else if (propIsString && ["window", "top", "parent", "self", "globalThis"].includes(prop)) {
return target.__window;
return windowProxy;
}
return target[prop];
@ -19,5 +21,3 @@ const windowProxy = new Proxy(window, {
return Reflect.set(target, prop, newValue);
},
});
window.__window = windowProxy;