@aigamo/route-sphere 2.2.11 → 2.2.13
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.cjs.js +2 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +42 -62
- package/dist/index.es.js.map +1 -1
- package/dist/react-router.cjs.js +2 -2
- package/dist/react-router.cjs.js.map +1 -1
- package/dist/react-router.es.js +40 -56
- package/dist/react-router.es.js.map +1 -1
- package/dist/tanstack-router.cjs.js +2 -2
- package/dist/tanstack-router.cjs.js.map +1 -1
- package/dist/tanstack-router.es.js +40 -58
- package/dist/tanstack-router.es.js.map +1 -1
- package/dist/useStateHandler-cGvOZfIR.js +51 -0
- package/dist/useStateHandler-cGvOZfIR.js.map +1 -0
- package/dist/useStateHandler-lwgfPmpv.cjs +2 -0
- package/dist/useStateHandler-lwgfPmpv.cjs.map +1 -0
- package/package.json +4 -4
- package/dist/useStateHandler-Dbkejxjm.js +0 -41
- package/dist/useStateHandler-Dbkejxjm.js.map +0 -1
- package/dist/useStateHandler-Ff_CnvN8.cjs +0 -2
- package/dist/useStateHandler-Ff_CnvN8.cjs.map +0 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=index.cjs.js.map
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./useStateHandler-lwgfPmpv.cjs`);let t=require(`react`),n=require(`react-router-dom`);var r=()=>((0,t.useLayoutEffect)(()=>{window.scrollTo(0,0)},[(0,n.useLocation)()]),null),i=e=>(0,t.useCallback)(()=>{try{return JSON.parse(window.localStorage.getItem(e)??JSON.stringify({}))}catch(e){return console.error(e),{}}},[e]),a=e=>(0,t.useCallback)(t=>{window.localStorage.setItem(e,JSON.stringify(t))},[e]),o=e=>{let n=i(e),r=a(e);return(0,t.useMemo)(()=>({deserialize:n,serialize:r}),[n,r])},s=(t,n,r,i)=>{e.t(o(t),n,r,i)},c=e=>(0,t.useCallback)(()=>e.state,[e]),l=e=>(0,t.useCallback)(t=>{e.state=t},[e]),u=e=>{let n=c(e),r=l(e);return(0,t.useMemo)(()=>({get:n,set:r}),[n,r])},d=(e,n)=>{let r=u(n),i=(0,t.useMemo)(()=>({onStateRestore:n.onStateRestore,onStateChange:n.onStateChange,onStateSave:n.onStateSave}),[n]);s(e,n.validateState,r,i)},f=(e,t)=>t.some(t=>e.includes(t));exports.ScrollToTop=r,exports.includesAny=f,exports.useLocalStorageState=d,exports.useStateHandler=e.t;
|
|
2
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/components/ScrollToTop.tsx","../src/components/useLocalStorageState.tsx","../src/helpers/includesAny.ts"],"sourcesContent":["import { useLayoutEffect } from 'react';\nimport { useLocation } from 'react-router-dom';\n\nexport const ScrollToTop = (): null => {\n\tconst location = useLocation();\n\n\tuseLayoutEffect(() => {\n\t\twindow.scrollTo(0, 0);\n\t}, [location]);\n\n\treturn null;\n};\n","import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocalStorageStateDeserializer = (key: string): (() => unknown) => {\n\treturn useCallback((): unknown => {\n\t\ttry {\n\t\t\treturn JSON.parse(\n\t\t\t\twindow.localStorage.getItem(key) ?? JSON.stringify({}),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconsole.error(error);\n\t\t\treturn {};\n\t\t}\n\t}, [key]);\n};\n\nconst useLocalStorageStateSerializer = <TState,>(\n\tkey: string,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\twindow.localStorage.setItem(key, JSON.stringify(state));\n\t\t},\n\t\t[key],\n\t);\n};\n\nconst useLocalStorageStateCodec = <TState,>(\n\tkey: string,\n): IStateCodec<TState> => {\n\tconst deserializer = useLocalStorageStateDeserializer(key);\n\tconst serializer = useLocalStorageStateSerializer(key);\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\nconst useLocalStorageStateHandler = <TState,>(\n\tkey: string,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocalStorageStateCodec(key);\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocalStorageStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocalStorageStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocalStorageStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocalStorageStateGetter(store);\n\tconst setter = useLocalStorageStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\nexport const useLocalStorageState = <TState,>(\n\tkey: string,\n\tstore: IStateStore<TState>,\n): void => {\n\tconst accessor = useLocalStorageStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocalStorageStateHandler(key, store.validateState, accessor, options);\n};\n","export const includesAny = <T>(array: T[], values: T[]): boolean => {\n\treturn values.some((value) => array.includes(value));\n};\n"],"
|
|
1
|
+
{"version":3,"file":"index.cjs.js","names":[],"sources":["../src/components/ScrollToTop.tsx","../src/components/useLocalStorageState.tsx","../src/helpers/includesAny.ts"],"sourcesContent":["import { useLayoutEffect } from 'react';\nimport { useLocation } from 'react-router-dom';\n\nexport const ScrollToTop = (): null => {\n\tconst location = useLocation();\n\n\tuseLayoutEffect(() => {\n\t\twindow.scrollTo(0, 0);\n\t}, [location]);\n\n\treturn null;\n};\n","import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocalStorageStateDeserializer = (key: string): (() => unknown) => {\n\treturn useCallback((): unknown => {\n\t\ttry {\n\t\t\treturn JSON.parse(\n\t\t\t\twindow.localStorage.getItem(key) ?? JSON.stringify({}),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconsole.error(error);\n\t\t\treturn {};\n\t\t}\n\t}, [key]);\n};\n\nconst useLocalStorageStateSerializer = <TState,>(\n\tkey: string,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\twindow.localStorage.setItem(key, JSON.stringify(state));\n\t\t},\n\t\t[key],\n\t);\n};\n\nconst useLocalStorageStateCodec = <TState,>(\n\tkey: string,\n): IStateCodec<TState> => {\n\tconst deserializer = useLocalStorageStateDeserializer(key);\n\tconst serializer = useLocalStorageStateSerializer(key);\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\nconst useLocalStorageStateHandler = <TState,>(\n\tkey: string,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocalStorageStateCodec(key);\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocalStorageStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocalStorageStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocalStorageStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocalStorageStateGetter(store);\n\tconst setter = useLocalStorageStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\nexport const useLocalStorageState = <TState,>(\n\tkey: string,\n\tstore: IStateStore<TState>,\n): void => {\n\tconst accessor = useLocalStorageStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocalStorageStateHandler(key, store.validateState, accessor, options);\n};\n","export const includesAny = <T>(array: T[], values: T[]): boolean => {\n\treturn values.some((value) => array.includes(value));\n};\n"],"mappings":"0KAGA,IAAa,QAGZ,EAAA,EAAA,qBAAsB,CACrB,OAAO,SAAS,EAAG,EAAE,EACnB,EAAA,EAAA,EAAA,cAJ2B,CAIjB,CAAC,CAEP,MCHF,EAAoC,IACzC,EAAA,EAAA,iBAAkC,CACjC,GAAI,CACH,OAAO,KAAK,MACX,OAAO,aAAa,QAAQ,EAAI,EAAI,KAAK,UAAU,EAAE,CAAC,CACtD,OACO,EAAO,CAEf,OADA,QAAQ,MAAM,EAAM,CACb,EAAE,GAER,CAAC,EAAI,CAAC,CAGJ,EACL,IAEA,EAAA,EAAA,aACE,GAAwB,CACxB,OAAO,aAAa,QAAQ,EAAK,KAAK,UAAU,EAAM,CAAC,EAExD,CAAC,EAAI,CACL,CAGI,EACL,GACyB,CACzB,IAAM,EAAe,EAAiC,EAAI,CACpD,EAAa,EAA+B,EAAI,CAQtD,OAAA,EAAA,EAAA,cAN6B,CAC3B,YAAa,EACb,UAAW,EACX,EACD,CAAC,EAAc,EAAW,CAC1B,EAII,GACL,EACA,EACA,EACA,IACU,CAEV,EAAA,EADc,EAA0B,EAAI,CACrB,EAAW,EAAU,EAAQ,EAG/C,EACL,IAEA,EAAA,EAAA,iBAAiC,EAAM,MAAO,CAAC,EAAM,CAAC,CAGjD,EACL,IAEA,EAAA,EAAA,aACE,GAAwB,CACxB,EAAM,MAAQ,GAEf,CAAC,EAAM,CACP,CAGI,EACL,GAC4B,CAC5B,IAAM,EAAS,EAA2B,EAAM,CAC1C,EAAS,EAA2B,EAAM,CAKhD,OAAA,EAAA,EAAA,cAHgC,CAAE,IAAK,EAAQ,IAAK,EAAQ,EAC3D,CAAC,EAAQ,EAAO,CAChB,EAIW,GACZ,EACA,IACU,CACV,IAAM,EAAW,EAA6B,EAAM,CAC9C,GAAA,EAAA,EAAA,cACE,CACN,eAAgB,EAAM,eACtB,cAAe,EAAM,cACrB,YAAa,EAAM,YACnB,EACD,CAAC,EAAM,CACP,CACD,EAA4B,EAAK,EAAM,cAAe,EAAU,EAAQ,EClG5D,GAAkB,EAAY,IACnC,EAAO,KAAM,GAAU,EAAM,SAAS,EAAM,CAAC"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,62 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
),
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
[t, o]
|
|
44
|
-
);
|
|
45
|
-
}, h = (e, t) => {
|
|
46
|
-
const o = f(t), a = c(
|
|
47
|
-
() => ({
|
|
48
|
-
onStateRestore: t.onStateRestore,
|
|
49
|
-
onStateChange: t.onStateChange,
|
|
50
|
-
onStateSave: t.onStateSave
|
|
51
|
-
}),
|
|
52
|
-
[t]
|
|
53
|
-
);
|
|
54
|
-
d(e, t.validateState, o, a);
|
|
55
|
-
}, v = (e, t) => t.some((o) => e.includes(o));
|
|
56
|
-
export {
|
|
57
|
-
C as ScrollToTop,
|
|
58
|
-
v as includesAny,
|
|
59
|
-
h as useLocalStorageState,
|
|
60
|
-
l as useStateHandler
|
|
61
|
-
};
|
|
62
|
-
//# sourceMappingURL=index.es.js.map
|
|
1
|
+
import { t as e } from "./useStateHandler-cGvOZfIR.js";
|
|
2
|
+
import { useCallback as t, useLayoutEffect as n, useMemo as r } from "react";
|
|
3
|
+
import { useLocation as i } from "react-router-dom";
|
|
4
|
+
//#region src/components/ScrollToTop.tsx
|
|
5
|
+
var a = () => (n(() => {
|
|
6
|
+
window.scrollTo(0, 0);
|
|
7
|
+
}, [i()]), null), o = (e) => t(() => {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(window.localStorage.getItem(e) ?? JSON.stringify({}));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
return console.error(e), {};
|
|
12
|
+
}
|
|
13
|
+
}, [e]), s = (e) => t((t) => {
|
|
14
|
+
window.localStorage.setItem(e, JSON.stringify(t));
|
|
15
|
+
}, [e]), c = (e) => {
|
|
16
|
+
let t = o(e), n = s(e);
|
|
17
|
+
return r(() => ({
|
|
18
|
+
deserialize: t,
|
|
19
|
+
serialize: n
|
|
20
|
+
}), [t, n]);
|
|
21
|
+
}, l = (t, n, r, i) => {
|
|
22
|
+
e(c(t), n, r, i);
|
|
23
|
+
}, u = (e) => t(() => e.state, [e]), d = (e) => t((t) => {
|
|
24
|
+
e.state = t;
|
|
25
|
+
}, [e]), f = (e) => {
|
|
26
|
+
let t = u(e), n = d(e);
|
|
27
|
+
return r(() => ({
|
|
28
|
+
get: t,
|
|
29
|
+
set: n
|
|
30
|
+
}), [t, n]);
|
|
31
|
+
}, p = (e, t) => {
|
|
32
|
+
let n = f(t), i = r(() => ({
|
|
33
|
+
onStateRestore: t.onStateRestore,
|
|
34
|
+
onStateChange: t.onStateChange,
|
|
35
|
+
onStateSave: t.onStateSave
|
|
36
|
+
}), [t]);
|
|
37
|
+
l(e, t.validateState, n, i);
|
|
38
|
+
}, m = (e, t) => t.some((t) => e.includes(t));
|
|
39
|
+
//#endregion
|
|
40
|
+
export { a as ScrollToTop, m as includesAny, p as useLocalStorageState, e as useStateHandler };
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/components/ScrollToTop.tsx","../src/components/useLocalStorageState.tsx","../src/helpers/includesAny.ts"],"sourcesContent":["import { useLayoutEffect } from 'react';\nimport { useLocation } from 'react-router-dom';\n\nexport const ScrollToTop = (): null => {\n\tconst location = useLocation();\n\n\tuseLayoutEffect(() => {\n\t\twindow.scrollTo(0, 0);\n\t}, [location]);\n\n\treturn null;\n};\n","import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocalStorageStateDeserializer = (key: string): (() => unknown) => {\n\treturn useCallback((): unknown => {\n\t\ttry {\n\t\t\treturn JSON.parse(\n\t\t\t\twindow.localStorage.getItem(key) ?? JSON.stringify({}),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconsole.error(error);\n\t\t\treturn {};\n\t\t}\n\t}, [key]);\n};\n\nconst useLocalStorageStateSerializer = <TState,>(\n\tkey: string,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\twindow.localStorage.setItem(key, JSON.stringify(state));\n\t\t},\n\t\t[key],\n\t);\n};\n\nconst useLocalStorageStateCodec = <TState,>(\n\tkey: string,\n): IStateCodec<TState> => {\n\tconst deserializer = useLocalStorageStateDeserializer(key);\n\tconst serializer = useLocalStorageStateSerializer(key);\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\nconst useLocalStorageStateHandler = <TState,>(\n\tkey: string,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocalStorageStateCodec(key);\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocalStorageStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocalStorageStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocalStorageStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocalStorageStateGetter(store);\n\tconst setter = useLocalStorageStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\nexport const useLocalStorageState = <TState,>(\n\tkey: string,\n\tstore: IStateStore<TState>,\n): void => {\n\tconst accessor = useLocalStorageStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocalStorageStateHandler(key, store.validateState, accessor, options);\n};\n","export const includesAny = <T>(array: T[], values: T[]): boolean => {\n\treturn values.some((value) => array.includes(value));\n};\n"],"
|
|
1
|
+
{"version":3,"file":"index.es.js","names":[],"sources":["../src/components/ScrollToTop.tsx","../src/components/useLocalStorageState.tsx","../src/helpers/includesAny.ts"],"sourcesContent":["import { useLayoutEffect } from 'react';\nimport { useLocation } from 'react-router-dom';\n\nexport const ScrollToTop = (): null => {\n\tconst location = useLocation();\n\n\tuseLayoutEffect(() => {\n\t\twindow.scrollTo(0, 0);\n\t}, [location]);\n\n\treturn null;\n};\n","import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocalStorageStateDeserializer = (key: string): (() => unknown) => {\n\treturn useCallback((): unknown => {\n\t\ttry {\n\t\t\treturn JSON.parse(\n\t\t\t\twindow.localStorage.getItem(key) ?? JSON.stringify({}),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconsole.error(error);\n\t\t\treturn {};\n\t\t}\n\t}, [key]);\n};\n\nconst useLocalStorageStateSerializer = <TState,>(\n\tkey: string,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\twindow.localStorage.setItem(key, JSON.stringify(state));\n\t\t},\n\t\t[key],\n\t);\n};\n\nconst useLocalStorageStateCodec = <TState,>(\n\tkey: string,\n): IStateCodec<TState> => {\n\tconst deserializer = useLocalStorageStateDeserializer(key);\n\tconst serializer = useLocalStorageStateSerializer(key);\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\nconst useLocalStorageStateHandler = <TState,>(\n\tkey: string,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocalStorageStateCodec(key);\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocalStorageStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocalStorageStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocalStorageStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocalStorageStateGetter(store);\n\tconst setter = useLocalStorageStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\nexport const useLocalStorageState = <TState,>(\n\tkey: string,\n\tstore: IStateStore<TState>,\n): void => {\n\tconst accessor = useLocalStorageStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocalStorageStateHandler(key, store.validateState, accessor, options);\n};\n","export const includesAny = <T>(array: T[], values: T[]): boolean => {\n\treturn values.some((value) => array.includes(value));\n};\n"],"mappings":";;;;AAGA,IAAa,WAGZ,QAAsB;AACrB,QAAO,SAAS,GAAG,EAAE;GACnB,CAJc,GAAa,CAIjB,CAAC,EAEP,OCHF,KAAoC,MAClC,QAA2B;AACjC,KAAI;AACH,SAAO,KAAK,MACX,OAAO,aAAa,QAAQ,EAAI,IAAI,KAAK,UAAU,EAAE,CAAC,CACtD;UACO,GAAO;AAEf,SADA,QAAQ,MAAM,EAAM,EACb,EAAE;;GAER,CAAC,EAAI,CAAC,EAGJ,KACL,MAEO,GACL,MAAwB;AACxB,QAAO,aAAa,QAAQ,GAAK,KAAK,UAAU,EAAM,CAAC;GAExD,CAAC,EAAI,CACL,EAGI,KACL,MACyB;CACzB,IAAM,IAAe,EAAiC,EAAI,EACpD,IAAa,EAA+B,EAAI;AAQtD,QAPc,SACe;EAC3B,aAAa;EACb,WAAW;EACX,GACD,CAAC,GAAc,EAAW,CAC1B;GAII,KACL,GACA,GACA,GACA,MACU;AAEV,GADc,EAA0B,EAAI,EACrB,GAAW,GAAU,EAAQ;GAG/C,KACL,MAEO,QAA0B,EAAM,OAAO,CAAC,EAAM,CAAC,EAGjD,KACL,MAEO,GACL,MAAwB;AACxB,GAAM,QAAQ;GAEf,CAAC,EAAM,CACP,EAGI,KACL,MAC4B;CAC5B,IAAM,IAAS,EAA2B,EAAM,EAC1C,IAAS,EAA2B,EAAM;AAKhD,QAJiB,SACe;EAAE,KAAK;EAAQ,KAAK;EAAQ,GAC3D,CAAC,GAAQ,EAAO,CAChB;GAIW,KACZ,GACA,MACU;CACV,IAAM,IAAW,EAA6B,EAAM,EAC9C,IAAU,SACR;EACN,gBAAgB,EAAM;EACtB,eAAe,EAAM;EACrB,aAAa,EAAM;EACnB,GACD,CAAC,EAAM,CACP;AACD,GAA4B,GAAK,EAAM,eAAe,GAAU,EAAQ;GClG5D,KAAkB,GAAY,MACnC,EAAO,MAAM,MAAU,EAAM,SAAS,EAAM,CAAC"}
|
package/dist/react-router.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=react-router.cjs.js.map
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./useStateHandler-lwgfPmpv.cjs`);let t=require(`react`),n=require(`react-router-dom`),r=require(`qs`);var i=()=>{let e=(0,n.useLocation)();return(0,t.useCallback)(()=>(0,r.parse)(e.search.slice(1)),[e])},a=()=>{let e=(0,n.useNavigate)();return(0,t.useCallback)(async t=>{await e(`?${(0,r.stringify)(t)}`)},[e])},o=()=>{let e=i(),n=a();return(0,t.useMemo)(()=>({deserialize:e,serialize:n}),[e,n])},s=(t,n,r)=>{e.t(o(),t,n,r)},c=e=>(0,t.useCallback)(()=>e.state,[e]),l=e=>(0,t.useCallback)(t=>{e.state=t},[e]),u=e=>{let n=c(e),r=l(e);return(0,t.useMemo)(()=>({get:n,set:r}),[n,r])},d=e=>{let n=u(e),r=(0,t.useMemo)(()=>({onStateRestore:e.onStateRestore,onStateChange:e.onStateChange,onStateSave:e.onStateSave}),[e]);s(e.validateState,n,r)};exports.useLocationState=d;
|
|
2
|
+
//# sourceMappingURL=react-router.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-router.cjs.js","sources":["../src/react-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { type ParsedQs, parse, stringify } from 'qs';\nimport { useCallback, useMemo } from 'react';\nimport { useLocation, useNavigate } from 'react-router-dom';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.search.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tconst newUrl = `?${stringify(state)}`;\n\t\t\tawait navigate(newUrl);\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"
|
|
1
|
+
{"version":3,"file":"react-router.cjs.js","names":[],"sources":["../src/react-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { type ParsedQs, parse, stringify } from 'qs';\nimport { useCallback, useMemo } from 'react';\nimport { useLocation, useNavigate } from 'react-router-dom';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.search.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tconst newUrl = `?${stringify(state)}`;\n\t\t\tawait navigate(newUrl);\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"mappings":"0LASA,IAAM,MAAuD,CAC5D,IAAM,GAAA,EAAA,EAAA,cAAwB,CAG9B,OAAA,EAAA,EAAA,kBAAA,EAAA,EAAA,OACuB,EAAS,OAAO,MAAM,EAAE,CAAC,CAC/C,CAAC,EAAS,CACV,EAGI,MAAuE,CAC5E,IAAM,GAAA,EAAA,EAAA,cAAwB,CAE9B,OAAA,EAAA,EAAA,aACC,KAAO,IAAiC,CAEvC,MAAM,EADS,KAAA,EAAA,EAAA,WAAc,EAAM,GACb,EAEvB,CAAC,EAAS,CACV,EAGI,MAA4D,CACjE,IAAM,EAAe,GAA8B,CAC7C,EAAa,GAA4B,CAQ/C,OAAA,EAAA,EAAA,cAN6B,CAC3B,YAAa,EACb,UAAW,EACX,EACD,CAAC,EAAc,EAAW,CAC1B,EAKI,GACL,EACA,EACA,IACU,CAEV,EAAA,EADc,GAAuB,CACd,EAAW,EAAU,EAAQ,EAG/C,EACL,IAEA,EAAA,EAAA,iBAAiC,EAAM,MAAO,CAAC,EAAM,CAAC,CAGjD,EACL,IAEA,EAAA,EAAA,aACE,GAAwB,CACxB,EAAM,MAAQ,GAEf,CAAC,EAAM,CACP,CAGI,EACL,GAC4B,CAC5B,IAAM,EAAS,EAAuB,EAAM,CACtC,EAAS,EAAuB,EAAM,CAK5C,OAAA,EAAA,EAAA,cAHgC,CAAE,IAAK,EAAQ,IAAK,EAAQ,EAC3D,CAAC,EAAQ,EAAO,CAChB,EAKW,EAA6B,GAAqC,CAC9E,IAAM,EAAW,EAAyB,EAAM,CAC1C,GAAA,EAAA,EAAA,cACE,CACN,eAAgB,EAAM,eACtB,cAAe,EAAM,cACrB,YAAa,EAAM,YACnB,EACD,CAAC,EAAM,CACP,CACD,EAAwB,EAAM,cAAe,EAAU,EAAQ"}
|
package/dist/react-router.es.js
CHANGED
|
@@ -1,57 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
), z = (t) => {
|
|
38
|
-
const e = g(t), o = p(t);
|
|
39
|
-
return n(
|
|
40
|
-
() => ({ get: e, set: o }),
|
|
41
|
-
[e, o]
|
|
42
|
-
);
|
|
43
|
-
}, w = (t) => {
|
|
44
|
-
const e = z(t), o = n(
|
|
45
|
-
() => ({
|
|
46
|
-
onStateRestore: t.onStateRestore,
|
|
47
|
-
onStateChange: t.onStateChange,
|
|
48
|
-
onStateSave: t.onStateSave
|
|
49
|
-
}),
|
|
50
|
-
[t]
|
|
51
|
-
);
|
|
52
|
-
L(t.validateState, e, o);
|
|
1
|
+
import { t as e } from "./useStateHandler-cGvOZfIR.js";
|
|
2
|
+
import { useCallback as t, useMemo as n } from "react";
|
|
3
|
+
import { useLocation as r, useNavigate as i } from "react-router-dom";
|
|
4
|
+
import { parse as a, stringify as o } from "qs";
|
|
5
|
+
//#region src/react-router/useLocationState.tsx
|
|
6
|
+
var s = () => {
|
|
7
|
+
let e = r();
|
|
8
|
+
return t(() => a(e.search.slice(1)), [e]);
|
|
9
|
+
}, c = () => {
|
|
10
|
+
let e = i();
|
|
11
|
+
return t(async (t) => {
|
|
12
|
+
await e(`?${o(t)}`);
|
|
13
|
+
}, [e]);
|
|
14
|
+
}, l = () => {
|
|
15
|
+
let e = s(), t = c();
|
|
16
|
+
return n(() => ({
|
|
17
|
+
deserialize: e,
|
|
18
|
+
serialize: t
|
|
19
|
+
}), [e, t]);
|
|
20
|
+
}, u = (t, n, r) => {
|
|
21
|
+
e(l(), t, n, r);
|
|
22
|
+
}, d = (e) => t(() => e.state, [e]), f = (e) => t((t) => {
|
|
23
|
+
e.state = t;
|
|
24
|
+
}, [e]), p = (e) => {
|
|
25
|
+
let t = d(e), r = f(e);
|
|
26
|
+
return n(() => ({
|
|
27
|
+
get: t,
|
|
28
|
+
set: r
|
|
29
|
+
}), [t, r]);
|
|
30
|
+
}, m = (e) => {
|
|
31
|
+
let t = p(e), r = n(() => ({
|
|
32
|
+
onStateRestore: e.onStateRestore,
|
|
33
|
+
onStateChange: e.onStateChange,
|
|
34
|
+
onStateSave: e.onStateSave
|
|
35
|
+
}), [e]);
|
|
36
|
+
u(e.validateState, t, r);
|
|
53
37
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
//# sourceMappingURL=react-router.es.js.map
|
|
38
|
+
//#endregion
|
|
39
|
+
export { m as useLocationState };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=react-router.es.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-router.es.js","sources":["../src/react-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { type ParsedQs, parse, stringify } from 'qs';\nimport { useCallback, useMemo } from 'react';\nimport { useLocation, useNavigate } from 'react-router-dom';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.search.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tconst newUrl = `?${stringify(state)}`;\n\t\t\tawait navigate(newUrl);\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"
|
|
1
|
+
{"version":3,"file":"react-router.es.js","names":[],"sources":["../src/react-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { type ParsedQs, parse, stringify } from 'qs';\nimport { useCallback, useMemo } from 'react';\nimport { useLocation, useNavigate } from 'react-router-dom';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.search.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tconst newUrl = `?${stringify(state)}`;\n\t\t\tawait navigate(newUrl);\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback((): TState => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState): void => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"mappings":";;;;;AASA,IAAM,UAAuD;CAC5D,IAAM,IAAW,GAAa;AAG9B,QAAO,QACU,EAAM,EAAS,OAAO,MAAM,EAAE,CAAC,EAC/C,CAAC,EAAS,CACV;GAGI,UAAuE;CAC5E,IAAM,IAAW,GAAa;AAE9B,QAAO,EACN,OAAO,MAAiC;AAEvC,QAAM,EADS,IAAI,EAAU,EAAM,GACb;IAEvB,CAAC,EAAS,CACV;GAGI,UAA4D;CACjE,IAAM,IAAe,GAA8B,EAC7C,IAAa,GAA4B;AAQ/C,QAPc,SACe;EAC3B,aAAa;EACb,WAAW;EACX,GACD,CAAC,GAAc,EAAW,CAC1B;GAKI,KACL,GACA,GACA,MACU;AAEV,GADc,GAAuB,EACd,GAAW,GAAU,EAAQ;GAG/C,KACL,MAEO,QAA0B,EAAM,OAAO,CAAC,EAAM,CAAC,EAGjD,KACL,MAEO,GACL,MAAwB;AACxB,GAAM,QAAQ;GAEf,CAAC,EAAM,CACP,EAGI,KACL,MAC4B;CAC5B,IAAM,IAAS,EAAuB,EAAM,EACtC,IAAS,EAAuB,EAAM;AAK5C,QAJiB,SACe;EAAE,KAAK;EAAQ,KAAK;EAAQ,GAC3D,CAAC,GAAQ,EAAO,CAChB;GAKW,KAA6B,MAAqC;CAC9E,IAAM,IAAW,EAAyB,EAAM,EAC1C,IAAU,SACR;EACN,gBAAgB,EAAM;EACtB,eAAe,EAAM;EACrB,aAAa,EAAM;EACnB,GACD,CAAC,EAAM,CACP;AACD,GAAwB,EAAM,eAAe,GAAU,EAAQ"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
//# sourceMappingURL=tanstack-router.cjs.js.map
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./useStateHandler-lwgfPmpv.cjs`);let t=require(`react`),n=require(`qs`),r=require(`@tanstack/react-router`);var i=()=>{let e=(0,r.useLocation)();return(0,t.useCallback)(()=>(0,n.parse)(e.searchStr.slice(1)),[e])},a=()=>{let e=(0,r.useNavigate)();return(0,t.useCallback)(async t=>{await e({search:t})},[e])},o=()=>{let e=i(),n=a();return(0,t.useMemo)(()=>({deserialize:e,serialize:n}),[e,n])},s=(t,n,r)=>{e.t(o(),t,n,r)},c=e=>(0,t.useCallback)(()=>e.state,[e]),l=e=>(0,t.useCallback)(t=>{e.state=t},[e]),u=e=>{let n=c(e),r=l(e);return(0,t.useMemo)(()=>({get:n,set:r}),[n,r])},d=e=>{let n=u(e),r=(0,t.useMemo)(()=>({onStateRestore:e.onStateRestore,onStateChange:e.onStateChange,onStateSave:e.onStateSave}),[e]);s(e.validateState,n,r)};exports.useLocationState=d;
|
|
2
|
+
//# sourceMappingURL=tanstack-router.cjs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tanstack-router.cjs.js","sources":["../src/tanstack-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useLocation, useNavigate } from '@tanstack/react-router';\nimport { type ParsedQs, parse } from 'qs';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.searchStr.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tawait navigate({ search: state as any /* FIXME */ });\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback(() => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState) => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"
|
|
1
|
+
{"version":3,"file":"tanstack-router.cjs.js","names":[],"sources":["../src/tanstack-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useLocation, useNavigate } from '@tanstack/react-router';\nimport { type ParsedQs, parse } from 'qs';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.searchStr.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tawait navigate({ search: state as any /* FIXME */ });\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback(() => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState) => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"mappings":"gMASA,IAAM,MAAuD,CAC5D,IAAM,GAAA,EAAA,EAAA,cAAwB,CAG9B,OAAA,EAAA,EAAA,kBAAA,EAAA,EAAA,OACuB,EAAS,UAAU,MAAM,EAAE,CAAC,CAClD,CAAC,EAAS,CACV,EAGI,MAAuE,CAC5E,IAAM,GAAA,EAAA,EAAA,cAAwB,CAE9B,OAAA,EAAA,EAAA,aACC,KAAO,IAAiC,CACvC,MAAM,EAAS,CAAE,OAAQ,EAA0B,CAAC,EAErD,CAAC,EAAS,CACV,EAGI,MAA4D,CACjE,IAAM,EAAe,GAA8B,CAC7C,EAAa,GAA4B,CAQ/C,OAAA,EAAA,EAAA,cAN6B,CAC3B,YAAa,EACb,UAAW,EACX,EACD,CAAC,EAAc,EAAW,CAC1B,EAKI,GACL,EACA,EACA,IACU,CAEV,EAAA,EADc,GAAuB,CACd,EAAW,EAAU,EAAQ,EAG/C,EACL,IAEA,EAAA,EAAA,iBAAyB,EAAM,MAAO,CAAC,EAAM,CAAC,CAGzC,EACL,IAEA,EAAA,EAAA,aACE,GAAkB,CAClB,EAAM,MAAQ,GAEf,CAAC,EAAM,CACP,CAGI,EACL,GAC4B,CAC5B,IAAM,EAAS,EAAuB,EAAM,CACtC,EAAS,EAAuB,EAAM,CAK5C,OAAA,EAAA,EAAA,cAHgC,CAAE,IAAK,EAAQ,IAAK,EAAQ,EAC3D,CAAC,EAAQ,EAAO,CAChB,EAKW,EAA6B,GAAqC,CAC9E,IAAM,EAAW,EAAyB,EAAM,CAC1C,GAAA,EAAA,EAAA,cACE,CACN,eAAgB,EAAM,eACtB,cAAe,EAAM,cACrB,YAAa,EAAM,YACnB,EACD,CAAC,EAAM,CACP,CACD,EAAwB,EAAM,cAAe,EAAU,EAAQ"}
|
|
@@ -1,59 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { parse as
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}, d = () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
38
|
-
[t]
|
|
39
|
-
), g = (t) => {
|
|
40
|
-
const e = L(t), o = p(t);
|
|
41
|
-
return s(
|
|
42
|
-
() => ({ get: e, set: o }),
|
|
43
|
-
[e, o]
|
|
44
|
-
);
|
|
45
|
-
}, C = (t) => {
|
|
46
|
-
const e = g(t), o = s(
|
|
47
|
-
() => ({
|
|
48
|
-
onStateRestore: t.onStateRestore,
|
|
49
|
-
onStateChange: t.onStateChange,
|
|
50
|
-
onStateSave: t.onStateSave
|
|
51
|
-
}),
|
|
52
|
-
[t]
|
|
53
|
-
);
|
|
54
|
-
m(t.validateState, e, o);
|
|
1
|
+
import { t as e } from "./useStateHandler-cGvOZfIR.js";
|
|
2
|
+
import { useCallback as t, useMemo as n } from "react";
|
|
3
|
+
import { parse as r } from "qs";
|
|
4
|
+
import { useLocation as i, useNavigate as a } from "@tanstack/react-router";
|
|
5
|
+
//#region src/tanstack-router/useLocationState.tsx
|
|
6
|
+
var o = () => {
|
|
7
|
+
let e = i();
|
|
8
|
+
return t(() => r(e.searchStr.slice(1)), [e]);
|
|
9
|
+
}, s = () => {
|
|
10
|
+
let e = a();
|
|
11
|
+
return t(async (t) => {
|
|
12
|
+
await e({ search: t });
|
|
13
|
+
}, [e]);
|
|
14
|
+
}, c = () => {
|
|
15
|
+
let e = o(), t = s();
|
|
16
|
+
return n(() => ({
|
|
17
|
+
deserialize: e,
|
|
18
|
+
serialize: t
|
|
19
|
+
}), [e, t]);
|
|
20
|
+
}, l = (t, n, r) => {
|
|
21
|
+
e(c(), t, n, r);
|
|
22
|
+
}, u = (e) => t(() => e.state, [e]), d = (e) => t((t) => {
|
|
23
|
+
e.state = t;
|
|
24
|
+
}, [e]), f = (e) => {
|
|
25
|
+
let t = u(e), r = d(e);
|
|
26
|
+
return n(() => ({
|
|
27
|
+
get: t,
|
|
28
|
+
set: r
|
|
29
|
+
}), [t, r]);
|
|
30
|
+
}, p = (e) => {
|
|
31
|
+
let t = f(e), r = n(() => ({
|
|
32
|
+
onStateRestore: e.onStateRestore,
|
|
33
|
+
onStateChange: e.onStateChange,
|
|
34
|
+
onStateSave: e.onStateSave
|
|
35
|
+
}), [e]);
|
|
36
|
+
l(e.validateState, t, r);
|
|
55
37
|
};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
//# sourceMappingURL=tanstack-router.es.js.map
|
|
38
|
+
//#endregion
|
|
39
|
+
export { p as useLocationState };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=tanstack-router.es.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tanstack-router.es.js","sources":["../src/tanstack-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useLocation, useNavigate } from '@tanstack/react-router';\nimport { type ParsedQs, parse } from 'qs';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.searchStr.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tawait navigate({ search: state as any /* FIXME */ });\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback(() => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState) => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"
|
|
1
|
+
{"version":3,"file":"tanstack-router.es.js","names":[],"sources":["../src/tanstack-router/useLocationState.tsx"],"sourcesContent":["import type { IStateAccessor } from '@/components/IStateAccessor';\nimport type { IStateCodec } from '@/components/IStateCodec';\nimport type { IStateHandlerOptions } from '@/components/IStateHandlerOptions';\nimport { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport { useLocation, useNavigate } from '@tanstack/react-router';\nimport { type ParsedQs, parse } from 'qs';\nimport { useCallback, useMemo } from 'react';\n\nconst useLocationStateDeserializer = (): (() => ParsedQs) => {\n\tconst location = useLocation();\n\n\t// Pass `location` as deps instead of `location.search`.\n\treturn useCallback(\n\t\t(): ParsedQs => parse(location.searchStr.slice(1)),\n\t\t[location],\n\t);\n};\n\nconst useLocationStateSerializer = <TState,>(): ((state: TState) => void) => {\n\tconst navigate = useNavigate();\n\n\treturn useCallback(\n\t\tasync (state: TState): Promise<void> => {\n\t\t\tawait navigate({ search: state as any /* FIXME */ });\n\t\t},\n\t\t[navigate],\n\t);\n};\n\nconst useLocationStateCodec = <TState,>(): IStateCodec<TState> => {\n\tconst deserializer = useLocationStateDeserializer();\n\tconst serializer = useLocationStateSerializer();\n\tconst codec = useMemo(\n\t\t(): IStateCodec<TState> => ({\n\t\t\tdeserialize: deserializer,\n\t\t\tserialize: serializer,\n\t\t}),\n\t\t[deserializer, serializer],\n\t);\n\treturn codec;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, options);\n};\n\nconst useLocationStateGetter = <TState,>(\n\tstore: IStateStore<TState>,\n): (() => TState) => {\n\treturn useCallback(() => store.state, [store]);\n};\n\nconst useLocationStateSetter = <TState,>(\n\tstore: IStateStore<TState>,\n): ((state: TState) => void) => {\n\treturn useCallback(\n\t\t(state: TState) => {\n\t\t\tstore.state = state;\n\t\t},\n\t\t[store],\n\t);\n};\n\nconst useLocationStateAccessor = <TState,>(\n\tstore: IStateStore<TState>,\n): IStateAccessor<TState> => {\n\tconst getter = useLocationStateGetter(store);\n\tconst setter = useLocationStateSetter(store);\n\tconst accessor = useMemo(\n\t\t(): IStateAccessor<TState> => ({ get: getter, set: setter }),\n\t\t[getter, setter],\n\t);\n\treturn accessor;\n};\n\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nexport const useLocationState = <TState,>(store: IStateStore<TState>): void => {\n\tconst accessor = useLocationStateAccessor(store);\n\tconst options = useMemo(\n\t\t() => ({\n\t\t\tonStateRestore: store.onStateRestore,\n\t\t\tonStateChange: store.onStateChange,\n\t\t\tonStateSave: store.onStateSave,\n\t\t}),\n\t\t[store],\n\t);\n\tuseLocationStateHandler(store.validateState, accessor, options);\n};\n"],"mappings":";;;;;AASA,IAAM,UAAuD;CAC5D,IAAM,IAAW,GAAa;AAG9B,QAAO,QACU,EAAM,EAAS,UAAU,MAAM,EAAE,CAAC,EAClD,CAAC,EAAS,CACV;GAGI,UAAuE;CAC5E,IAAM,IAAW,GAAa;AAE9B,QAAO,EACN,OAAO,MAAiC;AACvC,QAAM,EAAS,EAAE,QAAQ,GAA0B,CAAC;IAErD,CAAC,EAAS,CACV;GAGI,UAA4D;CACjE,IAAM,IAAe,GAA8B,EAC7C,IAAa,GAA4B;AAQ/C,QAPc,SACe;EAC3B,aAAa;EACb,WAAW;EACX,GACD,CAAC,GAAc,EAAW,CAC1B;GAKI,KACL,GACA,GACA,MACU;AAEV,GADc,GAAuB,EACd,GAAW,GAAU,EAAQ;GAG/C,KACL,MAEO,QAAkB,EAAM,OAAO,CAAC,EAAM,CAAC,EAGzC,KACL,MAEO,GACL,MAAkB;AAClB,GAAM,QAAQ;GAEf,CAAC,EAAM,CACP,EAGI,KACL,MAC4B;CAC5B,IAAM,IAAS,EAAuB,EAAM,EACtC,IAAS,EAAuB,EAAM;AAK5C,QAJiB,SACe;EAAE,KAAK;EAAQ,KAAK;EAAQ,GAC3D,CAAC,GAAQ,EAAO,CAChB;GAKW,KAA6B,MAAqC;CAC9E,IAAM,IAAW,EAAyB,EAAM,EAC1C,IAAU,SACR;EACN,gBAAgB,EAAM;EACtB,eAAe,EAAM;EACrB,aAAa,EAAM;EACnB,GACD,CAAC,EAAM,CACP;AACD,GAAwB,EAAM,eAAe,GAAU,EAAQ"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useCallback as e, useEffect as t, useRef as n } from "react";
|
|
2
|
+
import { isEqual as r, omitBy as i } from "lodash-es";
|
|
3
|
+
import { reaction as a } from "mobx";
|
|
4
|
+
//#region src/components/useStateHandler.tsx
|
|
5
|
+
var o = (e, n, r, i, a) => {
|
|
6
|
+
t(() => {
|
|
7
|
+
let t = n.deserialize();
|
|
8
|
+
r(t) && (e.current = !0, i.set(t), e.current = !1, a.onStateRestore?.({ state: t }));
|
|
9
|
+
}, [
|
|
10
|
+
n,
|
|
11
|
+
r,
|
|
12
|
+
e,
|
|
13
|
+
i,
|
|
14
|
+
a
|
|
15
|
+
]);
|
|
16
|
+
}, s = (n, o, s) => {
|
|
17
|
+
let c = e((e, t) => {
|
|
18
|
+
if (!s.onStateChange) return;
|
|
19
|
+
let a = i(e, (e, n) => r(t[n], e)), o = Object.keys(a);
|
|
20
|
+
console.assert(o.length > 0), s.onStateChange({
|
|
21
|
+
state: e,
|
|
22
|
+
previousState: t,
|
|
23
|
+
keys: o,
|
|
24
|
+
popState: n.current
|
|
25
|
+
});
|
|
26
|
+
}, [n, s]);
|
|
27
|
+
t(() => a(o.get, c), [o, c]), t(() => {
|
|
28
|
+
let e = o.get();
|
|
29
|
+
n.current = !0, c(e, {}), n.current = !1;
|
|
30
|
+
}, [
|
|
31
|
+
o,
|
|
32
|
+
n,
|
|
33
|
+
c
|
|
34
|
+
]);
|
|
35
|
+
}, c = (e, n, r, i) => {
|
|
36
|
+
t(() => a(n.get, (t) => {
|
|
37
|
+
e.current || (r.serialize(t), i.onStateSave?.({ state: t }));
|
|
38
|
+
}), [
|
|
39
|
+
n,
|
|
40
|
+
e,
|
|
41
|
+
r,
|
|
42
|
+
i
|
|
43
|
+
]);
|
|
44
|
+
}, l = (e, t, r, i) => {
|
|
45
|
+
let a = n(!1);
|
|
46
|
+
o(a, e, t, r, i), s(a, r, i), c(a, r, e, i);
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { l as t };
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=useStateHandler-cGvOZfIR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStateHandler-cGvOZfIR.js","names":[],"sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type {\n\tIStateAccessor,\n\tIStateGetter,\n\tIStateSetter,\n} from '@/components/IStateAccessor';\nimport type {\n\tIStateCodec,\n\tIStateDeserializer,\n\tIStateSerializer,\n} from '@/components/IStateCodec';\nimport type {\n\tIHandleStateChangeOptions,\n\tIRestoreStateOptions,\n\tISaveStateOptions,\n\tIStateHandlerOptions,\n} from '@/components/IStateHandlerOptions';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useCallback, useEffect, useRef } from 'react';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: IStateDeserializer /*<TState>*/,\n\tvalidator: (state: unknown) => state is TState,\n\tsetter: IStateSetter<TState>,\n\toptions: IRestoreStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\tconst state = deserializer.deserialize();\n\n\t\tif (validator(state)) {\n\t\t\tpopStateRef.current = true;\n\n\t\t\tsetter.set(state);\n\n\t\t\tpopStateRef.current = false;\n\n\t\t\toptions.onStateRestore?.({ state: state });\n\t\t}\n\t}, [deserializer, validator, popStateRef, setter, options]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\toptions: IHandleStateChangeOptions<TState>,\n): void => {\n\tconst handleStateChange = useCallback(\n\t\t(state: TState, previousState: TState): void => {\n\t\t\tif (!options.onStateChange) return;\n\n\t\t\t// Compare the current and previous values.\n\t\t\tconst diff = omitBy(state, (v, k) =>\n\t\t\t\tisEqual(previousState[k as keyof typeof previousState], v),\n\t\t\t);\n\n\t\t\t// Assuming that the current value is `{ filter: 'Miku', page: 3939, searchType: 'Artist' }`, and the previous one is `{ filter: 'Miku', page: 1 }`,\n\t\t\t// then the diff will be `{ page: 3939, searchType: 'Artist' }`, which results in `['page', 'searchType']`.\n\t\t\tconst keys = Object.keys(diff) as (keyof TState)[];\n\t\t\tconsole.assert(keys.length > 0);\n\n\t\t\toptions.onStateChange({\n\t\t\t\tstate: state,\n\t\t\t\tpreviousState: previousState,\n\t\t\t\tkeys: keys,\n\t\t\t\tpopState: popStateRef.current,\n\t\t\t});\n\t\t},\n\t\t[popStateRef, options],\n\t);\n\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, handleStateChange);\n\t}, [getter, handleStateChange]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tconst state = getter.get();\n\t\tconst previousState = {} as TState;\n\n\t\tpopStateRef.current = true;\n\n\t\thandleStateChange(state, previousState);\n\n\t\tpopStateRef.current = false;\n\t}, [getter, popStateRef, handleStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\tserializer: IStateSerializer<TState>,\n\toptions: ISaveStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, (state) => {\n\t\t\tif (popStateRef.current) return;\n\n\t\t\tserializer.serialize(state);\n\n\t\t\toptions.onStateSave?.({ state: state });\n\t\t});\n\t}, [getter, popStateRef, serializer, options]);\n};\n\nexport const useStateHandler = <TState,>(\n\tcodec: IStateCodec<TState>,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\t// Whether currently processing popstate. This is to prevent adding the previous state to history.\n\tconst popStateRef = useRef(false);\n\n\tuseRestoreState(popStateRef, codec, validator, accessor, options);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, accessor, options);\n\n\tuseSaveState(popStateRef, accessor, codec, options);\n};\n"],"mappings":";;;;AAoBA,IAAM,KACL,GACA,GACA,GACA,GACA,MACU;AACV,SAAgB;EACf,IAAM,IAAQ,EAAa,aAAa;AAExC,EAAI,EAAU,EAAM,KACnB,EAAY,UAAU,IAEtB,EAAO,IAAI,EAAM,EAEjB,EAAY,UAAU,IAEtB,EAAQ,iBAAiB,EAAS,UAAO,CAAC;IAEzC;EAAC;EAAc;EAAW;EAAa;EAAQ;EAAQ,CAAC;GAGtD,KACL,GACA,GACA,MACU;CACV,IAAM,IAAoB,GACxB,GAAe,MAAgC;AAC/C,MAAI,CAAC,EAAQ,cAAe;EAG5B,IAAM,IAAO,EAAO,IAAQ,GAAG,MAC9B,EAAQ,EAAc,IAAkC,EAAE,CAC1D,EAIK,IAAO,OAAO,KAAK,EAAK;AAG9B,EAFA,QAAQ,OAAO,EAAK,SAAS,EAAE,EAE/B,EAAQ,cAAc;GACd;GACQ;GACT;GACN,UAAU,EAAY;GACtB,CAAC;IAEH,CAAC,GAAa,EAAQ,CACtB;AAQD,CANA,QAEQ,EAAS,EAAO,KAAK,EAAkB,EAC5C,CAAC,GAAQ,EAAkB,CAAC,EAG/B,QAAgB;EACf,IAAM,IAAQ,EAAO,KAAK;AAO1B,EAJA,EAAY,UAAU,IAEtB,EAAkB,GAJI,EAAE,CAIe,EAEvC,EAAY,UAAU;IACpB;EAAC;EAAQ;EAAa;EAAkB,CAAC;GAGvC,KACL,GACA,GACA,GACA,MACU;AACV,SAEQ,EAAS,EAAO,MAAM,MAAU;AAClC,IAAY,YAEhB,EAAW,UAAU,EAAM,EAE3B,EAAQ,cAAc,EAAS,UAAO,CAAC;GACtC,EACA;EAAC;EAAQ;EAAa;EAAY;EAAQ,CAAC;GAGlC,KACZ,GACA,GACA,GACA,MACU;CAEV,IAAM,IAAc,EAAO,GAAM;AAOjC,CALA,EAAgB,GAAa,GAAO,GAAW,GAAU,EAAQ,EAGjE,EAAqB,GAAa,GAAU,EAAQ,EAEpD,EAAa,GAAa,GAAU,GAAO,EAAQ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
let e=require(`react`),t=require(`lodash-es`),n=require(`mobx`);var r=(t,n,r,i,a)=>{(0,e.useEffect)(()=>{let e=n.deserialize();r(e)&&(t.current=!0,i.set(e),t.current=!1,a.onStateRestore?.({state:e}))},[n,r,t,i,a])},i=(r,i,a)=>{let o=(0,e.useCallback)((e,n)=>{if(!a.onStateChange)return;let i=(0,t.omitBy)(e,(e,r)=>(0,t.isEqual)(n[r],e)),o=Object.keys(i);console.assert(o.length>0),a.onStateChange({state:e,previousState:n,keys:o,popState:r.current})},[r,a]);(0,e.useEffect)(()=>(0,n.reaction)(i.get,o),[i,o]),(0,e.useEffect)(()=>{let e=i.get();r.current=!0,o(e,{}),r.current=!1},[i,r,o])},a=(t,r,i,a)=>{(0,e.useEffect)(()=>(0,n.reaction)(r.get,e=>{t.current||(i.serialize(e),a.onStateSave?.({state:e}))}),[r,t,i,a])},o=(t,n,o,s)=>{let c=(0,e.useRef)(!1);r(c,t,n,o,s),i(c,o,s),a(c,o,t,s)};Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return o}});
|
|
2
|
+
//# sourceMappingURL=useStateHandler-lwgfPmpv.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStateHandler-lwgfPmpv.cjs","names":[],"sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type {\n\tIStateAccessor,\n\tIStateGetter,\n\tIStateSetter,\n} from '@/components/IStateAccessor';\nimport type {\n\tIStateCodec,\n\tIStateDeserializer,\n\tIStateSerializer,\n} from '@/components/IStateCodec';\nimport type {\n\tIHandleStateChangeOptions,\n\tIRestoreStateOptions,\n\tISaveStateOptions,\n\tIStateHandlerOptions,\n} from '@/components/IStateHandlerOptions';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useCallback, useEffect, useRef } from 'react';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: IStateDeserializer /*<TState>*/,\n\tvalidator: (state: unknown) => state is TState,\n\tsetter: IStateSetter<TState>,\n\toptions: IRestoreStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\tconst state = deserializer.deserialize();\n\n\t\tif (validator(state)) {\n\t\t\tpopStateRef.current = true;\n\n\t\t\tsetter.set(state);\n\n\t\t\tpopStateRef.current = false;\n\n\t\t\toptions.onStateRestore?.({ state: state });\n\t\t}\n\t}, [deserializer, validator, popStateRef, setter, options]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\toptions: IHandleStateChangeOptions<TState>,\n): void => {\n\tconst handleStateChange = useCallback(\n\t\t(state: TState, previousState: TState): void => {\n\t\t\tif (!options.onStateChange) return;\n\n\t\t\t// Compare the current and previous values.\n\t\t\tconst diff = omitBy(state, (v, k) =>\n\t\t\t\tisEqual(previousState[k as keyof typeof previousState], v),\n\t\t\t);\n\n\t\t\t// Assuming that the current value is `{ filter: 'Miku', page: 3939, searchType: 'Artist' }`, and the previous one is `{ filter: 'Miku', page: 1 }`,\n\t\t\t// then the diff will be `{ page: 3939, searchType: 'Artist' }`, which results in `['page', 'searchType']`.\n\t\t\tconst keys = Object.keys(diff) as (keyof TState)[];\n\t\t\tconsole.assert(keys.length > 0);\n\n\t\t\toptions.onStateChange({\n\t\t\t\tstate: state,\n\t\t\t\tpreviousState: previousState,\n\t\t\t\tkeys: keys,\n\t\t\t\tpopState: popStateRef.current,\n\t\t\t});\n\t\t},\n\t\t[popStateRef, options],\n\t);\n\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, handleStateChange);\n\t}, [getter, handleStateChange]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tconst state = getter.get();\n\t\tconst previousState = {} as TState;\n\n\t\tpopStateRef.current = true;\n\n\t\thandleStateChange(state, previousState);\n\n\t\tpopStateRef.current = false;\n\t}, [getter, popStateRef, handleStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\tserializer: IStateSerializer<TState>,\n\toptions: ISaveStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, (state) => {\n\t\t\tif (popStateRef.current) return;\n\n\t\t\tserializer.serialize(state);\n\n\t\t\toptions.onStateSave?.({ state: state });\n\t\t});\n\t}, [getter, popStateRef, serializer, options]);\n};\n\nexport const useStateHandler = <TState,>(\n\tcodec: IStateCodec<TState>,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\t// Whether currently processing popstate. This is to prevent adding the previous state to history.\n\tconst popStateRef = useRef(false);\n\n\tuseRestoreState(popStateRef, codec, validator, accessor, options);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, accessor, options);\n\n\tuseSaveState(popStateRef, accessor, codec, options);\n};\n"],"mappings":"gEAoBA,IAAM,GACL,EACA,EACA,EACA,EACA,IACU,EACV,EAAA,EAAA,eAAgB,CACf,IAAM,EAAQ,EAAa,aAAa,CAEpC,EAAU,EAAM,GACnB,EAAY,QAAU,GAEtB,EAAO,IAAI,EAAM,CAEjB,EAAY,QAAU,GAEtB,EAAQ,iBAAiB,CAAS,QAAO,CAAC,GAEzC,CAAC,EAAc,EAAW,EAAa,EAAQ,EAAQ,CAAC,EAGtD,GACL,EACA,EACA,IACU,CACV,IAAM,GAAA,EAAA,EAAA,cACJ,EAAe,IAAgC,CAC/C,GAAI,CAAC,EAAQ,cAAe,OAG5B,IAAM,GAAA,EAAA,EAAA,QAAc,GAAQ,EAAG,KAAA,EAAA,EAAA,SACtB,EAAc,GAAkC,EAAE,CAC1D,CAIK,EAAO,OAAO,KAAK,EAAK,CAC9B,QAAQ,OAAO,EAAK,OAAS,EAAE,CAE/B,EAAQ,cAAc,CACd,QACQ,gBACT,OACN,SAAU,EAAY,QACtB,CAAC,EAEH,CAAC,EAAa,EAAQ,CACtB,EAED,EAAA,EAAA,gBAEC,EAAA,EAAA,UAAgB,EAAO,IAAK,EAAkB,CAC5C,CAAC,EAAQ,EAAkB,CAAC,EAG/B,EAAA,EAAA,eAAgB,CACf,IAAM,EAAQ,EAAO,KAAK,CAG1B,EAAY,QAAU,GAEtB,EAAkB,EAJI,EAAE,CAIe,CAEvC,EAAY,QAAU,IACpB,CAAC,EAAQ,EAAa,EAAkB,CAAC,EAGvC,GACL,EACA,EACA,EACA,IACU,EACV,EAAA,EAAA,gBAEC,EAAA,EAAA,UAAgB,EAAO,IAAM,GAAU,CAClC,EAAY,UAEhB,EAAW,UAAU,EAAM,CAE3B,EAAQ,cAAc,CAAS,QAAO,CAAC,GACtC,CACA,CAAC,EAAQ,EAAa,EAAY,EAAQ,CAAC,EAGlC,GACZ,EACA,EACA,EACA,IACU,CAEV,IAAM,GAAA,EAAA,EAAA,QAAqB,GAAM,CAEjC,EAAgB,EAAa,EAAO,EAAW,EAAU,EAAQ,CAGjE,EAAqB,EAAa,EAAU,EAAQ,CAEpD,EAAa,EAAa,EAAU,EAAO,EAAQ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aigamo/route-sphere",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {},
|
|
6
6
|
"devDependencies": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"@typescript-eslint/eslint-plugin": "^8.51.0",
|
|
17
17
|
"@typescript-eslint/parser": "^8.51.0",
|
|
18
18
|
"@typescript/native-preview": "7.0.0-dev.20260217.1",
|
|
19
|
-
"@vitejs/plugin-react": "^
|
|
19
|
+
"@vitejs/plugin-react": "^6.0.0",
|
|
20
20
|
"eslint": "^9.39.2",
|
|
21
21
|
"eslint-config-prettier": "^10.1.8",
|
|
22
22
|
"eslint-config-react-app": "^7.0.1",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"react-dom": "^18.3.1",
|
|
36
36
|
"react-router-dom": "^7.13.1",
|
|
37
37
|
"rimraf": "^6.1.2",
|
|
38
|
-
"vite": "^
|
|
38
|
+
"vite": "^8.0.0",
|
|
39
39
|
"vite-plugin-dts": "^4.5.4",
|
|
40
|
-
"vitest": "^4.0
|
|
40
|
+
"vitest": "^4.1.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"@tanstack/react-router": "^1.144.0",
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { omitBy as h, isEqual as S } from "lodash-es";
|
|
2
|
-
import { reaction as l } from "mobx";
|
|
3
|
-
import { useRef as C, useEffect as a, useCallback as g } from "react";
|
|
4
|
-
const i = (e, u, r, t, n) => {
|
|
5
|
-
a(() => {
|
|
6
|
-
const s = u.deserialize();
|
|
7
|
-
r(s) && (e.current = !0, t.set(s), e.current = !1, n.onStateRestore?.({ state: s }));
|
|
8
|
-
}, [u, r, e, t, n]);
|
|
9
|
-
}, k = (e, u, r) => {
|
|
10
|
-
const t = g(
|
|
11
|
-
(n, s) => {
|
|
12
|
-
if (!r.onStateChange) return;
|
|
13
|
-
const f = h(
|
|
14
|
-
n,
|
|
15
|
-
(m, o) => S(s[o], m)
|
|
16
|
-
), c = Object.keys(f);
|
|
17
|
-
console.assert(c.length > 0), r.onStateChange({
|
|
18
|
-
state: n,
|
|
19
|
-
previousState: s,
|
|
20
|
-
keys: c,
|
|
21
|
-
popState: e.current
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
[e, r]
|
|
25
|
-
);
|
|
26
|
-
a(() => l(u.get, t), [u, t]), a(() => {
|
|
27
|
-
const n = u.get(), s = {};
|
|
28
|
-
e.current = !0, t(n, s), e.current = !1;
|
|
29
|
-
}, [u, e, t]);
|
|
30
|
-
}, y = (e, u, r, t) => {
|
|
31
|
-
a(() => l(u.get, (n) => {
|
|
32
|
-
e.current || (r.serialize(n), t.onStateSave?.({ state: n }));
|
|
33
|
-
}), [u, e, r, t]);
|
|
34
|
-
}, H = (e, u, r, t) => {
|
|
35
|
-
const n = C(!1);
|
|
36
|
-
i(n, e, u, r, t), k(n, r, t), y(n, r, e, t);
|
|
37
|
-
};
|
|
38
|
-
export {
|
|
39
|
-
H as u
|
|
40
|
-
};
|
|
41
|
-
//# sourceMappingURL=useStateHandler-Dbkejxjm.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useStateHandler-Dbkejxjm.js","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type {\n\tIStateAccessor,\n\tIStateGetter,\n\tIStateSetter,\n} from '@/components/IStateAccessor';\nimport type {\n\tIStateCodec,\n\tIStateDeserializer,\n\tIStateSerializer,\n} from '@/components/IStateCodec';\nimport type {\n\tIHandleStateChangeOptions,\n\tIRestoreStateOptions,\n\tISaveStateOptions,\n\tIStateHandlerOptions,\n} from '@/components/IStateHandlerOptions';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useCallback, useEffect, useRef } from 'react';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: IStateDeserializer /*<TState>*/,\n\tvalidator: (state: unknown) => state is TState,\n\tsetter: IStateSetter<TState>,\n\toptions: IRestoreStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\tconst state = deserializer.deserialize();\n\n\t\tif (validator(state)) {\n\t\t\tpopStateRef.current = true;\n\n\t\t\tsetter.set(state);\n\n\t\t\tpopStateRef.current = false;\n\n\t\t\toptions.onStateRestore?.({ state: state });\n\t\t}\n\t}, [deserializer, validator, popStateRef, setter, options]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\toptions: IHandleStateChangeOptions<TState>,\n): void => {\n\tconst handleStateChange = useCallback(\n\t\t(state: TState, previousState: TState): void => {\n\t\t\tif (!options.onStateChange) return;\n\n\t\t\t// Compare the current and previous values.\n\t\t\tconst diff = omitBy(state, (v, k) =>\n\t\t\t\tisEqual(previousState[k as keyof typeof previousState], v),\n\t\t\t);\n\n\t\t\t// Assuming that the current value is `{ filter: 'Miku', page: 3939, searchType: 'Artist' }`, and the previous one is `{ filter: 'Miku', page: 1 }`,\n\t\t\t// then the diff will be `{ page: 3939, searchType: 'Artist' }`, which results in `['page', 'searchType']`.\n\t\t\tconst keys = Object.keys(diff) as (keyof TState)[];\n\t\t\tconsole.assert(keys.length > 0);\n\n\t\t\toptions.onStateChange({\n\t\t\t\tstate: state,\n\t\t\t\tpreviousState: previousState,\n\t\t\t\tkeys: keys,\n\t\t\t\tpopState: popStateRef.current,\n\t\t\t});\n\t\t},\n\t\t[popStateRef, options],\n\t);\n\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, handleStateChange);\n\t}, [getter, handleStateChange]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tconst state = getter.get();\n\t\tconst previousState = {} as TState;\n\n\t\tpopStateRef.current = true;\n\n\t\thandleStateChange(state, previousState);\n\n\t\tpopStateRef.current = false;\n\t}, [getter, popStateRef, handleStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\tserializer: IStateSerializer<TState>,\n\toptions: ISaveStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, (state) => {\n\t\t\tif (popStateRef.current) return;\n\n\t\t\tserializer.serialize(state);\n\n\t\t\toptions.onStateSave?.({ state: state });\n\t\t});\n\t}, [getter, popStateRef, serializer, options]);\n};\n\nexport const useStateHandler = <TState,>(\n\tcodec: IStateCodec<TState>,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\t// Whether currently processing popstate. This is to prevent adding the previous state to history.\n\tconst popStateRef = useRef(false);\n\n\tuseRestoreState(popStateRef, codec, validator, accessor, options);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, accessor, options);\n\n\tuseSaveState(popStateRef, accessor, codec, options);\n};\n"],"names":["useRestoreState","popStateRef","deserializer","validator","setter","options","useEffect","state","useHandleStateChange","getter","handleStateChange","useCallback","previousState","diff","omitBy","v","k","isEqual","keys","reaction","useSaveState","serializer","useStateHandler","codec","accessor","useRef"],"mappings":";;;AAoBA,MAAMA,IAAkB,CACvBC,GACAC,GACAC,GACAC,GACAC,MACU;AACV,EAAAC,EAAU,MAAM;AACf,UAAMC,IAAQL,EAAa,YAAA;AAE3B,IAAIC,EAAUI,CAAK,MAClBN,EAAY,UAAU,IAEtBG,EAAO,IAAIG,CAAK,GAEhBN,EAAY,UAAU,IAEtBI,EAAQ,iBAAiB,EAAE,OAAAE,GAAc;AAAA,EAE3C,GAAG,CAACL,GAAcC,GAAWF,GAAaG,GAAQC,CAAO,CAAC;AAC3D,GAEMG,IAAuB,CAC5BP,GACAQ,GACAJ,MACU;AACV,QAAMK,IAAoBC;AAAA,IACzB,CAACJ,GAAeK,MAAgC;AAC/C,UAAI,CAACP,EAAQ,cAAe;AAG5B,YAAMQ,IAAOC;AAAA,QAAOP;AAAA,QAAO,CAACQ,GAAGC,MAC9BC,EAAQL,EAAcI,CAA+B,GAAGD,CAAC;AAAA,MAAA,GAKpDG,IAAO,OAAO,KAAKL,CAAI;AAC7B,cAAQ,OAAOK,EAAK,SAAS,CAAC,GAE9Bb,EAAQ,cAAc;AAAA,QACrB,OAAAE;AAAA,QACA,eAAAK;AAAA,QACA,MAAAM;AAAA,QACA,UAAUjB,EAAY;AAAA,MAAA,CACtB;AAAA,IACF;AAAA,IACA,CAACA,GAAaI,CAAO;AAAA,EAAA;AAGtB,EAAAC,EAAU,MAEFa,EAASV,EAAO,KAAKC,CAAiB,GAC3C,CAACD,GAAQC,CAAiB,CAAC,GAG9BJ,EAAU,MAAM;AACf,UAAMC,IAAQE,EAAO,IAAA,GACfG,IAAgB,CAAA;AAEtB,IAAAX,EAAY,UAAU,IAEtBS,EAAkBH,GAAOK,CAAa,GAEtCX,EAAY,UAAU;AAAA,EACvB,GAAG,CAACQ,GAAQR,GAAaS,CAAiB,CAAC;AAC5C,GAEMU,IAAe,CACpBnB,GACAQ,GACAY,GACAhB,MACU;AACV,EAAAC,EAAU,MAEFa,EAASV,EAAO,KAAK,CAACF,MAAU;AACtC,IAAIN,EAAY,YAEhBoB,EAAW,UAAUd,CAAK,GAE1BF,EAAQ,cAAc,EAAE,OAAAE,GAAc;AAAA,EACvC,CAAC,GACC,CAACE,GAAQR,GAAaoB,GAAYhB,CAAO,CAAC;AAC9C,GAEaiB,IAAkB,CAC9BC,GACApB,GACAqB,GACAnB,MACU;AAEV,QAAMJ,IAAcwB,EAAO,EAAK;AAEhC,EAAAzB,EAAgBC,GAAasB,GAAOpB,GAAWqB,GAAUnB,CAAO,GAGhEG,EAAqBP,GAAauB,GAAUnB,CAAO,GAEnDe,EAAanB,GAAauB,GAAUD,GAAOlB,CAAO;AACnD;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";const l=require("lodash-es"),f=require("mobx"),c=require("react"),E=(e,u,s,t,n)=>{c.useEffect(()=>{const r=u.deserialize();s(r)&&(e.current=!0,t.set(r),e.current=!1,n.onStateRestore?.({state:r}))},[u,s,e,t,n])},i=(e,u,s)=>{const t=c.useCallback((n,r)=>{if(!s.onStateChange)return;const h=l.omitBy(n,(S,o)=>l.isEqual(r[o],S)),a=Object.keys(h);console.assert(a.length>0),s.onStateChange({state:n,previousState:r,keys:a,popState:e.current})},[e,s]);c.useEffect(()=>f.reaction(u.get,t),[u,t]),c.useEffect(()=>{const n=u.get(),r={};e.current=!0,t(n,r),e.current=!1},[u,e,t])},C=(e,u,s,t)=>{c.useEffect(()=>f.reaction(u.get,n=>{e.current||(s.serialize(n),t.onStateSave?.({state:n}))}),[u,e,s,t])},d=(e,u,s,t)=>{const n=c.useRef(!1);E(n,e,u,s,t),i(n,s,t),C(n,s,e,t)};exports.useStateHandler=d;
|
|
2
|
-
//# sourceMappingURL=useStateHandler-Ff_CnvN8.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useStateHandler-Ff_CnvN8.cjs","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type {\n\tIStateAccessor,\n\tIStateGetter,\n\tIStateSetter,\n} from '@/components/IStateAccessor';\nimport type {\n\tIStateCodec,\n\tIStateDeserializer,\n\tIStateSerializer,\n} from '@/components/IStateCodec';\nimport type {\n\tIHandleStateChangeOptions,\n\tIRestoreStateOptions,\n\tISaveStateOptions,\n\tIStateHandlerOptions,\n} from '@/components/IStateHandlerOptions';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useCallback, useEffect, useRef } from 'react';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: IStateDeserializer /*<TState>*/,\n\tvalidator: (state: unknown) => state is TState,\n\tsetter: IStateSetter<TState>,\n\toptions: IRestoreStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\tconst state = deserializer.deserialize();\n\n\t\tif (validator(state)) {\n\t\t\tpopStateRef.current = true;\n\n\t\t\tsetter.set(state);\n\n\t\t\tpopStateRef.current = false;\n\n\t\t\toptions.onStateRestore?.({ state: state });\n\t\t}\n\t}, [deserializer, validator, popStateRef, setter, options]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\toptions: IHandleStateChangeOptions<TState>,\n): void => {\n\tconst handleStateChange = useCallback(\n\t\t(state: TState, previousState: TState): void => {\n\t\t\tif (!options.onStateChange) return;\n\n\t\t\t// Compare the current and previous values.\n\t\t\tconst diff = omitBy(state, (v, k) =>\n\t\t\t\tisEqual(previousState[k as keyof typeof previousState], v),\n\t\t\t);\n\n\t\t\t// Assuming that the current value is `{ filter: 'Miku', page: 3939, searchType: 'Artist' }`, and the previous one is `{ filter: 'Miku', page: 1 }`,\n\t\t\t// then the diff will be `{ page: 3939, searchType: 'Artist' }`, which results in `['page', 'searchType']`.\n\t\t\tconst keys = Object.keys(diff) as (keyof TState)[];\n\t\t\tconsole.assert(keys.length > 0);\n\n\t\t\toptions.onStateChange({\n\t\t\t\tstate: state,\n\t\t\t\tpreviousState: previousState,\n\t\t\t\tkeys: keys,\n\t\t\t\tpopState: popStateRef.current,\n\t\t\t});\n\t\t},\n\t\t[popStateRef, options],\n\t);\n\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, handleStateChange);\n\t}, [getter, handleStateChange]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tconst state = getter.get();\n\t\tconst previousState = {} as TState;\n\n\t\tpopStateRef.current = true;\n\n\t\thandleStateChange(state, previousState);\n\n\t\tpopStateRef.current = false;\n\t}, [getter, popStateRef, handleStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\tserializer: IStateSerializer<TState>,\n\toptions: ISaveStateOptions<TState>,\n): void => {\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, (state) => {\n\t\t\tif (popStateRef.current) return;\n\n\t\t\tserializer.serialize(state);\n\n\t\t\toptions.onStateSave?.({ state: state });\n\t\t});\n\t}, [getter, popStateRef, serializer, options]);\n};\n\nexport const useStateHandler = <TState,>(\n\tcodec: IStateCodec<TState>,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\toptions: IStateHandlerOptions<TState>,\n): void => {\n\t// Whether currently processing popstate. This is to prevent adding the previous state to history.\n\tconst popStateRef = useRef(false);\n\n\tuseRestoreState(popStateRef, codec, validator, accessor, options);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, accessor, options);\n\n\tuseSaveState(popStateRef, accessor, codec, options);\n};\n"],"names":["useRestoreState","popStateRef","deserializer","validator","setter","options","useEffect","state","useHandleStateChange","getter","handleStateChange","useCallback","previousState","diff","omitBy","v","k","isEqual","keys","reaction","useSaveState","serializer","useStateHandler","codec","accessor","useRef"],"mappings":"+EAoBMA,EAAkB,CACvBC,EACAC,EACAC,EACAC,EACAC,IACU,CACVC,EAAAA,UAAU,IAAM,CACf,MAAMC,EAAQL,EAAa,YAAA,EAEvBC,EAAUI,CAAK,IAClBN,EAAY,QAAU,GAEtBG,EAAO,IAAIG,CAAK,EAEhBN,EAAY,QAAU,GAEtBI,EAAQ,iBAAiB,CAAE,MAAAE,EAAc,EAE3C,EAAG,CAACL,EAAcC,EAAWF,EAAaG,EAAQC,CAAO,CAAC,CAC3D,EAEMG,EAAuB,CAC5BP,EACAQ,EACAJ,IACU,CACV,MAAMK,EAAoBC,EAAAA,YACzB,CAACJ,EAAeK,IAAgC,CAC/C,GAAI,CAACP,EAAQ,cAAe,OAG5B,MAAMQ,EAAOC,EAAAA,OAAOP,EAAO,CAACQ,EAAGC,IAC9BC,EAAAA,QAAQL,EAAcI,CAA+B,EAAGD,CAAC,CAAA,EAKpDG,EAAO,OAAO,KAAKL,CAAI,EAC7B,QAAQ,OAAOK,EAAK,OAAS,CAAC,EAE9Bb,EAAQ,cAAc,CACrB,MAAAE,EACA,cAAAK,EACA,KAAAM,EACA,SAAUjB,EAAY,OAAA,CACtB,CACF,EACA,CAACA,EAAaI,CAAO,CAAA,EAGtBC,EAAAA,UAAU,IAEFa,WAASV,EAAO,IAAKC,CAAiB,EAC3C,CAACD,EAAQC,CAAiB,CAAC,EAG9BJ,EAAAA,UAAU,IAAM,CACf,MAAMC,EAAQE,EAAO,IAAA,EACfG,EAAgB,CAAA,EAEtBX,EAAY,QAAU,GAEtBS,EAAkBH,EAAOK,CAAa,EAEtCX,EAAY,QAAU,EACvB,EAAG,CAACQ,EAAQR,EAAaS,CAAiB,CAAC,CAC5C,EAEMU,EAAe,CACpBnB,EACAQ,EACAY,EACAhB,IACU,CACVC,EAAAA,UAAU,IAEFa,WAASV,EAAO,IAAMF,GAAU,CAClCN,EAAY,UAEhBoB,EAAW,UAAUd,CAAK,EAE1BF,EAAQ,cAAc,CAAE,MAAAE,EAAc,EACvC,CAAC,EACC,CAACE,EAAQR,EAAaoB,EAAYhB,CAAO,CAAC,CAC9C,EAEaiB,EAAkB,CAC9BC,EACApB,EACAqB,EACAnB,IACU,CAEV,MAAMJ,EAAcwB,EAAAA,OAAO,EAAK,EAEhCzB,EAAgBC,EAAasB,EAAOpB,EAAWqB,EAAUnB,CAAO,EAGhEG,EAAqBP,EAAauB,EAAUnB,CAAO,EAEnDe,EAAanB,EAAauB,EAAUD,EAAOlB,CAAO,CACnD"}
|