|
| 1 | +import { browser } from "$lib/internal/utils/browser.js"; |
1 | 2 | import { addEventListener } from "$lib/internal/utils/event.js"; |
2 | 3 | import { Previous } from "$lib/utilities/index.js"; |
3 | | -import { browser } from "$app/environment"; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * @desc The `NetworkInformation` interface of the Network Information API |
@@ -86,6 +86,7 @@ interface NetworkStatus { |
86 | 86 | * @returns null if the function is not run in the browser |
87 | 87 | */ |
88 | 88 | export function useNetworkStatus() { |
| 89 | + console.log("browser", browser); |
89 | 90 | if (!browser) { |
90 | 91 | return { |
91 | 92 | get current() { |
@@ -119,18 +120,18 @@ export function useNetworkStatus() { |
119 | 120 | }; |
120 | 121 |
|
121 | 122 | $effect(() => { |
| 123 | + const callbacks: VoidFunction[] = []; |
| 124 | + |
122 | 125 | // The connection event handler also manages online and offline states. |
123 | 126 | if (connection) { |
124 | | - addEventListener(connection, "change", handleStatusChange, { passive: true }); |
| 127 | + callbacks.push(addEventListener(connection, "change", handleStatusChange, { passive: true })); |
125 | 128 | } else { |
126 | | - addEventListener(window, "online", handleStatusChange, { passive: true }); |
127 | | - addEventListener(window, "offline", handleStatusChange, { passive: true }); |
| 129 | + callbacks.push(addEventListener(window, "online", handleStatusChange, { passive: true })); |
| 130 | + callbacks.push(addEventListener(window, "offline", handleStatusChange, { passive: true })); |
128 | 131 | } |
129 | 132 |
|
130 | 133 | return () => { |
131 | | - window.removeEventListener("online", handleStatusChange); |
132 | | - window.removeEventListener("online", handleStatusChange); |
133 | | - connection?.removeEventListener("change", handleStatusChange); |
| 134 | + callbacks.forEach((c) => c()); |
134 | 135 | }; |
135 | 136 | }); |
136 | 137 |
|
|
0 commit comments