@1hook/use-is-online 0.1.11 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,33 +1,28 @@
1
- import React from 'react';
2
- import { isServer } from '@1hook/utils/is-server';
1
+ import { useSyncExternalStore } from 'react';
3
2
 
4
3
  // src/index.ts
5
- var listeners = /* @__PURE__ */ new Set();
6
- if (!isServer) {
7
- window.addEventListener(
8
- "online",
9
- () => {
10
- listeners.forEach((l) => l(true));
11
- },
12
- { passive: true }
13
- );
14
- window.addEventListener(
15
- "offline",
16
- () => {
17
- listeners.forEach((l) => l(false));
18
- },
19
- { passive: true }
20
- );
4
+ var allListeners = /* @__PURE__ */ new Set();
5
+ var callAllListeners = () => allListeners.forEach((l) => l());
6
+ function subscribe(getSnapshot) {
7
+ allListeners.add(getSnapshot);
8
+ if (allListeners.size === 1) {
9
+ window.addEventListener("online", callAllListeners, { passive: true });
10
+ window.addEventListener("offline", callAllListeners, { passive: true });
11
+ }
12
+ return () => {
13
+ allListeners.delete(getSnapshot);
14
+ if (allListeners.size === 0) {
15
+ window.removeEventListener("online", callAllListeners);
16
+ window.removeEventListener("offline", callAllListeners);
17
+ }
18
+ };
21
19
  }
22
20
  function useIsOnline() {
23
- const [isOnline, setIsOnline] = React.useState(isServer || navigator.onLine);
24
- React.useEffect(() => {
25
- listeners.add(setIsOnline);
26
- return () => {
27
- listeners.delete(setIsOnline);
28
- };
29
- }, []);
30
- return isOnline;
21
+ return useSyncExternalStore(
22
+ subscribe,
23
+ () => navigator.onLine,
24
+ () => true
25
+ );
31
26
  }
32
27
 
33
28
  export { useIsOnline };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAGA,IAAM,SAAA,uBAAgB,GAA+B,EAAA;AAErD,IAAI,CAAC,QAAU,EAAA;AACb,EAAO,MAAA,CAAA,gBAAA;AAAA,IACL,QAAA;AAAA,IACA,MAAM;AACJ,MAAA,SAAA,CAAU,OAAQ,CAAA,CAAC,CAAM,KAAA,CAAA,CAAE,IAAI,CAAC,CAAA;AAAA,KAClC;AAAA,IACA,EAAE,SAAS,IAAK;AAAA,GAClB;AACA,EAAO,MAAA,CAAA,gBAAA;AAAA,IACL,SAAA;AAAA,IACA,MAAM;AACJ,MAAA,SAAA,CAAU,OAAQ,CAAA,CAAC,CAAM,KAAA,CAAA,CAAE,KAAK,CAAC,CAAA;AAAA,KACnC;AAAA,IACA,EAAE,SAAS,IAAK;AAAA,GAClB;AACF;AAEO,SAAS,WAAc,GAAA;AAC5B,EAAM,MAAA,CAAC,UAAU,WAAW,CAAA,GAAI,MAAM,QAAS,CAAA,QAAA,IAAY,UAAU,MAAM,CAAA;AAE3E,EAAA,KAAA,CAAM,UAAU,MAAM;AACpB,IAAA,SAAA,CAAU,IAAI,WAAW,CAAA;AACzB,IAAA,OAAO,MAAM;AACX,MAAA,SAAA,CAAU,OAAO,WAAW,CAAA;AAAA,KAC9B;AAAA,GACF,EAAG,EAAE,CAAA;AAEL,EAAO,OAAA,QAAA;AACT","file":"index.js","sourcesContent":["import React from 'react'\nimport { isServer } from '@1hook/utils/is-server'\n\nconst listeners = new Set<(online: boolean) => void>()\n\nif (!isServer) {\n window.addEventListener(\n 'online',\n () => {\n listeners.forEach((l) => l(true))\n },\n { passive: true },\n )\n window.addEventListener(\n 'offline',\n () => {\n listeners.forEach((l) => l(false))\n },\n { passive: true },\n )\n}\n\nexport function useIsOnline() {\n const [isOnline, setIsOnline] = React.useState(isServer || navigator.onLine)\n\n React.useEffect(() => {\n listeners.add(setIsOnline)\n return () => {\n listeners.delete(setIsOnline)\n }\n }, [])\n\n return isOnline\n}\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAEA,IAAM,YAAA,uBAAmB,GAAgB,EAAA;AACzC,IAAM,mBAAmB,MAAM,YAAA,CAAa,QAAQ,CAAC,CAAA,KAAM,GAAG,CAAA;AAE9D,SAAS,UAAU,WAAyB,EAAA;AAC1C,EAAA,YAAA,CAAa,IAAI,WAAW,CAAA;AAC5B,EAAI,IAAA,YAAA,CAAa,SAAS,CAAG,EAAA;AAC3B,IAAA,MAAA,CAAO,iBAAiB,QAAU,EAAA,gBAAA,EAAkB,EAAE,OAAA,EAAS,MAAM,CAAA;AACrE,IAAA,MAAA,CAAO,iBAAiB,SAAW,EAAA,gBAAA,EAAkB,EAAE,OAAA,EAAS,MAAM,CAAA;AAAA;AAExE,EAAA,OAAO,MAAM;AACX,IAAA,YAAA,CAAa,OAAO,WAAW,CAAA;AAC/B,IAAI,IAAA,YAAA,CAAa,SAAS,CAAG,EAAA;AAC3B,MAAO,MAAA,CAAA,mBAAA,CAAoB,UAAU,gBAAgB,CAAA;AACrD,MAAO,MAAA,CAAA,mBAAA,CAAoB,WAAW,gBAAgB,CAAA;AAAA;AACxD,GACF;AACF;AAEO,SAAS,WAAc,GAAA;AAC5B,EAAO,OAAA,oBAAA;AAAA,IACL,SAAA;AAAA,IACA,MAAM,SAAU,CAAA,MAAA;AAAA,IAChB,MAAM;AAAA,GACR;AACF","file":"index.js","sourcesContent":["import { useSyncExternalStore } from 'react'\n\nconst allListeners = new Set<() => void>()\nconst callAllListeners = () => allListeners.forEach((l) => l())\n\nfunction subscribe(getSnapshot: () => void) {\n allListeners.add(getSnapshot)\n if (allListeners.size === 1) {\n window.addEventListener('online', callAllListeners, { passive: true })\n window.addEventListener('offline', callAllListeners, { passive: true })\n }\n return () => {\n allListeners.delete(getSnapshot)\n if (allListeners.size === 0) {\n window.removeEventListener('online', callAllListeners)\n window.removeEventListener('offline', callAllListeners)\n }\n }\n}\n\nexport function useIsOnline() {\n return useSyncExternalStore(\n subscribe,\n () => navigator.onLine,\n () => true,\n )\n}\n"]}
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1hook/use-is-online",
3
- "version": "0.1.11",
3
+ "version": "0.2.0",
4
4
  "files": [
5
5
  "dist",
6
6
  "README.md"
@@ -20,7 +20,7 @@
20
20
  "react-dom": ">=18.0.0 || >=19.0.0"
21
21
  },
22
22
  "dependencies": {
23
- "@1hook/utils": "0.1.11"
23
+ "@1hook/utils": "0.2.0"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",