@aigamo/route-sphere 2.2.0-0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.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 {
|
|
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"],"names":["ScrollToTop","location","useLocation","useLayoutEffect","useLocalStorageStateDeserializer","key","useCallback","error","useLocalStorageStateSerializer","state","useLocalStorageStateCodec","deserializer","serializer","useMemo","useLocalStorageStateHandler","validator","accessor","options","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,ECJMG,EAAoCC,GAClCC,EAAAA,YAAY,IAAe,CACjC,GAAI,CACH,OAAO,KAAK,MACX,OAAO,aAAa,QAAQD,CAAG,GAAK,KAAK,UAAU,CAAA,CAAE,CAAA,CAEvD,OAASE,EAAO,CACf,eAAQ,MAAMA,CAAK,EACZ,CAAA,CACR,CACD,EAAG,CAACF,CAAG,CAAC,EAGHG,EACLH,GAEOC,EAAAA,YACLG,GAAwB,CACxB,OAAO,aAAa,QAAQJ,EAAK,KAAK,UAAUI,CAAK,CAAC,CACvD,EACA,CAACJ,CAAG,CAAA,EAIAK,EACLL,GACyB,CACzB,MAAMM,EAAeP,EAAiCC,CAAG,EACnDO,EAAaJ,EAA+BH,CAAG,EAQrD,OAPcQ,EAAAA,QACb,KAA4B,CAC3B,YAAaF,EACb,UAAWC,CAAA,GAEZ,CAACD,EAAcC,CAAU,CAAA,CAG3B,EAEME,EAA8B,CACnCT,EACAU,EACAC,EACAC,IACU,CACV,MAAMC,EAAQR,EAA0BL,CAAG,EAC3Cc,EAAAA,gBAAgBD,EAAOH,EAAWC,EAAUC,CAAO,CACpD,EAEMG,EACLC,GAEOf,EAAAA,YAAY,IAAce,EAAM,MAAO,CAACA,CAAK,CAAC,EAGhDC,EACLD,GAEOf,EAAAA,YACLG,GAAwB,CACxBY,EAAM,MAAQZ,CACf,EACA,CAACY,CAAK,CAAA,EAIFE,EACLF,GAC4B,CAC5B,MAAMG,EAASJ,EAA2BC,CAAK,EACzCI,EAASH,EAA2BD,CAAK,EAK/C,OAJiBR,EAAAA,QAChB,KAA+B,CAAE,IAAKW,EAAQ,IAAKC,CAAA,GACnD,CAACD,EAAQC,CAAM,CAAA,CAGjB,EAEaC,EAAuB,CACnCrB,EACAgB,IACU,CACV,MAAML,EAAWO,EAA6BF,CAAK,EAC7CJ,EAAUJ,EAAAA,QACf,KAAO,CACN,eAAgBQ,EAAM,eACtB,cAAeA,EAAM,cACrB,YAAaA,EAAM,WAAA,GAEpB,CAACA,CAAK,CAAA,EAEPP,EAA4BT,EAAKgB,EAAM,cAAeL,EAAUC,CAAO,CACxE,ECnGaU,EAAc,CAAIC,EAAYC,IACnCA,EAAO,KAAMC,GAAUF,EAAM,SAASE,CAAK,CAAC"}
|
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 {
|
|
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"],"names":["ScrollToTop","location","useLocation","useLayoutEffect","useLocalStorageStateDeserializer","key","useCallback","error","useLocalStorageStateSerializer","state","useLocalStorageStateCodec","deserializer","serializer","useMemo","useLocalStorageStateHandler","validator","accessor","options","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,GCJMG,IAAmC,CAACC,MAClCC,EAAY,MAAe;AACjC,MAAI;AACH,WAAO,KAAK;AAAA,MACX,OAAO,aAAa,QAAQD,CAAG,KAAK,KAAK,UAAU,CAAA,CAAE;AAAA,IAAA;AAAA,EAEvD,SAASE,GAAO;AACf,mBAAQ,MAAMA,CAAK,GACZ,CAAA;AAAA,EACR;AACD,GAAG,CAACF,CAAG,CAAC,GAGHG,IAAiC,CACtC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O,KAAK,CAACC,MAAUF,EAAM,SAASE,CAAK,CAAC;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStateHandler-Dbkejxjm.js","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import
|
|
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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStateHandler-Ff_CnvN8.cjs","sources":["../src/components/useStateHandler.tsx"],"sourcesContent":["import
|
|
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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
"version": "2.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"dependencies": {},
|
|
6
6
|
"devDependencies": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"eslint-plugin-prettier": "^5.5.4",
|
|
27
27
|
"eslint-plugin-react": "^7.37.5",
|
|
28
28
|
"eslint-plugin-react-hooks": "^7.0.1",
|
|
29
|
-
"lodash-es": "^4.17.
|
|
29
|
+
"lodash-es": "^4.17.23",
|
|
30
30
|
"mobx": "^6.15.0",
|
|
31
31
|
"mobx-react-lite": "^4.1.1",
|
|
32
32
|
"prettier": "^3.7.4",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"@tanstack/react-router": "^1.144.0",
|
|
44
|
-
"lodash-es": "^4.17.
|
|
44
|
+
"lodash-es": "^4.17.23",
|
|
45
45
|
"mobx": "^6.15.0",
|
|
46
46
|
"mobx-react-lite": "^3.0.0 || ^4.0.0",
|
|
47
47
|
"qs": "^6.14.1",
|