Skip to content

Commit

Permalink
refactor: replace addEventListener with useEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-weglarz committed Jul 21, 2024
1 parent c406865 commit 14c85cb
Showing 1 changed file with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { onMount } from "svelte";
import { browser } from "$lib/internal/utils/browser.js";
import { addEventListener } from "$lib/internal/utils/event.js";
import { IsSupported } from "$lib/utilities/index.js";
import { IsSupported, useEventListener } from "$lib/utilities/index.js";

/**
* @desc The `NetworkInformation` interface of the Network Information API
Expand Down Expand Up @@ -51,20 +50,13 @@ export class NetworkStatus {
constructor() {
onMount(() => {
this.#updateStatus();
const callbacks: VoidFunction[] = [];

if (this.#connection) {
callbacks.push(
addEventListener(this.#connection, "change", this.#updateStatus, { passive: true })
);
useEventListener(this.#connection, "change", this.#updateStatus, { passive: true });
} else {
callbacks.push(addEventListener(window, "online", this.#updateStatus, { passive: true }));
callbacks.push(addEventListener(window, "offline", this.#updateStatus, { passive: true }));
useEventListener(window, "online", this.#updateStatus, { passive: true });
useEventListener(window, "offline", this.#updateStatus, { passive: true });
}

return () => {
callbacks.forEach((c) => c());
};
});
}

Expand Down

0 comments on commit 14c85cb

Please sign in to comment.