I like .bind() more

This commit is contained in:
MotorTruck1221 2025-01-04 00:04:59 -07:00
parent b34b24e783
commit 702bc64bc3
No known key found for this signature in database
GPG key ID: 08F417E2B8B61EA4
3 changed files with 49 additions and 25 deletions

View file

@ -8,7 +8,7 @@ import Layout from "@layouts/Layout.astro";
</Layout-->
<script>
import { EventHandler } from "@utils/events";
import { createBareMuxConn, createProxyScripts, SW } from "@utils/serviceWorker";
import { checkProxyScripts, createBareMuxConn, createProxyScripts, SW } from "@utils/serviceWorker";
import { navigate } from "astro:transitions/client";
function isComingFromIframe() {
try {
@ -25,24 +25,12 @@ import { createBareMuxConn, createProxyScripts, SW } from "@utils/serviceWorker"
const isIframe = isComingFromIframe();
if (!isIframe) {
console.log("Assuming request isn't coming from iframe. Redirecting...");
//navigate("/");
navigate("/");
}
}),
"DOMContentLoaded": (async () => {
for (let item of createProxyScripts()) {
document.body.appendChild(item);
}
const checkScript = setInterval(async () => {
if (typeof __uv$config !== "undefined" && typeof ScramjetController !== "undefined") {
clearInterval(checkScript);
const conn = await createBareMuxConn("/baremux/worker.js");
window.sw = new SW(conn);
}
}, 100);
})
},
logging: false
});
//Handle the events.
eHandle.handleEvents();
//Handle the events
eHandle.bind();
</script>

View file

@ -23,7 +23,7 @@ interface Events {
* logging: false // Set this to true to enable logging when things go wrong.
* });
*
* eventHandler.handleEvents(); // Attaches every event you passed.
* eventHandler.bind(); // Attaches every event you passed.
*/
class EventHandler {
#eventItems: Events;
@ -37,7 +37,7 @@ class EventHandler {
/**
* Attatches the events you passed when creating the class. If none are passed, an error is thrown.
*/
handleEvents() {
bind(): void | Error {
const events = Object.entries(this.#eventItems.events);
if (!events || events.length === 0) throw new Error('No events added!');
events.map((event) => {

View file

@ -33,6 +33,25 @@ function* createProxyScripts() {
yield sj;
};
/**
* Function that resolves ONLY when uv and Scramjet are not undefined. This prevents us from using these values before they are added and executed.
*
* @example
* await checkProxyScripts();
* @example
* checkProxyScripts().then(() => { // Do something });
*/
const checkProxyScripts = (): Promise<void> => {
return new Promise((resolve) => {
const checkScript = setInterval(() => {
if (typeof __uv$config !== "undefined" && typeof ScramjetController !== "undefined") {
clearInterval(checkScript);
resolve();
}
}, 100);
});
};
/**
* Creates a bareMux connection an returns it the instantiated instance as a promise.
*
@ -79,6 +98,7 @@ type SWInit = {
*/
class SW {
#init!: SWInit;
#ready: boolean = false;
constructor(conn: BareMuxConnection) {
const sj = (): ScramjetController => {
const sj = new ScramjetController({
@ -98,7 +118,8 @@ class SW {
(async () => await scram.init())();
navigator.serviceWorker.ready.then(async (reg) => {
console.log("Service worker ready and active!");
this.#init = { serviceWorker: reg, sj: scram, bareMuxConn: conn }
this.#init = { serviceWorker: reg, sj: scram, bareMuxConn: conn };
this.#ready = true;
});
navigator.serviceWorker.register("/sw.js", { scope: '/' });
}
@ -111,16 +132,31 @@ class SW {
* Allows you to overrid the items set. Should be used sparingly or never.
*/
setSWInfo(items: SWInit): void {
this.#init = { serviceWorker: items.serviceWorker, sj: items.sj, bareMuxConn: items.bareMuxConn }
this.#init = { serviceWorker: items.serviceWorker, sj: items.sj, bareMuxConn: items.bareMuxConn };
this.#ready = true;
}
/**
* Returns an object with the service worker, scramjet controller and baremux connection all in one method.
* Returns a promise that resolves to the serviceWorker, scramjet controller and bareMux Connection ONLY when these values are ready.
*
* @example
* const sw = new SW(conn); // "conn" must be a baremux connection that you created.
* const swInfo = await sw.getSWInfo();
*
* @example
* const sw = new SW(conn); // "conn" must be a baremux connection that you created
* sw.getInfo().then((info) => { // Do something with said info }
*/
getSWInfo(): SWInit | Error {
if (this.#init !== undefined) return this.#init;
return new Error("this object is undefined!");
getSWInfo(): Promise<SWInit> {
return new Promise((resolve) => {
const checkState = setInterval(() => {
if (this.#ready) {
clearInterval(checkState);
resolve(this.#init);
}
}, 100);
});
}
}
export { createScript, createProxyScripts, createBareMuxConn, setTransport, SW };
export { createScript, createProxyScripts, checkProxyScripts, createBareMuxConn, setTransport, SW };