@willbooster/shared-lib-react 3.2.4 → 3.2.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.
@@ -1,2 +1,2 @@
1
- "use strict";var e=require("react");function t(e){return window.addEventListener("storage",e),()=>window.removeEventListener("storage",e)}exports.useStorage=function(n,r,s,o={}){const i=e.useSyncExternalStore(t,(()=>window[n].getItem(r)),(()=>"ssrJsonText"in o&&o.ssrJsonText)),u=e.useMemo((()=>{try{if(i){const e=JSON.parse(i);return null!=o&&o.parseAfterJsonParse?null==o?void 0:o.parseAfterJsonParse(e):e}}catch(e){}return s}),[i,s]),a=e.useCallback((e=>{try{const t="function"==typeof e?e(u):e;null==t?window[n].removeItem(r):window[n].setItem(r,JSON.stringify(t))}catch(e){console.warn(e)}}),[r,u]);return e.useEffect((()=>{null===window[n].getItem(r)&&void 0!==s&&window[n].setItem(r,JSON.stringify(s))}),[r,s]),[u,a]};
1
+ "use strict";var e=require("react");exports.useStorage=function(t,n,r,s={}){const o=e.useSyncExternalStore((e=>{const t=t=>{t.key===n&&e()};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)}),(()=>window[t].getItem(n)),(()=>"ssrJsonText"in s&&s.ssrJsonText)),i=e.useMemo((()=>{try{if(o){const e=JSON.parse(o);return null!=s&&s.parseAfterJsonParse?null==s?void 0:s.parseAfterJsonParse(e):e}}catch(e){}return r}),[o,r]),a=e.useCallback((e=>{const r="function"==typeof e?e(i):e;let s=null;null==r?window[t].removeItem(n):(s=JSON.stringify(r),window[t].setItem(n,s)),window.dispatchEvent(new StorageEvent("storage",{key:n,newValue:s}))}),[n,i]);return e.useEffect((()=>{null===window[t].getItem(n)&&void 0!==r&&window[t].setItem(n,JSON.stringify(r))}),[n,r]),[i,a]};
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 initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n subscribeStorageEvent,\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);\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n try {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(nextState));\n }\n } catch (error) {\n console.warn(error);\n }\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [key, initialValue]);\n\n return [value, setState];\n}\n\nfunction subscribeStorageEvent(callback: (this: Window, ev: StorageEvent) => unknown) {\n window.addEventListener('storage', callback);\n return () => window.removeEventListener('storage', callback);\n}\n"],"names":["subscribeStorageEvent","callback","window","addEventListener","removeEventListener","nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","removeItem","setItem","stringify","error","console","warn","useEffect","undefined"],"mappings":"oCA8DA,SAASA,EAAsBC,GAE7B,OADAC,OAAOC,iBAAiB,UAAWF,GAC5B,IAAMC,OAAOE,oBAAoB,UAAWH,EACrD,oBAvDO,SACLI,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAoBA,qBACnCV,GACA,IAAME,OAAOG,GAAwBM,QAAQL,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBI,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIL,EAAU,CACZ,MAAMM,EAAOC,KAAKC,MAAMR,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBU,oBAAsBV,MAAAA,OAAAA,EAAAA,EAAoBU,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOZ,CAAY,GAElB,CAACE,EAAUF,IAERa,EAAWC,EAAWA,aACzBC,IACC,IACE,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAEhGC,QACFrB,OAAOG,GAAwBmB,WAAWlB,GAE1CJ,OAAOG,GAAwBoB,QAAQnB,EAAKU,KAAKU,UAAUH,GAE9D,CAAC,MAAOI,GACPC,QAAQC,KAAKF,EACf,IAGF,CAACrB,EAAKO,IAUR,OAPAiB,EAAAA,WAAU,KAC4C,OAAhD5B,OAAOG,GAAwBM,QAAQL,SAAkCyB,IAAjBxB,GAC1DL,OAAOG,GAAwBoB,QAAQnB,EAAKU,KAAKU,UAAUnB,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACM,EAAOO,EACjB"}
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 initialValue: 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 window.addEventListener('storage', newCallback);\n return () => window.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);\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [jsonText, initialValue]);\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 window.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","window","addEventListener","removeEventListener","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"uDAUO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAoBA,sBAClCC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,OAAOC,iBAAiB,UAAWH,GAC5B,IAAME,OAAOE,oBAAoB,UAAWJ,EAAY,IAEjE,IAAME,OAAOT,GAAwBY,QAAQX,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBU,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIX,EAAU,CACZ,MAAMY,EAAOC,KAAKC,MAAMd,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBgB,oBAAsBhB,MAAAA,OAAAA,EAAAA,EAAoBgB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOlB,CAAY,GAElB,CAACE,EAAUF,IAERmB,EAAWC,EAAWA,aACzBC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFf,OAAOT,GAAwB0B,WAAWzB,IAE1CwB,EAAWR,KAAKU,UAAUH,GAC1Bf,OAAOT,GAAwB4B,QAAQ3B,EAAKwB,IAE9ChB,OAAOoB,cAAc,IAAIC,aAAa,UAAW,CAAE7B,MAAKwB,aAAY,GAGtE,CAACxB,EAAKa,IAUR,OAPAiB,EAAAA,WAAU,KAC4C,OAAhDtB,OAAOT,GAAwBY,QAAQX,SAAkC+B,IAAjB9B,GAC1DO,OAAOT,GAAwB4B,QAAQ3B,EAAKgB,KAAKU,UAAUzB,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACY,EAAOO,EACjB"}
@@ -1,2 +1,2 @@
1
- import{useSyncExternalStore as t,useMemo as e,useCallback as n,useEffect as r}from"react";function o(o,i,w,a={}){const c=t(s,(()=>window[o].getItem(i)),(()=>"ssrJsonText"in a&&a.ssrJsonText)),d=e((()=>{try{if(c){const t=JSON.parse(c);return null!=a&&a.parseAfterJsonParse?null==a?void 0:a.parseAfterJsonParse(t):t}}catch(t){}return w}),[c,w]),u=n((t=>{try{const e="function"==typeof t?t(d):t;null==e?window[o].removeItem(i):window[o].setItem(i,JSON.stringify(e))}catch(t){console.warn(t)}}),[i,d]);return r((()=>{null===window[o].getItem(i)&&void 0!==w&&window[o].setItem(i,JSON.stringify(w))}),[i,w]),[d,u]}function s(t){return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)}export{o as useStorage};
1
+ import{useSyncExternalStore as e,useMemo as t,useCallback as n,useEffect as r}from"react";function o(o,s,i,w={}){const a=e((e=>{const t=t=>{t.key===s&&e()};return window.addEventListener("storage",t),()=>window.removeEventListener("storage",t)}),(()=>window[o].getItem(s)),(()=>"ssrJsonText"in w&&w.ssrJsonText)),d=t((()=>{try{if(a){const e=JSON.parse(a);return null!=w&&w.parseAfterJsonParse?null==w?void 0:w.parseAfterJsonParse(e):e}}catch(e){}return i}),[a,i]),l=n((e=>{const t="function"==typeof e?e(d):e;let n=null;null==t?window[o].removeItem(s):(n=JSON.stringify(t),window[o].setItem(s,n)),window.dispatchEvent(new StorageEvent("storage",{key:s,newValue:n}))}),[s,d]);return r((()=>{null===window[o].getItem(s)&&void 0!==i&&window[o].setItem(s,JSON.stringify(i))}),[s,i]),[d,l]}export{o 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 initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n subscribeStorageEvent,\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);\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n try {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(nextState));\n }\n } catch (error) {\n console.warn(error);\n }\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [key, initialValue]);\n\n return [value, setState];\n}\n\nfunction subscribeStorageEvent(callback: (this: Window, ev: StorageEvent) => unknown) {\n window.addEventListener('storage', callback);\n return () => window.removeEventListener('storage', callback);\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","subscribeStorageEvent","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","removeItem","setItem","stringify","error","console","warn","useEffect","undefined","callback","addEventListener","removeEventListener"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EACfC,GACA,IAAMC,OAAOP,GAAwBQ,QAAQP,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBM,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIP,EAAU,CACZ,MAAMQ,EAAOC,KAAKC,MAAMV,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBY,oBAAsBZ,MAAAA,OAAAA,EAAAA,EAAoBY,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOd,CAAY,GAElB,CAACE,EAAUF,IAERe,EAAWC,GACdC,IACC,IACE,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAEhGC,QACFb,OAAOP,GAAwBqB,WAAWpB,GAE1CM,OAAOP,GAAwBsB,QAAQrB,EAAKY,KAAKU,UAAUH,GAE9D,CAAC,MAAOI,GACPC,QAAQC,KAAKF,EACf,IAGF,CAACvB,EAAKS,IAUR,OAPAiB,GAAU,KAC4C,OAAhDpB,OAAOP,GAAwBQ,QAAQP,SAAkC2B,IAAjB1B,GAC1DK,OAAOP,GAAwBsB,QAAQrB,EAAKY,KAAKU,UAAUrB,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACQ,EAAOO,EACjB,CAEA,SAASX,EAAsBuB,GAE7B,OADAtB,OAAOuB,iBAAiB,UAAWD,GAC5B,IAAMtB,OAAOwB,oBAAoB,UAAWF,EACrD"}
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 initialValue: 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 window.addEventListener('storage', newCallback);\n return () => window.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);\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [jsonText, initialValue]);\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 window.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","window","addEventListener","removeEventListener","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,GACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,OAAOC,iBAAiB,UAAWH,GAC5B,IAAME,OAAOE,oBAAoB,UAAWJ,EAAY,IAEjE,IAAME,OAAOT,GAAwBY,QAAQX,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBU,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIX,EAAU,CACZ,MAAMY,EAAOC,KAAKC,MAAMd,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBgB,oBAAsBhB,MAAAA,OAAAA,EAAAA,EAAoBgB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOlB,CAAY,GAElB,CAACE,EAAUF,IAERmB,EAAWC,GACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFf,OAAOT,GAAwB0B,WAAWzB,IAE1CwB,EAAWR,KAAKU,UAAUH,GAC1Bf,OAAOT,GAAwB4B,QAAQ3B,EAAKwB,IAE9ChB,OAAOoB,cAAc,IAAIC,aAAa,UAAW,CAAE7B,MAAKwB,aAAY,GAGtE,CAACxB,EAAKa,IAUR,OAPAiB,GAAU,KAC4C,OAAhDtB,OAAOT,GAAwBY,QAAQX,SAAkC+B,IAAjB9B,GAC1DO,OAAOT,GAAwB4B,QAAQ3B,EAAKgB,KAAKU,UAAUzB,GAC7D,GAEC,CAACD,EAAKC,IAEF,CAACY,EAAOO,EACjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/shared-lib-react",
3
- "version": "3.2.4",
3
+ "version": "3.2.5",
4
4
  "license": "Apache-2.0",
5
5
  "author": "WillBooster Inc.",
6
6
  "sideEffects": false,