@aigamo/route-sphere 2.1.0-4 → 2.1.0-6
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/components/IStateAccessor.d.ts +7 -0
- package/dist/components/IStateCodec.d.ts +7 -0
- package/dist/components/useStateHandler.d.ts +3 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +45 -38
- package/dist/index.es.js.map +1 -1
- package/dist/react-router.cjs.js +1 -1
- package/dist/react-router.cjs.js.map +1 -1
- package/dist/react-router.es.js +33 -30
- package/dist/react-router.es.js.map +1 -1
- package/dist/tanstack-router.cjs.js +1 -1
- package/dist/tanstack-router.cjs.js.map +1 -1
- package/dist/tanstack-router.es.js +30 -27
- package/dist/tanstack-router.es.js.map +1 -1
- package/dist/useStateHandler-B54QKldW.cjs +2 -0
- package/dist/useStateHandler-B54QKldW.cjs.map +1 -0
- package/dist/useStateHandler-Cmr_YOo4.js +39 -0
- package/dist/useStateHandler-Cmr_YOo4.js.map +1 -0
- package/package.json +1 -1
- package/dist/useStateHandler-BKLDeBsK.cjs +0 -2
- package/dist/useStateHandler-BKLDeBsK.cjs.map +0 -1
- package/dist/useStateHandler-CdocwC8x.js +0 -39
- package/dist/useStateHandler-CdocwC8x.js.map +0 -1
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { StateChangeEvent } from '../stores/StateChangeEvent';
|
|
2
|
-
|
|
2
|
+
import { IStateAccessor } from './IStateAccessor';
|
|
3
|
+
import { IStateCodec } from './IStateCodec';
|
|
4
|
+
export declare const useStateHandler: <TState>(codec: IStateCodec<TState>, validator: (state: unknown) => state is TState, accessor: IStateAccessor<TState>, onStateChange: ((event: StateChangeEvent<TState>) => void) | undefined) => void;
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react"),n=require("react-router-dom"),c=require("./useStateHandler-B54QKldW.cjs"),l=()=>{const e=n.useLocation();return r.useLayoutEffect(()=>{window.scrollTo(0,0)},[e]),null},u=e=>r.useCallback(()=>{try{return JSON.parse(window.localStorage.getItem(e)??JSON.stringify({}))}catch(t){return console.error(t),{}}},[e]),S=e=>r.useCallback(t=>{window.localStorage.setItem(e,JSON.stringify(t))},[e]),i=e=>{const t=u(e),o=S(e);return r.useMemo(()=>({deserialize:t,serialize:o}),[t,o])},d=(e,t,o,a)=>{const s=i(e);c.useStateHandler(s,t,o,a)},g=e=>r.useCallback(()=>e.state,[e]),L=e=>r.useCallback(t=>{e.state=t},[e]),m=e=>{const t=g(e),o=L(e);return r.useMemo(()=>({get:t,set:o}),[t,o])},b=(e,t)=>{const o=m(t);d(e,t.validateState,o,t.onStateChange)},w=(e,t)=>t.some(o=>e.includes(o));exports.useStateHandler=c.useStateHandler;exports.ScrollToTop=l;exports.includesAny=w;exports.useLocalStorageState=b;
|
|
2
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 { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { useCallback } from 'react';\n\nimport { useStateHandler } from './useStateHandler';\n\nconst useLocalStorageStateDeserializer = (key: string): (() =>
|
|
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 { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { useCallback, useMemo } from 'react';\n\nimport type { IStateAccessor } from './IStateAccessor';\nimport type { IStateCodec } from './IStateCodec';\nimport { useStateHandler } from './useStateHandler';\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\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n): void => {\n\tconst codec = useLocalStorageStateCodec(key);\n\tuseStateHandler(codec, validator, accessor, onStateChange);\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\tuseLocalStorageStateHandler(\n\t\tkey,\n\t\tstore.validateState,\n\t\taccessor,\n\t\tstore.onStateChange,\n\t);\n};\n","export const includesAny = <T>(array: T[], values: T[]): boolean => {\n\treturn values.some((value) => array.includes(value));\n};\n"],"names":["ScrollToTop","location","useLocation","useLayoutEffect","useLocalStorageStateDeserializer","key","useCallback","error","useLocalStorageStateSerializer","state","useLocalStorageStateCodec","deserializer","serializer","useMemo","useLocalStorageStateHandler","validator","accessor","onStateChange","codec","useStateHandler","useLocalStorageStateGetter","store","useLocalStorageStateSetter","useLocalStorageStateAccessor","getter","setter","useLocalStorageState","includesAny","array","values","value"],"mappings":"mLAGaA,EAAc,IAAY,CACtC,MAAMC,EAAWC,EAAAA,YAAA,EAEjBC,OAAAA,EAAAA,gBAAgB,IAAM,CACrB,OAAO,SAAS,EAAG,CAAC,CACrB,EAAG,CAACF,CAAQ,CAAC,EAEN,IACR,ECHMG,EAAoCC,GAClCC,EAAAA,YAAY,IAAe,CACjC,GAAI,CACH,OAAO,KAAK,MACX,OAAO,aAAa,QAAQD,CAAG,GAAK,KA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
|
3
|
-
import { u
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
return
|
|
1
|
+
import { useLayoutEffect as n, useMemo as c, useCallback as r } from "react";
|
|
2
|
+
import { useLocation as l } from "react-router-dom";
|
|
3
|
+
import { u } from "./useStateHandler-Cmr_YOo4.js";
|
|
4
|
+
const C = () => {
|
|
5
|
+
const e = l();
|
|
6
|
+
return n(() => {
|
|
7
7
|
window.scrollTo(0, 0);
|
|
8
|
-
}, [
|
|
9
|
-
},
|
|
8
|
+
}, [e]), null;
|
|
9
|
+
}, i = (e) => r(() => {
|
|
10
10
|
try {
|
|
11
11
|
return JSON.parse(
|
|
12
|
-
window.localStorage.getItem(
|
|
12
|
+
window.localStorage.getItem(e) ?? JSON.stringify({})
|
|
13
13
|
);
|
|
14
|
-
} catch (
|
|
15
|
-
return console.error(
|
|
14
|
+
} catch (t) {
|
|
15
|
+
return console.error(t), {};
|
|
16
16
|
}
|
|
17
|
-
}, [
|
|
18
|
-
(
|
|
19
|
-
window.localStorage.setItem(
|
|
17
|
+
}, [e]), S = (e) => r(
|
|
18
|
+
(t) => {
|
|
19
|
+
window.localStorage.setItem(e, JSON.stringify(t));
|
|
20
20
|
},
|
|
21
|
-
[
|
|
22
|
-
),
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
c
|
|
21
|
+
[e]
|
|
22
|
+
), g = (e) => {
|
|
23
|
+
const t = i(e), o = S(e);
|
|
24
|
+
return c(
|
|
25
|
+
() => ({
|
|
26
|
+
deserialize: t,
|
|
27
|
+
serialize: o
|
|
28
|
+
}),
|
|
29
|
+
[t, o]
|
|
31
30
|
);
|
|
32
|
-
},
|
|
33
|
-
(e)
|
|
34
|
-
|
|
31
|
+
}, d = (e, t, o, a) => {
|
|
32
|
+
const s = g(e);
|
|
33
|
+
u(s, t, o, a);
|
|
34
|
+
}, m = (e) => r(() => e.state, [e]), L = (e) => r(
|
|
35
|
+
(t) => {
|
|
36
|
+
e.state = t;
|
|
35
37
|
},
|
|
36
|
-
[
|
|
37
|
-
),
|
|
38
|
-
const
|
|
38
|
+
[e]
|
|
39
|
+
), f = (e) => {
|
|
40
|
+
const t = m(e), o = L(e);
|
|
41
|
+
return c(
|
|
42
|
+
() => ({ get: t, set: o }),
|
|
43
|
+
[t, o]
|
|
44
|
+
);
|
|
45
|
+
}, J = (e, t) => {
|
|
46
|
+
const o = f(t);
|
|
39
47
|
d(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
a
|
|
48
|
+
e,
|
|
49
|
+
t.validateState,
|
|
50
|
+
o,
|
|
51
|
+
t.onStateChange
|
|
45
52
|
);
|
|
46
|
-
}, N = (
|
|
53
|
+
}, N = (e, t) => t.some((o) => e.includes(o));
|
|
47
54
|
export {
|
|
48
|
-
|
|
55
|
+
C as ScrollToTop,
|
|
49
56
|
N as includesAny,
|
|
50
57
|
J as useLocalStorageState,
|
|
51
|
-
|
|
58
|
+
u as useStateHandler
|
|
52
59
|
};
|
|
53
60
|
//# 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 { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { useCallback } from 'react';\n\nimport { useStateHandler } from './useStateHandler';\n\nconst useLocalStorageStateDeserializer = (key: string): (() =>
|
|
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 { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { useCallback, useMemo } from 'react';\n\nimport type { IStateAccessor } from './IStateAccessor';\nimport type { IStateCodec } from './IStateCodec';\nimport { useStateHandler } from './useStateHandler';\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\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n): void => {\n\tconst codec = useLocalStorageStateCodec(key);\n\tuseStateHandler(codec, validator, accessor, onStateChange);\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\tuseLocalStorageStateHandler(\n\t\tkey,\n\t\tstore.validateState,\n\t\taccessor,\n\t\tstore.onStateChange,\n\t);\n};\n","export const includesAny = <T>(array: T[], values: T[]): boolean => {\n\treturn values.some((value) => array.includes(value));\n};\n"],"names":["ScrollToTop","location","useLocation","useLayoutEffect","useLocalStorageStateDeserializer","key","useCallback","error","useLocalStorageStateSerializer","state","useLocalStorageStateCodec","deserializer","serializer","useMemo","useLocalStorageStateHandler","validator","accessor","onStateChange","codec","useStateHandler","useLocalStorageStateGetter","store","useLocalStorageStateSetter","useLocalStorageStateAccessor","getter","setter","useLocalStorageState","includesAny","array","values","value"],"mappings":";;;AAGO,MAAMA,IAAc,MAAY;AACtC,QAAMC,IAAWC,EAAA;AAEjB,SAAAC,EAAgB,MAAM;AACrB,WAAO,SAAS,GAAG,CAAC;AAAA,EACrB,GAAG,CAACF,CAAQ,CAAC,GAEN;AACR,GCHMG,IAAmC,CAACC,MAClCC,EAAY,MAAe;AACjC,MAAI;AACH,WAAO,KAAK;AAAA,MACX,OAAO,aAAa,QAAQD,CAAG,KAAK,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,CAAIC,GAAYC,MACnCA,EAAO,KAAK,CAACC,MAAUF,EAAM,SAASE,CAAK,CAAC;"}
|
package/dist/react-router.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./useStateHandler-B54QKldW.cjs"),o=require("qs"),s=require("react"),n=require("react-router-dom"),i=()=>{const e=n.useLocation();return s.useCallback(()=>o.parse(e.search.slice(1)),[e])},u=()=>{const e=n.useNavigate();return s.useCallback(async t=>{const a=`?${o.stringify(t)}`;await e(a)},[e])},l=()=>{const e=i(),t=u();return s.useMemo(()=>({deserialize:e,serialize:t}),[e,t])},S=(e,t,a)=>{const c=l();r.useStateHandler(c,e,t,a)},d=e=>s.useCallback(()=>e.state,[e]),L=e=>s.useCallback(t=>{e.state=t},[e]),g=e=>{const t=d(e),a=L(e);return s.useMemo(()=>({get:t,set:a}),[t,a])},b=e=>{const t=g(e);S(e.validateState,t,e.onStateChange)};exports.useLocationState=b;
|
|
2
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 { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { type ParsedQs, parse, stringify } from 'qs';\nimport { useCallback } 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\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\
|
|
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 { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\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\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, onStateChange);\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\tuseLocationStateHandler(store.validateState, accessor, store.onStateChange);\n};\n"],"names":["useLocationStateDeserializer","location","useLocation","useCallback","parse","useLocationStateSerializer","navigate","useNavigate","state","newUrl","stringify","useLocationStateCodec","deserializer","serializer","useMemo","useLocationStateHandler","validator","accessor","onStateChange","codec","useStateHandler","useLocationStateGetter","store","useLocationStateSetter","useLocationStateAccessor","getter","setter","useLocationState"],"mappings":"mMASMA,EAA+B,IAAwB,CAC5D,MAAMC,EAAWC,EAAAA,YAAA,EAGjB,OAAOC,EAAAA,YACN,IAAgBC,EAAAA,MAAMH,EAAS,OAAO,MAAM,CAAC,CAAC,EAC9C,CAACA,CAAQ,CAAA,CAEX,EAEMI,EAA6B,IAA0C,CAC5E,MAAMC,EAAWC,EAAAA,YAAA,EAEjB,OAAOJ,EAAAA,YACN,MAAOK,GAAiC,CACvC,MAAMC,EAAS,IAAIC,EAAAA,UAAUF,CAAK,CAAC,GACnC,MAAMF,EAASG,CAAM,CACtB,EACA,CAACH,CAAQ,CAAA,CAEX,EAEMK,EAAwB,IAAoC,CACjE,MAAMC,EAAeZ,EAAA,EACfa,EAAaR,EAAA,EAQnB,OAPcS,EAAAA,QACb,KAA4B,CAC3B,YAAaF,EACb,UAAWC,CAAA,GAEZ,CAACD,EAAcC,CAAU,CAAA,CAG3B,EAGME,EAA0B,CAC/BC,EACAC,EACAC,IACU,CACV,MAAMC,EAAQR,EAAA,EACdS,EAAAA,gBAAgBD,EAAOH,EAAWC,EAAUC,CAAa,CAC1D,EAEMG,EACLC,GAEOnB,EAAAA,YAAY,IAAcmB,EAAM,MAAO,CAACA,CAAK,CAAC,EAGhDC,EACLD,GAEOnB,EAAAA,YACLK,GAAwB,CACxBc,EAAM,MAAQd,CACf,EACA,CAACc,CAAK,CAAA,EAIFE,EACLF,GAC4B,CAC5B,MAAMG,EAASJ,EAAuBC,CAAK,EACrCI,EAASH,EAAuBD,CAAK,EAK3C,OAJiBR,EAAAA,QAChB,KAA+B,CAAE,IAAKW,EAAQ,IAAKC,CAAA,GACnD,CAACD,EAAQC,CAAM,CAAA,CAGjB,EAGaC,EAA6BL,GAAqC,CAC9E,MAAML,EAAWO,EAAyBF,CAAK,EAC/CP,EAAwBO,EAAM,cAAeL,EAAUK,EAAM,aAAa,CAC3E"}
|
package/dist/react-router.es.js
CHANGED
|
@@ -1,45 +1,48 @@
|
|
|
1
|
-
import { u as
|
|
2
|
-
import { parse as
|
|
3
|
-
import { useCallback as
|
|
4
|
-
import { useLocation as
|
|
5
|
-
const
|
|
6
|
-
const t =
|
|
7
|
-
return
|
|
8
|
-
() =>
|
|
1
|
+
import { u as n } from "./useStateHandler-Cmr_YOo4.js";
|
|
2
|
+
import { parse as r, stringify as i } from "qs";
|
|
3
|
+
import { useMemo as c, useCallback as s } from "react";
|
|
4
|
+
import { useLocation as u, useNavigate as l } from "react-router-dom";
|
|
5
|
+
const S = () => {
|
|
6
|
+
const t = u();
|
|
7
|
+
return s(
|
|
8
|
+
() => r(t.search.slice(1)),
|
|
9
9
|
[t]
|
|
10
10
|
);
|
|
11
|
-
},
|
|
12
|
-
const t =
|
|
13
|
-
return
|
|
11
|
+
}, d = () => {
|
|
12
|
+
const t = l();
|
|
13
|
+
return s(
|
|
14
14
|
async (e) => {
|
|
15
|
-
const
|
|
16
|
-
await t(
|
|
15
|
+
const o = `?${i(e)}`;
|
|
16
|
+
await t(o);
|
|
17
17
|
},
|
|
18
18
|
[t]
|
|
19
19
|
);
|
|
20
|
-
},
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
r
|
|
20
|
+
}, m = () => {
|
|
21
|
+
const t = S(), e = d();
|
|
22
|
+
return c(
|
|
23
|
+
() => ({
|
|
24
|
+
deserialize: t,
|
|
25
|
+
serialize: e
|
|
26
|
+
}),
|
|
27
|
+
[t, e]
|
|
29
28
|
);
|
|
30
|
-
},
|
|
29
|
+
}, L = (t, e, o) => {
|
|
30
|
+
const a = m();
|
|
31
|
+
n(a, t, e, o);
|
|
32
|
+
}, g = (t) => s(() => t.state, [t]), p = (t) => s(
|
|
31
33
|
(e) => {
|
|
32
34
|
t.state = e;
|
|
33
35
|
},
|
|
34
36
|
[t]
|
|
35
|
-
),
|
|
36
|
-
const e =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
e,
|
|
40
|
-
t.onStateChange,
|
|
41
|
-
a
|
|
37
|
+
), z = (t) => {
|
|
38
|
+
const e = g(t), o = p(t);
|
|
39
|
+
return c(
|
|
40
|
+
() => ({ get: e, set: o }),
|
|
41
|
+
[e, o]
|
|
42
42
|
);
|
|
43
|
+
}, w = (t) => {
|
|
44
|
+
const e = z(t);
|
|
45
|
+
L(t.validateState, e, t.onStateChange);
|
|
43
46
|
};
|
|
44
47
|
export {
|
|
45
48
|
w as useLocationState
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react-router.es.js","sources":["../src/react-router/useLocationState.tsx"],"sourcesContent":["import { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { type ParsedQs, parse, stringify } from 'qs';\nimport { useCallback } 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\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\
|
|
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 { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\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\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, onStateChange);\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\tuseLocationStateHandler(store.validateState, accessor, store.onStateChange);\n};\n"],"names":["useLocationStateDeserializer","location","useLocation","useCallback","parse","useLocationStateSerializer","navigate","useNavigate","state","newUrl","stringify","useLocationStateCodec","deserializer","serializer","useMemo","useLocationStateHandler","validator","accessor","onStateChange","codec","useStateHandler","useLocationStateGetter","store","useLocationStateSetter","useLocationStateAccessor","getter","setter","useLocationState"],"mappings":";;;;AASA,MAAMA,IAA+B,MAAwB;AAC5D,QAAMC,IAAWC,EAAA;AAGjB,SAAOC;AAAA,IACN,MAAgBC,EAAMH,EAAS,OAAO,MAAM,CAAC,CAAC;AAAA,IAC9C,CAACA,CAAQ;AAAA,EAAA;AAEX,GAEMI,IAA6B,MAA0C;AAC5E,QAAMC,IAAWC,EAAA;AAEjB,SAAOJ;AAAA,IACN,OAAOK,MAAiC;AACvC,YAAMC,IAAS,IAAIC,EAAUF,CAAK,CAAC;AACnC,YAAMF,EAASG,CAAM;AAAA,IACtB;AAAA,IACA,CAACH,CAAQ;AAAA,EAAA;AAEX,GAEMK,IAAwB,MAAoC;AACjE,QAAMC,IAAeZ,EAAA,GACfa,IAAaR,EAAA;AAQnB,SAPcS;AAAA,IACb,OAA4B;AAAA,MAC3B,aAAaF;AAAA,MACb,WAAWC;AAAA,IAAA;AAAA,IAEZ,CAACD,GAAcC,CAAU;AAAA,EAAA;AAG3B,GAGME,IAA0B,CAC/BC,GACAC,GACAC,MACU;AACV,QAAMC,IAAQR,EAAA;AACd,EAAAS,EAAgBD,GAAOH,GAAWC,GAAUC,CAAa;AAC1D,GAEMG,IAAyB,CAC9BC,MAEOnB,EAAY,MAAcmB,EAAM,OAAO,CAACA,CAAK,CAAC,GAGhDC,IAAyB,CAC9BD,MAEOnB;AAAA,EACN,CAACK,MAAwB;AACxB,IAAAc,EAAM,QAAQd;AAAA,EACf;AAAA,EACA,CAACc,CAAK;AAAA,GAIFE,IAA2B,CAChCF,MAC4B;AAC5B,QAAMG,IAASJ,EAAuBC,CAAK,GACrCI,IAASH,EAAuBD,CAAK;AAK3C,SAJiBR;AAAA,IAChB,OAA+B,EAAE,KAAKW,GAAQ,KAAKC,EAAA;AAAA,IACnD,CAACD,GAAQC,CAAM;AAAA,EAAA;AAGjB,GAGaC,IAAmB,CAAUL,MAAqC;AAC9E,QAAML,IAAWO,EAAyBF,CAAK;AAC/C,EAAAP,EAAwBO,EAAM,eAAeL,GAAUK,EAAM,aAAa;AAC3E;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./useStateHandler-B54QKldW.cjs"),o=require("@tanstack/react-router"),r=require("qs"),a=require("react"),i=()=>{const e=o.useLocation();return a.useCallback(()=>r.parse(e.searchStr.slice(1)),[e])},u=()=>{const e=o.useNavigate();return a.useCallback(async t=>{await e({search:t})},[e])},l=()=>{const e=i(),t=u();return a.useMemo(()=>({deserialize:e,serialize:t}),[e,t])},S=(e,t,s)=>{const c=l();n.useStateHandler(c,e,t,s)},d=e=>a.useCallback(()=>e.state,[e]),L=e=>a.useCallback(t=>{e.state=t},[e]),g=e=>{const t=d(e),s=L(e);return a.useMemo(()=>({get:t,set:s}),[t,s])},b=e=>{const t=g(e);S(e.validateState,t,e.onStateChange)};exports.useLocationState=b;
|
|
2
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 { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { useLocation, useNavigate } from '@tanstack/react-router';\nimport { type ParsedQs, parse } from 'qs';\nimport { useCallback } 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\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\
|
|
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 { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\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\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, onStateChange);\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\tuseLocationStateHandler(store.validateState, accessor, store.onStateChange);\n};\n"],"names":["useLocationStateDeserializer","location","useLocation","useCallback","parse","useLocationStateSerializer","navigate","useNavigate","state","useLocationStateCodec","deserializer","serializer","useMemo","useLocationStateHandler","validator","accessor","onStateChange","codec","useStateHandler","useLocationStateGetter","store","useLocationStateSetter","useLocationStateAccessor","getter","setter","useLocationState"],"mappings":"yMASMA,EAA+B,IAAwB,CAC5D,MAAMC,EAAWC,EAAAA,YAAA,EAGjB,OAAOC,EAAAA,YACN,IAAgBC,EAAAA,MAAMH,EAAS,UAAU,MAAM,CAAC,CAAC,EACjD,CAACA,CAAQ,CAAA,CAEX,EAEMI,EAA6B,IAA0C,CAC5E,MAAMC,EAAWC,EAAAA,YAAA,EAEjB,OAAOJ,EAAAA,YACN,MAAOK,GAAiC,CACvC,MAAMF,EAAS,CAAE,OAAQE,CAAA,CAA0B,CACpD,EACA,CAACF,CAAQ,CAAA,CAEX,EAEMG,EAAwB,IAAoC,CACjE,MAAMC,EAAeV,EAAA,EACfW,EAAaN,EAAA,EAQnB,OAPcO,EAAAA,QACb,KAA4B,CAC3B,YAAaF,EACb,UAAWC,CAAA,GAEZ,CAACD,EAAcC,CAAU,CAAA,CAG3B,EAGME,EAA0B,CAC/BC,EACAC,EACAC,IACU,CACV,MAAMC,EAAQR,EAAA,EACdS,EAAAA,gBAAgBD,EAAOH,EAAWC,EAAUC,CAAa,CAC1D,EAEMG,EACLC,GAEOjB,EAAAA,YAAY,IAAMiB,EAAM,MAAO,CAACA,CAAK,CAAC,EAGxCC,EACLD,GAEOjB,EAAAA,YACLK,GAAkB,CAClBY,EAAM,MAAQZ,CACf,EACA,CAACY,CAAK,CAAA,EAIFE,EACLF,GAC4B,CAC5B,MAAMG,EAASJ,EAAuBC,CAAK,EACrCI,EAASH,EAAuBD,CAAK,EAK3C,OAJiBR,EAAAA,QAChB,KAA+B,CAAE,IAAKW,EAAQ,IAAKC,CAAA,GACnD,CAACD,EAAQC,CAAM,CAAA,CAGjB,EAGaC,EAA6BL,GAAqC,CAC9E,MAAML,EAAWO,EAAyBF,CAAK,EAC/CP,EAAwBO,EAAM,cAAeL,EAAUK,EAAM,aAAa,CAC3E"}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { u as
|
|
2
|
-
import { useLocation as
|
|
3
|
-
import { parse as
|
|
4
|
-
import { useCallback as a } from "react";
|
|
5
|
-
const
|
|
6
|
-
const t =
|
|
1
|
+
import { u as n } from "./useStateHandler-Cmr_YOo4.js";
|
|
2
|
+
import { useLocation as r, useNavigate as i } from "@tanstack/react-router";
|
|
3
|
+
import { parse as u } from "qs";
|
|
4
|
+
import { useMemo as c, useCallback as a } from "react";
|
|
5
|
+
const S = () => {
|
|
6
|
+
const t = r();
|
|
7
7
|
return a(
|
|
8
|
-
() =>
|
|
8
|
+
() => u(t.searchStr.slice(1)),
|
|
9
9
|
[t]
|
|
10
10
|
);
|
|
11
|
-
},
|
|
12
|
-
const t =
|
|
11
|
+
}, l = () => {
|
|
12
|
+
const t = i();
|
|
13
13
|
return a(
|
|
14
14
|
async (e) => {
|
|
15
15
|
await t({
|
|
@@ -19,31 +19,34 @@ const l = () => {
|
|
|
19
19
|
},
|
|
20
20
|
[t]
|
|
21
21
|
);
|
|
22
|
-
},
|
|
23
|
-
const
|
|
24
|
-
c(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
s
|
|
22
|
+
}, d = () => {
|
|
23
|
+
const t = S(), e = l();
|
|
24
|
+
return c(
|
|
25
|
+
() => ({
|
|
26
|
+
deserialize: t,
|
|
27
|
+
serialize: e
|
|
28
|
+
}),
|
|
29
|
+
[t, e]
|
|
31
30
|
);
|
|
32
|
-
},
|
|
31
|
+
}, m = (t, e, o) => {
|
|
32
|
+
const s = d();
|
|
33
|
+
n(s, t, e, o);
|
|
34
|
+
}, L = (t) => a(() => t.state, [t]), p = (t) => a(
|
|
33
35
|
(e) => {
|
|
34
36
|
t.state = e;
|
|
35
37
|
},
|
|
36
38
|
[t]
|
|
37
|
-
),
|
|
38
|
-
const e =
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
e,
|
|
42
|
-
t.onStateChange,
|
|
43
|
-
o
|
|
39
|
+
), z = (t) => {
|
|
40
|
+
const e = L(t), o = p(t);
|
|
41
|
+
return c(
|
|
42
|
+
() => ({ get: e, set: o }),
|
|
43
|
+
[e, o]
|
|
44
44
|
);
|
|
45
|
+
}, C = (t) => {
|
|
46
|
+
const e = z(t);
|
|
47
|
+
m(t.validateState, e, t.onStateChange);
|
|
45
48
|
};
|
|
46
49
|
export {
|
|
47
|
-
|
|
50
|
+
C as useLocationState
|
|
48
51
|
};
|
|
49
52
|
//# sourceMappingURL=tanstack-router.es.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tanstack-router.es.js","sources":["../src/tanstack-router/useLocationState.tsx"],"sourcesContent":["import { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { useLocation, useNavigate } from '@tanstack/react-router';\nimport { type ParsedQs, parse } from 'qs';\nimport { useCallback } 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\n/** Updates a store that implements the {@link LocationStateStore} interface when a route changes, and vice versa. */\nconst useLocationStateHandler = <TState,>(\n\
|
|
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 { useStateHandler } from '@/components/useStateHandler';\nimport type { IStateStore } from '@/stores/IStateStore';\nimport type { StateChangeEvent } from '@/stores/StateChangeEvent';\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\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n): void => {\n\tconst codec = useLocationStateCodec();\n\tuseStateHandler(codec, validator, accessor, onStateChange);\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\tuseLocationStateHandler(store.validateState, accessor, store.onStateChange);\n};\n"],"names":["useLocationStateDeserializer","location","useLocation","useCallback","parse","useLocationStateSerializer","navigate","useNavigate","state","useLocationStateCodec","deserializer","serializer","useMemo","useLocationStateHandler","validator","accessor","onStateChange","codec","useStateHandler","useLocationStateGetter","store","useLocationStateSetter","useLocationStateAccess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AAeL,GAAUK,EAAM,aAAa;AAC3E;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";const f=require("lodash-es"),i=require("mobx"),c=require("react"),y=(t,e,s,u)=>{c.useEffect(()=>{const r=e.deserialize();s(r)&&(t.current=!0,u.set(r),t.current=!1)},[e,s,t,u])},a=(t,e,s)=>{c.useEffect(()=>{if(e)return i.reaction(s.get,(u,r)=>{const o=f.omitBy(u,(l,E)=>f.isEqual(r[E],l)),n=Object.keys(o);console.assert(n.length>0),e({keys:n,popState:t.current})})},[s,e,t]),c.useEffect(()=>{if(!e)return;const u=Object.keys(s.get());e({keys:u,popState:!0})},[s,e])},k=(t,e,s)=>{c.useEffect(()=>i.reaction(e.get,u=>{t.current||s.serialize(u)}),[e,t,s])},q=(t,e,s,u)=>{const r=c.useRef(!1);y(r,t,e,s),a(r,u,s),k(r,s,t)};exports.useStateHandler=q;
|
|
2
|
+
//# sourceMappingURL=useStateHandler-B54QKldW.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStateHandler-B54QKldW.cjs","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useEffect, useRef } from 'react';\n\nimport type {\n\tIStateAccessor,\n\tIStateGetter,\n\tIStateSetter,\n} from './IStateAccessor';\nimport type {\n\tIStateCodec,\n\tIStateDeserializer,\n\tIStateSerializer,\n} from './IStateCodec';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: IStateDeserializer /*<TState>*/,\n\tvalidator: (state: unknown) => state is TState,\n\tsetter: IStateSetter<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\t\t}\n\t}, [deserializer, validator, popStateRef, setter]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n\tgetter: IStateGetter<TState>,\n): void => {\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, (state, previousState) => {\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\tonStateChange({ keys: keys, popState: popStateRef.current });\n\t\t});\n\t}, [getter, onStateChange, popStateRef]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\tconst keys = Object.keys(getter.get()) as (keyof TState)[];\n\n\t\tonStateChange({ keys: keys, popState: true /* Always true. */ });\n\t}, [getter, onStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\tserializer: IStateSerializer<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\t\t});\n\t}, [getter, popStateRef, serializer]);\n};\n\nexport const useStateHandler = <TState,>(\n\tcodec: IStateCodec<TState>,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\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);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, onStateChange, accessor);\n\n\tuseSaveState(popStateRef, accessor, codec);\n};\n"],"names":["useRestoreState","popStateRef","deserializer","validator","setter","useEffect","state","useHandleStateChange","onStateChange","getter","reaction","previousState","diff","omitBy","v","k","isEqual","keys","useSaveState","serializer","useStateHandler","codec","accessor","useRef"],"mappings":"+EAgBMA,EAAkB,CACvBC,EACAC,EACAC,EACAC,IACU,CACVC,EAAAA,UAAU,IAAM,CACf,MAAMC,EAAQJ,EAAa,YAAA,EAEvBC,EAAUG,CAAK,IAClBL,EAAY,QAAU,GAEtBG,EAAO,IAAIE,CAAK,EAEhBL,EAAY,QAAU,GAExB,EAAG,CAACC,EAAcC,EAAWF,EAAaG,CAAM,CAAC,CAClD,EAEMG,EAAuB,CAC5BN,EACAO,EACAC,IACU,CACVJ,EAAAA,UAAU,IAAM,CACf,GAAKG,EAGL,OAAOE,EAAAA,SAASD,EAAO,IAAK,CAACH,EAAOK,IAAkB,CAErD,MAAMC,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,EAE9BT,EAAc,CAAE,KAAAS,EAAY,SAAUhB,EAAY,QAAS,CAC5D,CAAC,CACF,EAAG,CAACQ,EAAQD,EAAeP,CAAW,CAAC,EAGvCI,EAAAA,UAAU,IAAM,CACf,GAAI,CAACG,EAAe,OAEpB,MAAMS,EAAO,OAAO,KAAKR,EAAO,KAAK,EAErCD,EAAc,CAAE,KAAAS,EAAY,SAAU,EAAA,CAAyB,CAChE,EAAG,CAACR,EAAQD,CAAa,CAAC,CAC3B,EAEMU,EAAe,CACpBjB,EACAQ,EACAU,IACU,CACVd,EAAAA,UAAU,IAEFK,WAASD,EAAO,IAAMH,GAAU,CAClCL,EAAY,SAEhBkB,EAAW,UAAUb,CAAK,CAC3B,CAAC,EACC,CAACG,EAAQR,EAAakB,CAAU,CAAC,CACrC,EAEaC,EAAkB,CAC9BC,EACAlB,EACAmB,EACAd,IACU,CAEV,MAAMP,EAAcsB,EAAAA,OAAO,EAAK,EAEhCvB,EAAgBC,EAAaoB,EAAOlB,EAAWmB,CAAQ,EAGvDf,EAAqBN,EAAaO,EAAec,CAAQ,EAEzDJ,EAAajB,EAAaqB,EAAUD,CAAK,CAC1C"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { omitBy as l, isEqual as y } from "lodash-es";
|
|
2
|
+
import { reaction as f } from "mobx";
|
|
3
|
+
import { useRef as k, useEffect as n } from "react";
|
|
4
|
+
const b = (r, s, t, e) => {
|
|
5
|
+
n(() => {
|
|
6
|
+
const u = s.deserialize();
|
|
7
|
+
t(u) && (r.current = !0, e.set(u), r.current = !1);
|
|
8
|
+
}, [s, t, r, e]);
|
|
9
|
+
}, j = (r, s, t) => {
|
|
10
|
+
n(() => {
|
|
11
|
+
if (s)
|
|
12
|
+
return f(t.get, (e, u) => {
|
|
13
|
+
const o = l(
|
|
14
|
+
e,
|
|
15
|
+
(i, m) => y(u[m], i)
|
|
16
|
+
), c = Object.keys(o);
|
|
17
|
+
console.assert(c.length > 0), s({ keys: c, popState: r.current });
|
|
18
|
+
});
|
|
19
|
+
}, [t, s, r]), n(() => {
|
|
20
|
+
if (!s) return;
|
|
21
|
+
const e = Object.keys(t.get());
|
|
22
|
+
s({
|
|
23
|
+
keys: e,
|
|
24
|
+
popState: !0
|
|
25
|
+
/* Always true. */
|
|
26
|
+
});
|
|
27
|
+
}, [t, s]);
|
|
28
|
+
}, p = (r, s, t) => {
|
|
29
|
+
n(() => f(s.get, (e) => {
|
|
30
|
+
r.current || t.serialize(e);
|
|
31
|
+
}), [s, r, t]);
|
|
32
|
+
}, d = (r, s, t, e) => {
|
|
33
|
+
const u = k(!1);
|
|
34
|
+
b(u, r, s, t), j(u, e, t), p(u, t, r);
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
d as u
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=useStateHandler-Cmr_YOo4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStateHandler-Cmr_YOo4.js","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useEffect, useRef } from 'react';\n\nimport type {\n\tIStateAccessor,\n\tIStateGetter,\n\tIStateSetter,\n} from './IStateAccessor';\nimport type {\n\tIStateCodec,\n\tIStateDeserializer,\n\tIStateSerializer,\n} from './IStateCodec';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: IStateDeserializer /*<TState>*/,\n\tvalidator: (state: unknown) => state is TState,\n\tsetter: IStateSetter<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\t\t}\n\t}, [deserializer, validator, popStateRef, setter]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n\tgetter: IStateGetter<TState>,\n): void => {\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\t// Returns the disposer.\n\t\treturn reaction(getter.get, (state, previousState) => {\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\tonStateChange({ keys: keys, popState: popStateRef.current });\n\t\t});\n\t}, [getter, onStateChange, popStateRef]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\tconst keys = Object.keys(getter.get()) as (keyof TState)[];\n\n\t\tonStateChange({ keys: keys, popState: true /* Always true. */ });\n\t}, [getter, onStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tgetter: IStateGetter<TState>,\n\tserializer: IStateSerializer<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\t\t});\n\t}, [getter, popStateRef, serializer]);\n};\n\nexport const useStateHandler = <TState,>(\n\tcodec: IStateCodec<TState>,\n\tvalidator: (state: unknown) => state is TState,\n\taccessor: IStateAccessor<TState>,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\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);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, onStateChange, accessor);\n\n\tuseSaveState(popStateRef, accessor, codec);\n};\n"],"names":["useRestoreState","popStateRef","deserializer","validator","setter","useEffect","state","useHandleStateChange","onStateChange","getter","reaction","previousState","diff","omitBy","v","k","isEqual","keys","useSaveState","serializer","useStateHandler","codec","accessor","useRef"],"mappings":";;;AAgBA,M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C,GAAaoB,GAAOlB,GAAWmB,CAAQ,GAGvDf,EAAqBN,GAAaO,GAAec,CAAQ,GAEzDJ,EAAajB,GAAaqB,GAAUD,CAAK;AAC1C;"}
|
package/package.json
CHANGED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";const t=require("lodash-es"),i=require("mobx"),f=require("react"),y=(u,s,c,r)=>{f.useEffect(()=>{const e=s();c(e)&&(u.current=!0,r(e),u.current=!1)},[s,c,u,r])},k=(u,s,c)=>{f.useEffect(()=>{if(s)return i.reaction(c,(r,e)=>{const o=t.omitBy(r,(l,E)=>t.isEqual(e[E],l)),n=Object.keys(o);console.assert(n.length>0),s({keys:n,popState:u.current})})},[c,s,u]),f.useEffect(()=>{if(!s)return;const r=Object.keys(c());s({keys:r,popState:!0})},[c,s])},q=(u,s,c)=>{f.useEffect(()=>i.reaction(s,r=>{u.current||c(r)}),[s,u,c])},b=(u,s,c,r,e,o)=>{const n=f.useRef(!1);y(n,u,s,c),k(n,r,e),q(n,e,o)};exports.useStateHandler=b;
|
|
2
|
-
//# sourceMappingURL=useStateHandler-BKLDeBsK.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useStateHandler-BKLDeBsK.cjs","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useEffect, useRef } from 'react';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: () => any,\n\tstateValidator: (state: any) => state is TState,\n\tstateSetter: (state: TState) => void,\n): void => {\n\tuseEffect(() => {\n\t\tconst state = deserializer();\n\n\t\tif (stateValidator(state)) {\n\t\t\tpopStateRef.current = true;\n\n\t\t\tstateSetter(state);\n\n\t\t\tpopStateRef.current = false;\n\t\t}\n\t}, [deserializer, stateValidator, popStateRef, stateSetter]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n\tstateGetter: () => TState,\n): void => {\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\t// Returns the disposer.\n\t\treturn reaction(stateGetter, (state, previousState) => {\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\tonStateChange({ keys: keys, popState: popStateRef.current });\n\t\t});\n\t}, [stateGetter, onStateChange, popStateRef]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\tconst keys = Object.keys(stateGetter()) as (keyof TState)[];\n\n\t\tonStateChange({ keys: keys, popState: true /* Always true. */ });\n\t}, [stateGetter, onStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tstateGetter: () => TState,\n\tserializer: (state: TState) => void,\n): void => {\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(stateGetter, (state) => {\n\t\t\tif (popStateRef.current) return;\n\n\t\t\tserializer(state);\n\t\t});\n\t}, [stateGetter, popStateRef, serializer]);\n};\n\nexport const useStateHandler = <TState,>(\n\tdeserializer: () => any,\n\tstateValidator: (state: any) => state is TState,\n\tstateSetter: (state: TState) => void,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n\tstateGetter: () => TState,\n\tserializer: (state: TState) => void,\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, deserializer, stateValidator, stateSetter);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, onStateChange, stateGetter);\n\n\tuseSaveState(popStateRef, stateGetter, serializer);\n};\n"],"names":["useRestoreState","popStateRef","deserializer","stateValidator","stateSetter","useEffect","state","useHandleStateChange","onStateChange","stateGetter","reaction","previousState","diff","omitBy","v","k","isEqual","keys","useSaveState","serializer","useStateHandler","useRef"],"mappings":"+EAKMA,EAAkB,CACvBC,EACAC,EACAC,EACAC,IACU,CACVC,EAAAA,UAAU,IAAM,CACf,MAAMC,EAAQJ,EAAA,EAEVC,EAAeG,CAAK,IACvBL,EAAY,QAAU,GAEtBG,EAAYE,CAAK,EAEjBL,EAAY,QAAU,GAExB,EAAG,CAACC,EAAcC,EAAgBF,EAAaG,CAAW,CAAC,CAC5D,EAEMG,EAAuB,CAC5BN,EACAO,EACAC,IACU,CACVJ,EAAAA,UAAU,IAAM,CACf,GAAKG,EAGL,OAAOE,WAASD,EAAa,CAACH,EAAOK,IAAkB,CAEtD,MAAMC,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,EAE9BT,EAAc,CAAE,KAAAS,EAAY,SAAUhB,EAAY,QAAS,CAC5D,CAAC,CACF,EAAG,CAACQ,EAAaD,EAAeP,CAAW,CAAC,EAG5CI,EAAAA,UAAU,IAAM,CACf,GAAI,CAACG,EAAe,OAEpB,MAAMS,EAAO,OAAO,KAAKR,EAAA,CAAa,EAEtCD,EAAc,CAAE,KAAAS,EAAY,SAAU,EAAA,CAAyB,CAChE,EAAG,CAACR,EAAaD,CAAa,CAAC,CAChC,EAEMU,EAAe,CACpBjB,EACAQ,EACAU,IACU,CACVd,EAAAA,UAAU,IAEFK,EAAAA,SAASD,EAAcH,GAAU,CACnCL,EAAY,SAEhBkB,EAAWb,CAAK,CACjB,CAAC,EACC,CAACG,EAAaR,EAAakB,CAAU,CAAC,CAC1C,EAEaC,EAAkB,CAC9BlB,EACAC,EACAC,EACAI,EACAC,EACAU,IACU,CAEV,MAAMlB,EAAcoB,EAAAA,OAAO,EAAK,EAEhCrB,EAAgBC,EAAaC,EAAcC,EAAgBC,CAAW,EAGtEG,EAAqBN,EAAaO,EAAeC,CAAW,EAE5DS,EAAajB,EAAaQ,EAAaU,CAAU,CAClD"}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { omitBy as k, isEqual as l } from "lodash-es";
|
|
2
|
-
import { reaction as i } from "mobx";
|
|
3
|
-
import { useRef as t, useEffect as f } from "react";
|
|
4
|
-
const b = (u, r, s, c) => {
|
|
5
|
-
f(() => {
|
|
6
|
-
const n = r();
|
|
7
|
-
s(n) && (u.current = !0, c(n), u.current = !1);
|
|
8
|
-
}, [r, s, u, c]);
|
|
9
|
-
}, j = (u, r, s) => {
|
|
10
|
-
f(() => {
|
|
11
|
-
if (r)
|
|
12
|
-
return i(s, (c, n) => {
|
|
13
|
-
const m = k(
|
|
14
|
-
c,
|
|
15
|
-
(e, y) => l(n[y], e)
|
|
16
|
-
), o = Object.keys(m);
|
|
17
|
-
console.assert(o.length > 0), r({ keys: o, popState: u.current });
|
|
18
|
-
});
|
|
19
|
-
}, [s, r, u]), f(() => {
|
|
20
|
-
if (!r) return;
|
|
21
|
-
const c = Object.keys(s());
|
|
22
|
-
r({
|
|
23
|
-
keys: c,
|
|
24
|
-
popState: !0
|
|
25
|
-
/* Always true. */
|
|
26
|
-
});
|
|
27
|
-
}, [s, r]);
|
|
28
|
-
}, p = (u, r, s) => {
|
|
29
|
-
f(() => i(r, (c) => {
|
|
30
|
-
u.current || s(c);
|
|
31
|
-
}), [r, u, s]);
|
|
32
|
-
}, d = (u, r, s, c, n, m) => {
|
|
33
|
-
const o = t(!1);
|
|
34
|
-
b(o, u, r, s), j(o, c, n), p(o, n, m);
|
|
35
|
-
};
|
|
36
|
-
export {
|
|
37
|
-
d as u
|
|
38
|
-
};
|
|
39
|
-
//# sourceMappingURL=useStateHandler-CdocwC8x.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useStateHandler-CdocwC8x.js","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import type { StateChangeEvent } from '@/stores/StateChangeEvent';\nimport { isEqual, omitBy } from 'lodash-es';\nimport { reaction } from 'mobx';\nimport { type MutableRefObject, useEffect, useRef } from 'react';\n\nconst useRestoreState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tdeserializer: () => any,\n\tstateValidator: (state: any) => state is TState,\n\tstateSetter: (state: TState) => void,\n): void => {\n\tuseEffect(() => {\n\t\tconst state = deserializer();\n\n\t\tif (stateValidator(state)) {\n\t\t\tpopStateRef.current = true;\n\n\t\t\tstateSetter(state);\n\n\t\t\tpopStateRef.current = false;\n\t\t}\n\t}, [deserializer, stateValidator, popStateRef, stateSetter]);\n};\n\nconst useHandleStateChange = <TState extends Partial<TState>>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n\tstateGetter: () => TState,\n): void => {\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\t// Returns the disposer.\n\t\treturn reaction(stateGetter, (state, previousState) => {\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\tonStateChange({ keys: keys, popState: popStateRef.current });\n\t\t});\n\t}, [stateGetter, onStateChange, popStateRef]);\n\n\t// This is called when the page is first loaded.\n\tuseEffect(() => {\n\t\tif (!onStateChange) return;\n\n\t\tconst keys = Object.keys(stateGetter()) as (keyof TState)[];\n\n\t\tonStateChange({ keys: keys, popState: true /* Always true. */ });\n\t}, [stateGetter, onStateChange]);\n};\n\nconst useSaveState = <TState,>(\n\tpopStateRef: MutableRefObject<boolean>,\n\tstateGetter: () => TState,\n\tserializer: (state: TState) => void,\n): void => {\n\tuseEffect(() => {\n\t\t// Returns the disposer.\n\t\treturn reaction(stateGetter, (state) => {\n\t\t\tif (popStateRef.current) return;\n\n\t\t\tserializer(state);\n\t\t});\n\t}, [stateGetter, popStateRef, serializer]);\n};\n\nexport const useStateHandler = <TState,>(\n\tdeserializer: () => any,\n\tstateValidator: (state: any) => state is TState,\n\tstateSetter: (state: TState) => void,\n\tonStateChange: ((event: StateChangeEvent<TState>) => void) | undefined,\n\tstateGetter: () => TState,\n\tserializer: (state: TState) => void,\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, deserializer, stateValidator, stateSetter);\n\n\t// This must be called before `useSaveState`, so that state can be changed in the `onStateChange` callback.\n\tuseHandleStateChange(popStateRef, onStateChange, stateGetter);\n\n\tuseSaveState(popStateRef, stateGetter, serializer);\n};\n"],"names":["useRestoreState","popStateRef","deserializer","stateValidator","stateSetter","useEffect","state","useHandleStateChange","onStateChange","stateGetter","reaction","previousState","diff","omitBy","v","k","isEqual","keys","useSaveState","serializer","useStateHandler","useRef"],"mappings":";;;AAKA,MAAMA,IAAkB,CACvBC,GACAC,GACAC,GACAC,MACU;AACV,EAAAC,EAAU,MAAM;AACf,UAAMC,IAAQJ,EAAA;AAEd,IAAIC,EAAeG,CAAK,MACvBL,EAAY,UAAU,IAEtBG,EAAYE,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