From 702bc64bc326e01639f4dd7d7ed38444dffd83e4 Mon Sep 17 00:00:00 2001 From: MotorTruck1221 Date: Sat, 4 Jan 2025 00:04:59 -0700 Subject: [PATCH] I like .bind() more --- src/pages/loading.astro | 20 +++------------ src/utils/events.ts | 4 +-- src/utils/serviceWorker.ts | 50 ++++++++++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/pages/loading.astro b/src/pages/loading.astro index 0fbb78a..6253859 100644 --- a/src/pages/loading.astro +++ b/src/pages/loading.astro @@ -8,7 +8,7 @@ import Layout from "@layouts/Layout.astro"; diff --git a/src/utils/events.ts b/src/utils/events.ts index 22e93da..6c61648 100644 --- a/src/utils/events.ts +++ b/src/utils/events.ts @@ -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) => { diff --git a/src/utils/serviceWorker.ts b/src/utils/serviceWorker.ts index e2b0023..ed6c9f2 100644 --- a/src/utils/serviceWorker.ts +++ b/src/utils/serviceWorker.ts @@ -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 => { + 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 { + 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 };