@willbooster/shared-lib-react 4.0.4 → 4.0.5
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/hooks/useInterval.cjs +1 -1
- package/dist/hooks/useInterval.cjs.map +1 -1
- package/dist/hooks/useInterval.js +1 -1
- package/dist/hooks/useInterval.js.map +1 -1
- package/dist/hooks/useNoHydrationError.cjs.map +1 -1
- package/dist/hooks/useNoHydrationError.js.map +1 -1
- package/dist/hooks/useStorage.cjs +1 -1
- package/dist/hooks/useStorage.cjs.map +1 -1
- package/dist/hooks/useStorage.js +1 -1
- package/dist/hooks/useStorage.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("react");exports.useInterval=function(r,t){const s=e.useRef(void 0),u=e.useRef(r);u.current=r;const c=e.useCallback(()=>{globalThis.clearInterval(s.current)},[]);return e.useEffect(()=>(void 0!==t&&(s.current=globalThis.setInterval(()=>u.current(),t)),c),[t]),c};
|
|
1
|
+
"use strict";var e=require("react");exports.useInterval=function(r,t){const s=e.useRef(void 0),u=e.useRef(r);u.current=r;const c=e.useCallback(()=>{globalThis.clearInterval(s.current)},[]);return e.useEffect(()=>(void 0!==t&&(s.current=globalThis.setInterval(()=>{u.current()},t)),c),[c,t]),c};
|
|
2
2
|
//# sourceMappingURL=useInterval.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.
|
|
1
|
+
{"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timeout>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => {\n callbackRef.current();\n }, milliseconds);\n }\n return clearInterval;\n }, [clearInterval, milliseconds]);\n\n return clearInterval;\n}\n"],"names":["callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"wDAoBO,SAAqBA,EAAsBC,GAChD,MAAMC,EAAUC,EAAAA,YAAgCC,GAC1CC,EAAcF,EAAAA,OAAOH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,EAAAA,YAAY,KAChCC,WAAWF,cAAcL,EAAQI,UAChC,IAWH,OATAI,EAAAA,UAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,YAAY,KACvCN,EAAYC,WACXL,IAEEM,GACN,CAACA,EAAeN,IAEZM,CACT"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useRef as r,useCallback as t,useEffect as e}from"react";function n(n,o){const c=r(void 0),l=r(n);l.current=n;const a=t(()=>{globalThis.clearInterval(c.current)},[]);return e(()=>(void 0!==o&&(c.current=globalThis.setInterval(()=>l.current(),o)),a),[o]),a}export{n as useInterval};
|
|
1
|
+
import{useRef as r,useCallback as t,useEffect as e}from"react";function n(n,o){const c=r(void 0),l=r(n);l.current=n;const a=t(()=>{globalThis.clearInterval(c.current)},[]);return e(()=>(void 0!==o&&(c.current=globalThis.setInterval(()=>{l.current()},o)),a),[a,o]),a}export{n as useInterval};
|
|
2
2
|
//# sourceMappingURL=useInterval.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.
|
|
1
|
+
{"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timeout>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => {\n callbackRef.current();\n }, milliseconds);\n }\n return clearInterval;\n }, [clearInterval, milliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"+DAoBO,SAASA,EAAYC,EAAsBC,GAChD,MAAMC,EAAUC,OAAgCC,GAC1CC,EAAcF,EAAOH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,EAAY,KAChCC,WAAWF,cAAcL,EAAQI,UAChC,IAWH,OATAI,EAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,YAAY,KACvCN,EAAYC,WACXL,IAEEM,GACN,CAACA,EAAeN,IAEZM,CACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useNoHydrationError.cjs","sources":["../../src/hooks/useNoHydrationError.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\n\nfunction emptyFunction(): void {}\nfunction emptySubscribe(): () => void {\n return emptyFunction;\n}\n\n// cf. https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store\nexport function useNoHydrationError<T>(clientValue: T, ssrValue: T): T {\n return useSyncExternalStore(\n emptySubscribe,\n () => clientValue,\n () => ssrValue\n );\n}\n"],"names":["emptyFunction","emptySubscribe","clientValue","ssrValue","useSyncExternalStore"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useNoHydrationError.cjs","sources":["../../src/hooks/useNoHydrationError.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction emptyFunction(): void {}\nfunction emptySubscribe(): () => void {\n return emptyFunction;\n}\n\n// cf. https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store\nexport function useNoHydrationError<T>(clientValue: T, ssrValue: T): T {\n return useSyncExternalStore(\n emptySubscribe,\n () => clientValue,\n () => ssrValue\n );\n}\n"],"names":["emptyFunction","emptySubscribe","clientValue","ssrValue","useSyncExternalStore"],"mappings":"oCAGA,SAASA,IAAuB,CAChC,SAASC,IACP,OAAOD,CACT,6BAGO,SAAgCE,EAAgBC,GACrD,OAAOC,EAAAA,qBACLH,EACA,IAAMC,EACN,IAAMC,EAEV"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useNoHydrationError.js","sources":["../../src/hooks/useNoHydrationError.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\n\nfunction emptyFunction(): void {}\nfunction emptySubscribe(): () => void {\n return emptyFunction;\n}\n\n// cf. https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store\nexport function useNoHydrationError<T>(clientValue: T, ssrValue: T): T {\n return useSyncExternalStore(\n emptySubscribe,\n () => clientValue,\n () => ssrValue\n );\n}\n"],"names":["emptyFunction","emptySubscribe","useNoHydrationError","clientValue","ssrValue","useSyncExternalStore"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useNoHydrationError.js","sources":["../../src/hooks/useNoHydrationError.ts"],"sourcesContent":["import { useSyncExternalStore } from 'react';\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction emptyFunction(): void {}\nfunction emptySubscribe(): () => void {\n return emptyFunction;\n}\n\n// cf. https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store\nexport function useNoHydrationError<T>(clientValue: T, ssrValue: T): T {\n return useSyncExternalStore(\n emptySubscribe,\n () => clientValue,\n () => ssrValue\n );\n}\n"],"names":["emptyFunction","emptySubscribe","useNoHydrationError","clientValue","ssrValue","useSyncExternalStore"],"mappings":"6CAGA,SAASA,IAAuB,CAChC,SAASC,IACP,OAAOD,CACT,CAGO,SAASE,EAAuBC,EAAgBC,GACrD,OAAOC,EACLJ,EACA,IAAME,EACN,IAAMC,EAEV"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("react");exports.useStorage=function(t,n,s,
|
|
1
|
+
"use strict";var e=require("react");exports.useStorage=function(t,n,s,o={}){const r=e.useSyncExternalStore(e=>{const t=t=>{t.key===n&&e()};return globalThis.addEventListener("storage",t),()=>{globalThis.removeEventListener("storage",t)}},()=>window[t].getItem(n),()=>"ssrJsonText"in o&&o.ssrJsonText),i=e.useMemo(()=>{try{if(r){var e,t;const n=JSON.parse(r);return null!=(e=null==(t=o.parseAfterJsonParse)?void 0:t.call(o,n))?e:n}}catch(e){}return"function"==typeof s?s():s},[r,s]),l=e.useCallback(e=>{const s="function"==typeof e?e(i):e;let o=null;null==s?window[t].removeItem(n):(o=JSON.stringify(s),window[t].setItem(n,o)),globalThis.dispatchEvent(new StorageEvent("storage",{key:n,newValue:o}))},[n,i]);return e.useEffect(()=>{const e="function"==typeof s?s():s;null===window[t].getItem(n)&&void 0!==e&&window[t].setItem(n,JSON.stringify(e))},[n,s]),[i,l]};
|
|
2
2
|
//# sourceMappingURL=useStorage.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions
|
|
1
|
+
{"version":3,"file":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => {\n globalThis.removeEventListener('storage', newCallback);\n };\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions.parseAfterJsonParse?.(json) ?? json;\n }\n } catch {\n // do nothing\n }\n return typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n }, [jsonText, initialValueOrFunc]); // Don't add nonReactiveOptions to deps because it's not allowed to change.\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value] // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n );\n\n useEffect(() => {\n const resolvedInitialValue =\n typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n if (window[nonReactiveStorageType].getItem(key) === null && resolvedInitialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(resolvedInitialValue));\n }\n }, [key, initialValueOrFunc]); // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValueOrFunc","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","_nonReactiveOptions$p","_nonReactiveOptions$p2","json","JSON","parse","parseAfterJsonParse","call","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","resolvedInitialValue","undefined"],"mappings":"uDAUO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAAA,qBACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,KAGzB,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,KACLE,WAAWE,oBAAoB,UAAWJ,KAG9C,IAAMK,OAAOZ,GAAwBa,QAAQZ,GAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,aAE5DC,EAAQC,EAAAA,QAAQ,KACpB,IACE,GAAIZ,EAAU,CAAA,IAAAa,EAAAC,EACZ,MAAMC,EAAOC,KAAKC,MAAMjB,GACxB,OAAqD,OAArDa,EAA6C,OAA7CC,EAAOf,EAAmBmB,0BAAmB,EAAtCJ,EAAAK,KAAApB,EAAyCgB,IAAKF,EAAIE,CAC3D,CACF,CAAE,MAAAK,GACA,CAEF,MAAqC,mBAAvBtB,EAAqCA,IAAmCA,GACrF,CAACE,EAAUF,IAERuB,EAAWC,EAAAA,YACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCZ,GAASY,EAGpG,IAAIE,EAA0B,KAC1BD,QACFhB,OAAOZ,GAAwB8B,WAAW7B,IAE1C4B,EAAWT,KAAKW,UAAUH,GAC1BhB,OAAOZ,GAAwBgC,QAAQ/B,EAAK4B,IAE9CpB,WAAWwB,cAAc,IAAIC,aAAa,UAAW,CAAEjC,MAAK4B,eAG9D,CAAC5B,EAAKc,IAWR,OARAoB,EAAAA,UAAU,KACR,MAAMC,EAC0B,mBAAvBlC,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CoC,IAAzBD,GAC1DxB,OAAOZ,GAAwBgC,QAAQ/B,EAAKmB,KAAKW,UAAUK,KAE5D,CAACnC,EAAKC,IAEF,CAACa,EAAOU,EACjB"}
|
package/dist/hooks/useStorage.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useSyncExternalStore as
|
|
1
|
+
import{useSyncExternalStore as t,useMemo as e,useCallback as n,useEffect as o}from"react";function r(r,s,i,l={}){const a=t(t=>{const e=e=>{e.key===s&&t()};return globalThis.addEventListener("storage",e),()=>{globalThis.removeEventListener("storage",e)}},()=>window[r].getItem(s),()=>"ssrJsonText"in l&&l.ssrJsonText),c=e(()=>{try{if(a){var t,e;const n=JSON.parse(a);return null!=(t=null==(e=l.parseAfterJsonParse)?void 0:e.call(l,n))?t:n}}catch(t){}return"function"==typeof i?i():i},[a,i]),u=n(t=>{const e="function"==typeof t?t(c):t;let n=null;null==e?window[r].removeItem(s):(n=JSON.stringify(e),window[r].setItem(s,n)),globalThis.dispatchEvent(new StorageEvent("storage",{key:s,newValue:n}))},[s,c]);return o(()=>{const t="function"==typeof i?i():i;null===window[r].getItem(s)&&void 0!==t&&window[r].setItem(s,JSON.stringify(t))},[s,i]),[c,u]}export{r as useStorage};
|
|
2
2
|
//# sourceMappingURL=useStorage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStorage.js","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions
|
|
1
|
+
{"version":3,"file":"useStorage.js","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => {\n globalThis.removeEventListener('storage', newCallback);\n };\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions.parseAfterJsonParse?.(json) ?? json;\n }\n } catch {\n // do nothing\n }\n return typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n }, [jsonText, initialValueOrFunc]); // Don't add nonReactiveOptions to deps because it's not allowed to change.\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value] // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n );\n\n useEffect(() => {\n const resolvedInitialValue =\n typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n if (window[nonReactiveStorageType].getItem(key) === null && resolvedInitialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(resolvedInitialValue));\n }\n }, [key, initialValueOrFunc]); // Don't add nonReactiveStorageType to deps because it's not allowed to change.\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValueOrFunc","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","_nonReactiveOptions$p","_nonReactiveOptions$p2","json","JSON","parse","parseAfterJsonParse","call","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","resolvedInitialValue","undefined"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,KAGzB,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,KACLE,WAAWE,oBAAoB,UAAWJ,KAG9C,IAAMK,OAAOZ,GAAwBa,QAAQZ,GAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,aAE5DC,EAAQC,EAAQ,KACpB,IACE,GAAIZ,EAAU,CAAA,IAAAa,EAAAC,EACZ,MAAMC,EAAOC,KAAKC,MAAMjB,GACxB,OAAqD,OAArDa,EAA6C,OAA7CC,EAAOf,EAAmBmB,0BAAmB,EAAtCJ,EAAAK,KAAApB,EAAyCgB,IAAKF,EAAIE,CAC3D,CACF,CAAE,MAAAK,GACA,CAEF,MAAqC,mBAAvBtB,EAAqCA,IAAmCA,GACrF,CAACE,EAAUF,IAERuB,EAAWC,EACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCZ,GAASY,EAGpG,IAAIE,EAA0B,KAC1BD,QACFhB,OAAOZ,GAAwB8B,WAAW7B,IAE1C4B,EAAWT,KAAKW,UAAUH,GAC1BhB,OAAOZ,GAAwBgC,QAAQ/B,EAAK4B,IAE9CpB,WAAWwB,cAAc,IAAIC,aAAa,UAAW,CAAEjC,MAAK4B,eAG9D,CAAC5B,EAAKc,IAWR,OARAoB,EAAU,KACR,MAAMC,EAC0B,mBAAvBlC,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CoC,IAAzBD,GAC1DxB,OAAOZ,GAAwBgC,QAAQ/B,EAAKmB,KAAKW,UAAUK,KAE5D,CAACnC,EAAKC,IAEF,CAACa,EAAOU,EACjB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/shared-lib-react",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"author": "WillBooster Inc.",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -50,11 +50,11 @@
|
|
|
50
50
|
"@types/micromatch": "4.0.9",
|
|
51
51
|
"@types/react": "19.1.9",
|
|
52
52
|
"@types/react-dom": "19.1.7",
|
|
53
|
-
"@willbooster/eslint-config-ts-react": "11.5.
|
|
53
|
+
"@willbooster/eslint-config-ts-react": "11.5.4",
|
|
54
54
|
"@willbooster/prettier-config": "10.2.0",
|
|
55
55
|
"babel-loader": "10.0.0",
|
|
56
|
-
"build-ts": "15.0.
|
|
57
|
-
"eslint": "9.
|
|
56
|
+
"build-ts": "15.0.19",
|
|
57
|
+
"eslint": "9.33.0",
|
|
58
58
|
"eslint-config-flat-gitignore": "2.1.0",
|
|
59
59
|
"eslint-config-prettier": "10.1.8",
|
|
60
60
|
"eslint-import-resolver-typescript": "4.4.4",
|