diff --git a/src/pages/loading.astro b/src/pages/loading.astro index 042197b..abbbb10 100644 --- a/src/pages/loading.astro +++ b/src/pages/loading.astro @@ -3,11 +3,11 @@ import LoadingComponent from "@components/Loading.astro"; import Layout from "@layouts/Layout.astro"; --- - + diff --git a/src/utils/events.ts b/src/utils/events.ts index edc8b69..22e93da 100644 --- a/src/utils/events.ts +++ b/src/utils/events.ts @@ -19,15 +19,11 @@ interface Events { * "astro:before-swap": () => {}, * "astro:after-swap": () => {}, * "DOMContentLoaded": () => {} - * }, // Do note: these are optional to pass. But if you try to call the method associated with the event, it WILL throw an error. + * }, // Pass any number of these : D (they are all optional) * logging: false // Set this to true to enable logging when things go wrong. * }); * - * eventHandler.pageLoad(); // For astro:page-load - * eventHandler.beforeSwap(); // For astro:before-swap - * eventHandler.afterSwap(); // For astro:after-swap - * eventHandler.domContent(); // For DOMContentLoaded - * // NOTE: If you do not pass a function in the events: {} associated with these events, it WILL throw an error. + * eventHandler.handleEvents(); // Attaches every event you passed. */ class EventHandler { #eventItems: Events; @@ -38,24 +34,15 @@ class EventHandler { if (items.logging) return document.addEventListener(eventType, () => fn()); try { document.addEventListener(eventType, () => fn()) } catch (_) {}; } - #throwErrorOnUnspecified(fn: Event) { - throw new Error(`No function specified for ${fn}`); - } - pageLoad() { - if (!this.#eventItems.events["astro:page-load"]) return this.#throwErrorOnUnspecified("astro:page-load"); - this.#attachEvent(this.#eventItems, "astro:page-load", this.#eventItems.events["astro:page-load"]); - } - beforeSwap() { - if (!this.#eventItems.events["astro:before-swap"]) return this.#throwErrorOnUnspecified("astro:before-swap"); - this.#attachEvent(this.#eventItems, "astro:before-swap", this.#eventItems.events["astro:before-swap"]); - } - afterSwap() { - if (!this.#eventItems.events["astro:after-swap"]) return this.#throwErrorOnUnspecified("astro:after-swap"); - this.#attachEvent(this.#eventItems, "astro:after-swap", this.#eventItems.events["astro:after-swap"]); - } - domContent() { - if (!this.#eventItems.events.DOMContentLoaded) return this.#throwErrorOnUnspecified("DOMContentLoaded"); - this.#attachEvent(this.#eventItems, "DOMContentLoaded", this.#eventItems.events.DOMContentLoaded); + /** + * Attatches the events you passed when creating the class. If none are passed, an error is thrown. + */ + handleEvents() { + const events = Object.entries(this.#eventItems.events); + if (!events || events.length === 0) throw new Error('No events added!'); + events.map((event) => { + this.#attachEvent(this.#eventItems, event[0] as Event, event[1]); + }); } }