@dr.pogodin/react-global-state 0.23.0 → 0.24.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/build/code/GlobalStateProvider.js +60 -48
- package/build/code/GlobalStateProvider.js.map +1 -1
- package/build/code/index.js +5 -5
- package/build/code/index.js.map +1 -1
- package/build/code/useAsyncCollection.js +86 -76
- package/build/code/useAsyncCollection.js.map +1 -1
- package/build/code/useAsyncData.js +169 -72
- package/build/code/useAsyncData.js.map +1 -1
- package/build/code/useGlobalState.js +5 -5
- package/build/code/useGlobalState.js.map +1 -1
- package/build/code/utils.js +8 -19
- package/build/code/utils.js.map +1 -1
- package/build/types/GlobalStateProvider.d.ts +14 -16
- package/build/types/index.d.ts +4 -4
- package/build/types/utils.d.ts +2 -14
- package/package.json +22 -20
- package/tsconfig.json +3 -2
|
@@ -1,46 +1,40 @@
|
|
|
1
|
+
import { c as _c } from "react/compiler-runtime";
|
|
1
2
|
import { createContext, use, useState } from 'react';
|
|
2
3
|
import GlobalState from "./GlobalState.js";
|
|
3
4
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
4
5
|
const Context = /*#__PURE__*/createContext(null);
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @return
|
|
8
|
+
* Returns the GlobalState object from the context. In most cases you should use
|
|
9
|
+
* other hooks, like useGlobalState(), etc. to interact with the global state,
|
|
10
|
+
* instead of accessing the GlobalState object directly.
|
|
11
11
|
*/
|
|
12
|
-
export function
|
|
13
|
-
// TODO: Think about it: on one hand we on purpose called this function
|
|
14
|
-
// as getGlobalState(), so that ppl looking for the state hook prefer using
|
|
15
|
-
// useGlobalState(), while this getGlobalState() is reserved for nieche cases;
|
|
16
|
-
// on the other hand, perhaps we can rename it into useSomething, to both
|
|
17
|
-
// follow conventions, and to keep stuff clearly named at the same time.
|
|
18
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
12
|
+
export function useGlobalStateObject() {
|
|
19
13
|
const globalState = use(Context);
|
|
20
14
|
if (!globalState) throw new Error('Missing GlobalStateProvider');
|
|
21
15
|
return globalState;
|
|
22
16
|
}
|
|
23
17
|
|
|
24
18
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
27
|
-
*
|
|
28
|
-
* this hook will throw if
|
|
29
|
-
*
|
|
30
|
-
* if the {@link <GlobalStateProvider>} (hence the state) is missing.
|
|
19
|
+
* Returns SSR context.
|
|
20
|
+
* @param throwWithoutSsrContext - If _true_ (default), this hook will throw
|
|
21
|
+
* if no SSR context is attached to the global state; set _false_ to not throw
|
|
22
|
+
* in such case. In either case this hook will throw if the <GlobalStateProvider>
|
|
23
|
+
* (hence the global state) is missing.
|
|
31
24
|
* @returns SSR context.
|
|
32
25
|
* @throws
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
26
|
+
* - If current component has no parent <GlobalStateProvider> in the rendered
|
|
27
|
+
* React tree.
|
|
28
|
+
* - If `throwWithoutSsrContext` is _true_ (default), and there is no SSR
|
|
29
|
+
* context attached to the global state provided by <GlobalStateProvider>.
|
|
37
30
|
*/
|
|
38
|
-
export function
|
|
31
|
+
export function useSsrContext(t0) {
|
|
32
|
+
const throwWithoutSsrContext = t0 === undefined ? true : t0;
|
|
39
33
|
const {
|
|
40
34
|
ssrContext
|
|
41
|
-
} =
|
|
35
|
+
} = useGlobalStateObject();
|
|
42
36
|
if (!ssrContext && throwWithoutSsrContext) {
|
|
43
|
-
throw new Error(
|
|
37
|
+
throw new Error("No SSR context found");
|
|
44
38
|
}
|
|
45
39
|
return ssrContext;
|
|
46
40
|
}
|
|
@@ -57,34 +51,52 @@ export function getSsrContext(throwWithoutSsrContext = true) {
|
|
|
57
51
|
* - If `GlobalState` instance, it will be used by this provider.
|
|
58
52
|
* - If not given, a new `GlobalState` instance will be created and used.
|
|
59
53
|
*/
|
|
60
|
-
const GlobalStateProvider =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
54
|
+
const GlobalStateProvider = t0 => {
|
|
55
|
+
const $ = _c(3);
|
|
56
|
+
const {
|
|
57
|
+
children,
|
|
58
|
+
...rest
|
|
59
|
+
} = t0;
|
|
64
60
|
const [localState, setLocalState] = useState();
|
|
65
61
|
let state;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
62
|
+
if ("stateProxy" in rest && rest.stateProxy) {
|
|
63
|
+
if (localState) {
|
|
64
|
+
setLocalState(undefined);
|
|
65
|
+
}
|
|
66
|
+
if (rest.stateProxy === true) {
|
|
67
|
+
const gs = use(Context);
|
|
68
|
+
if (!gs) {
|
|
69
|
+
throw Error("Missing GlobalStateProvider");
|
|
70
|
+
}
|
|
71
|
+
state = gs;
|
|
72
|
+
} else {
|
|
73
|
+
state = rest.stateProxy;
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
if (localState) {
|
|
77
|
+
state = localState;
|
|
78
|
+
} else {
|
|
79
|
+
const {
|
|
80
|
+
initialState,
|
|
81
|
+
ssrContext
|
|
82
|
+
} = rest;
|
|
83
|
+
state = new GlobalState(typeof initialState === "function" ? initialState() : initialState, ssrContext);
|
|
84
|
+
setLocalState(state);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let t1;
|
|
88
|
+
if ($[0] !== children || $[1] !== state) {
|
|
89
|
+
t1 = /*#__PURE__*/_jsx(Context, {
|
|
90
|
+
value: state,
|
|
91
|
+
children: children
|
|
92
|
+
});
|
|
93
|
+
$[0] = children;
|
|
94
|
+
$[1] = state;
|
|
95
|
+
$[2] = t1;
|
|
76
96
|
} else {
|
|
77
|
-
|
|
78
|
-
initialState,
|
|
79
|
-
ssrContext
|
|
80
|
-
} = rest;
|
|
81
|
-
state = new GlobalState(typeof initialState === 'function' ? initialState() : initialState, ssrContext);
|
|
82
|
-
setLocalState(state);
|
|
97
|
+
t1 = $[2];
|
|
83
98
|
}
|
|
84
|
-
return
|
|
85
|
-
value: state,
|
|
86
|
-
children: children
|
|
87
|
-
});
|
|
99
|
+
return t1;
|
|
88
100
|
};
|
|
89
101
|
export default GlobalStateProvider;
|
|
90
102
|
//# sourceMappingURL=GlobalStateProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStateProvider.js","names":["createContext","use","useState","GlobalState","jsx","_jsx","Context","
|
|
1
|
+
{"version":3,"file":"GlobalStateProvider.js","names":["createContext","use","useState","GlobalState","jsx","_jsx","Context","useGlobalStateObject","globalState","Error","useSsrContext","t0","throwWithoutSsrContext","undefined","ssrContext","GlobalStateProvider","$","_c","children","rest","localState","setLocalState","state","stateProxy","gs","initialState","t1"],"sources":["../../src/GlobalStateProvider.tsx"],"sourcesContent":["import {\n type ReactNode,\n createContext,\n use,\n useState,\n} from 'react';\n\nimport GlobalState from './GlobalState';\nimport type SsrContext from './SsrContext';\n\nimport type { ValueOrInitializerT } from './utils';\n\nconst Context = createContext<GlobalState<unknown> | null>(null);\n\n/**\n * Returns the GlobalState object from the context. In most cases you should use\n * other hooks, like useGlobalState(), etc. to interact with the global state,\n * instead of accessing the GlobalState object directly.\n */\nexport function useGlobalStateObject<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n>(): GlobalState<StateT, SsrContextT> {\n const globalState = use(Context);\n if (!globalState) throw new Error('Missing GlobalStateProvider');\n return globalState as GlobalState<StateT, SsrContextT>;\n}\n\n/**\n * Returns SSR context.\n * @param throwWithoutSsrContext - If _true_ (default), this hook will throw\n * if no SSR context is attached to the global state; set _false_ to not throw\n * in such case. In either case this hook will throw if the <GlobalStateProvider>\n * (hence the global state) is missing.\n * @returns SSR context.\n * @throws\n * - If current component has no parent <GlobalStateProvider> in the rendered\n * React tree.\n * - If `throwWithoutSsrContext` is _true_ (default), and there is no SSR\n * context attached to the global state provided by <GlobalStateProvider>.\n */\nexport function useSsrContext<\n SsrContextT extends SsrContext<unknown>,\n>(\n throwWithoutSsrContext = true,\n): SsrContextT | undefined {\n const { ssrContext } = useGlobalStateObject<SsrContextT['state'], SsrContextT>();\n if (!ssrContext && throwWithoutSsrContext) {\n throw new Error('No SSR context found');\n }\n return ssrContext;\n}\n\ntype NewStateProps<StateT, SsrContextT extends SsrContext<StateT>> = {\n initialState: ValueOrInitializerT<StateT>;\n ssrContext?: SsrContextT;\n};\n\ntype GlobalStateProviderProps<\n StateT,\n SsrContextT extends SsrContext<StateT>,\n> = (NewStateProps<StateT, SsrContextT> | {\n stateProxy: GlobalState<StateT, SsrContextT> | true;\n}) & {\n children?: ReactNode;\n};\n\n/**\n * Provides global state to its children.\n * @param prop.children Component children, which will be provided with\n * the global state, and rendered in place of the provider.\n * @param prop.initialState Initial content of the global state.\n * @param prop.ssrContext Server-side rendering (SSR) context.\n * @param prop.stateProxy This option is useful for code\n * splitting and SSR implementation:\n * - If `true`, this provider instance will fetch and reuse the global state\n * from a parent provider.\n * - If `GlobalState` instance, it will be used by this provider.\n * - If not given, a new `GlobalState` instance will be created and used.\n */\nconst GlobalStateProvider = <\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n>(\n { children, ...rest }: GlobalStateProviderProps<StateT, SsrContextT>,\n): ReactNode => {\n type GST = GlobalState<StateT, SsrContextT>;\n\n const [localState, setLocalState] = useState<GST>();\n\n let state: GST;\n\n // Below we cast `rest.stateProxy` as \"boolean\" for safe backward\n // compatibility with plain JavaScript (as TypeScript typings only\n // permit \"true\" or GlobalState value; while legacy codebase may\n // pass in a boolean value here, occasionally equal \"false\").\n if ('stateProxy' in rest && (rest.stateProxy as boolean)) {\n if (localState) setLocalState(undefined);\n\n if (rest.stateProxy === true) {\n const gs = use(Context);\n if (!gs) throw Error('Missing GlobalStateProvider');\n state = gs as GlobalState<StateT, SsrContextT>;\n } else state = rest.stateProxy;\n } else if (localState) {\n state = localState;\n } else {\n const {\n initialState,\n ssrContext,\n } = rest as NewStateProps<StateT, SsrContextT>;\n\n state = new GlobalState(\n typeof initialState === 'function' ? (initialState as () => StateT)() : initialState,\n ssrContext,\n );\n\n setLocalState(state);\n }\n\n return <Context value={state}>{children}</Context>;\n};\n\nexport default GlobalStateProvider;\n"],"mappings":";AAAA,SAEEA,aAAa,EACbC,GAAG,EACHC,QAAQ,QACH,OAAO;AAAC,OAERC,WAAW;AAAA,SAAAC,GAAA,IAAAC,IAAA;AAKlB,MAAMC,OAAO,gBAAGN,aAAa,CAA8B,IAAI,CAAC;;AAEhE;AACA;AACA;AACA;AACA;AACA,OAAO,SAASO,oBAAoBA,CAAA,EAGE;EACpC,MAAMC,WAAW,GAAGP,GAAG,CAACK,OAAO,CAAC;EAChC,IAAI,CAACE,WAAW,EAAE,MAAM,IAAIC,KAAK,CAAC,6BAA6B,CAAC;EAChE,OAAOD,WAAW;AACpB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAAAE,cAAAC,EAAA;EAGL,MAAAC,sBAAA,GAAAD,EAA6B,KAA7BE,SAA6B,GAA7B,IAA6B,GAA7BF,EAA6B;EAE7B;IAAAG;EAAA,IAAuBP,oBAAoB,CAAoC,CAAC;EAChF,IAAI,CAACO,UAAoC,IAArCF,sBAAqC;IACvC,MAAM,IAAIH,KAAK,CAAC,sBAAsB,CAAC;EAAC;EACzC,OACMK,UAAU;AAAA;AAiBnB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,mBAAmB,GAAGJ,EAAA;EAAA,MAAAK,CAAA,GAAAC,EAAA;EAI1B;IAAAC,QAAA;IAAA,GAAAC;EAAA,IAAAR,EAAoE;EAIpE,OAAAS,UAAA,EAAAC,aAAA,IAAoCnB,QAAQ,CAAM,CAAC;EAE/CoB,GAAA,CAAAA,KAAA;EAMJ,IAAI,YAAY,IAAIH,IAAoC,IAA3BA,IAAI,CAAAI,UAAuB;IACtD,IAAIH,UAAU;MAAEC,aAAa,CAACR,SAAS,CAAC;IAAA;IAExC,IAAIM,IAAI,CAAAI,UAAW,KAAK,IAAI;MAC1B,MAAAC,EAAA,GAAWvB,GAAG,CAACK,OAAO,CAAC;MACvB,IAAI,CAACkB,EAAE;QAAE,MAAMf,KAAK,CAAC,6BAA6B,CAAC;MAAC;MACpDa,KAAA,CAAAA,CAAA,CAAQE,EAAE;IAAL;MACAF,KAAA,CAAAA,CAAA,CAAQH,IAAI,CAAAI,UAAW;IAAlB;EAAmB;IAC1B,IAAIH,UAAU;MACnBE,KAAA,CAAAA,CAAA,CAAQF,UAAU;IAAb;MAEL;QAAAK,YAAA;QAAAX;MAAA,IAGIK,IAAI;MAERG,KAAA,CAAAA,CAAA,CAAQA,GAAA,CAAInB,WAAW,CACrB,OAAOsB,YAAY,KAAK,UAA4D,GAA9CA,YAAY,CAAiC,CAAC,GAApFA,YAAoF,EACpFX,UACF,CAAC;MAEDO,aAAa,CAACC,KAAK,CAAC;IAAA;EACrB;EAAA,IAAAI,EAAA;EAAA,IAAAV,CAAA,QAAAE,QAAA,IAAAF,CAAA,QAAAM,KAAA;IAEMI,EAAA,gBAAArB,IAAA,CAACC,OAAO;MAAQgB,KAAK,EAALA,KAAK;MAAAJ,QAAA,EAAGA;IAAQ,CAAU,CAAC;IAAAF,CAAA,MAAAE,QAAA;IAAAF,CAAA,MAAAM,KAAA;IAAAN,CAAA,MAAAU,EAAA;EAAA;IAAAA,EAAA,GAAAV,CAAA;EAAA;EAAA,OAA3CU,EAA2C;AAAA,CACnD;AAED,eAAeX,mBAAmB","ignoreList":[]}
|
package/build/code/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import GlobalState from "./GlobalState.js";
|
|
2
|
-
import GlobalStateProvider, {
|
|
2
|
+
import GlobalStateProvider, { useGlobalStateObject, useSsrContext } from "./GlobalStateProvider.js";
|
|
3
3
|
import SsrContext from "./SsrContext.js";
|
|
4
4
|
import useAsyncCollection from "./useAsyncCollection.js";
|
|
5
5
|
import { loadAsyncData, newAsyncDataEnvelope, useAsyncData } from "./useAsyncData.js";
|
|
6
6
|
import useGlobalState from "./useGlobalState.js";
|
|
7
|
-
export { GlobalState, GlobalStateProvider, SsrContext,
|
|
7
|
+
export { GlobalState, GlobalStateProvider, SsrContext, loadAsyncData, newAsyncDataEnvelope, useAsyncCollection, useAsyncData, useGlobalState, useGlobalStateObject, useSsrContext };
|
|
8
8
|
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
10
10
|
|
|
@@ -15,13 +15,13 @@ const api = {
|
|
|
15
15
|
GlobalState,
|
|
16
16
|
GlobalStateProvider,
|
|
17
17
|
SsrContext,
|
|
18
|
-
getGlobalState,
|
|
19
|
-
getSsrContext,
|
|
20
18
|
loadAsyncData,
|
|
21
19
|
newAsyncDataEnvelope,
|
|
22
20
|
useAsyncCollection,
|
|
23
21
|
useAsyncData,
|
|
24
|
-
useGlobalState
|
|
22
|
+
useGlobalState,
|
|
23
|
+
useGlobalStateObject,
|
|
24
|
+
useSsrContext
|
|
25
25
|
};
|
|
26
26
|
export function withGlobalStateType() {
|
|
27
27
|
return api;
|
package/build/code/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["GlobalState","GlobalStateProvider","
|
|
1
|
+
{"version":3,"file":"index.js","names":["GlobalState","GlobalStateProvider","useGlobalStateObject","useSsrContext","SsrContext","useAsyncCollection","loadAsyncData","newAsyncDataEnvelope","useAsyncData","useGlobalState","api","withGlobalStateType"],"sources":["../../src/index.ts"],"sourcesContent":["import GlobalState from './GlobalState';\n\nimport GlobalStateProvider, {\n useGlobalStateObject,\n useSsrContext,\n} from './GlobalStateProvider';\n\nimport SsrContext from './SsrContext';\n\nimport useAsyncCollection, {\n type UseAsyncCollectionI,\n type UseAsyncCollectionResT,\n} from './useAsyncCollection';\n\nimport {\n type AsyncDataEnvelopeT,\n type AsyncDataLoaderT,\n type AsyncDataReloaderT,\n type LoadAsyncDataI,\n type UseAsyncDataI,\n type UseAsyncDataOptionsT,\n type UseAsyncDataResT,\n loadAsyncData,\n newAsyncDataEnvelope,\n useAsyncData,\n} from './useAsyncData';\n\nimport useGlobalState, { type UseGlobalStateI } from './useGlobalState';\n\nexport type {\n AsyncCollectionLoaderT,\n AsyncCollectionReloaderT,\n AsyncCollectionT,\n} from './useAsyncCollection';\n\nexport type { SetterT, UseGlobalStateResT } from './useGlobalState';\n\nexport type { ForceT, ValueOrInitializerT } from './utils';\n\nexport {\n type AsyncDataEnvelopeT,\n type AsyncDataLoaderT,\n type AsyncDataReloaderT,\n GlobalState,\n GlobalStateProvider,\n SsrContext,\n type UseAsyncCollectionI,\n type UseAsyncCollectionResT,\n type UseAsyncDataI,\n type UseAsyncDataOptionsT,\n type UseAsyncDataResT,\n type UseGlobalStateI,\n loadAsyncData,\n newAsyncDataEnvelope,\n useAsyncCollection,\n useAsyncData,\n useGlobalState,\n useGlobalStateObject,\n useSsrContext,\n};\n\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\ninterface API<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n> {\n GlobalState: typeof GlobalState<StateT, SsrContextT>;\n GlobalStateProvider: typeof GlobalStateProvider<StateT, SsrContextT>;\n SsrContext: typeof SsrContext<StateT>;\n loadAsyncData: LoadAsyncDataI<StateT>;\n newAsyncDataEnvelope: typeof newAsyncDataEnvelope;\n useAsyncCollection: UseAsyncCollectionI<StateT>;\n useAsyncData: UseAsyncDataI<StateT>;\n useGlobalState: UseGlobalStateI<StateT>;\n useGlobalStateObject: typeof useGlobalStateObject<StateT, SsrContextT>;\n useSsrContext: typeof useSsrContext<SsrContextT>;\n}\n\nconst api = {\n // TODO: I am puzzled, why ESLint eforces this sorting order as alphabetical?\n // Perhaps, we should tune something in its config settings, as it seems to mix\n // some extra sorting logic, which I am not sure I like.\n GlobalState,\n GlobalStateProvider,\n SsrContext,\n loadAsyncData,\n newAsyncDataEnvelope,\n useAsyncCollection,\n useAsyncData,\n useGlobalState,\n useGlobalStateObject,\n useSsrContext,\n};\n\nexport function withGlobalStateType<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n>(): API<StateT, SsrContextT> {\n return api;\n}\n"],"mappings":"OAAOA,WAAW;AAAA,OAEXC,mBAAmB,IACxBC,oBAAoB,EACpBC,aAAa;AAAA,OAGRC,UAAU;AAAA,OAEVC,kBAAkB;AAAA,SAavBC,aAAa,EACbC,oBAAoB,EACpBC,YAAY;AAAA,OAGPC,cAAc;AAYrB,SAIET,WAAW,EACXC,mBAAmB,EACnBG,UAAU,EAOVE,aAAa,EACbC,oBAAoB,EACpBF,kBAAkB,EAClBG,YAAY,EACZC,cAAc,EACdP,oBAAoB,EACpBC,aAAa;;AAGf;;AAiBA,MAAMO,GAAG,GAAG;EACV;EACA;EACA;EACAV,WAAW;EACXC,mBAAmB;EACnBG,UAAU;EACVE,aAAa;EACbC,oBAAoB;EACpBF,kBAAkB;EAClBG,YAAY;EACZC,cAAc;EACdP,oBAAoB;EACpBC;AACF,CAAC;AAED,OAAO,SAASQ,mBAAmBA,CAAA,EAGL;EAC5B,OAAOD,GAAG;AACZ","ignoreList":[]}
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { useEffect, useRef, useState } from 'react';
|
|
6
|
-
import {
|
|
6
|
+
import { useGlobalStateObject } from "./GlobalStateProvider.js";
|
|
7
7
|
import { DEFAULT_MAXAGE, loadAsyncData, newAsyncDataEnvelope } from "./useAsyncData.js";
|
|
8
8
|
import useGlobalState from "./useGlobalState.js";
|
|
9
|
-
import {
|
|
9
|
+
import { areEqual, isDebugMode } from "./utils.js";
|
|
10
10
|
/**
|
|
11
11
|
* GarbageCollector: the piece of logic executed on mounting of
|
|
12
12
|
* an useAsyncCollection() hook, and on update of hook params, to update
|
|
@@ -96,7 +96,7 @@ function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
|
96
96
|
const maxage = (_options$maxage = options.maxage) !== null && _options$maxage !== void 0 ? _options$maxage : DEFAULT_MAXAGE;
|
|
97
97
|
const refreshAge = (_options$refreshAge = options.refreshAge) !== null && _options$refreshAge !== void 0 ? _options$refreshAge : maxage;
|
|
98
98
|
const garbageCollectAge = (_options$garbageColle = options.garbageCollectAge) !== null && _options$garbageColle !== void 0 ? _options$garbageColle : maxage;
|
|
99
|
-
const globalState =
|
|
99
|
+
const globalState = useGlobalStateObject();
|
|
100
100
|
|
|
101
101
|
// Server-side logic.
|
|
102
102
|
if (globalState.ssrContext) {
|
|
@@ -118,64 +118,58 @@ function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
|
+
}
|
|
122
|
+
const {
|
|
123
|
+
disabled
|
|
124
|
+
} = options;
|
|
121
125
|
|
|
122
|
-
|
|
123
|
-
} else {
|
|
124
|
-
const {
|
|
125
|
-
disabled
|
|
126
|
-
} = options;
|
|
127
|
-
|
|
128
|
-
// Reference-counting & garbage collection.
|
|
129
|
-
|
|
130
|
-
const idsHash = hash(ids);
|
|
126
|
+
// Reference-counting & garbage collection.
|
|
131
127
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
128
|
+
const idsString = JSON.stringify(ids);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
const localIds = JSON.parse(idsString);
|
|
131
|
+
if (!disabled) gcOnWithhold(localIds, path, globalState);
|
|
132
|
+
return () => {
|
|
133
|
+
if (!disabled) {
|
|
134
|
+
gcOnRelease(localIds, path, globalState, garbageCollectAge);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
140
137
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}, [disabled, garbageCollectAge, globalState, idsHash, path]);
|
|
138
|
+
// `ids` are represented in the dependencies array by `idsHash` value,
|
|
139
|
+
// as useEffect() hook requires a constant size of dependencies array.
|
|
140
|
+
}, [disabled, garbageCollectAge, globalState, idsString, path]);
|
|
145
141
|
|
|
146
|
-
|
|
147
|
-
|
|
142
|
+
// NOTE: a bunch of Rules of Hooks ignored belows because in our very
|
|
143
|
+
// special case the otherwise wrong behavior is actually what we need.
|
|
148
144
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
}
|
|
145
|
+
// Data loading and refreshing.
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
if (!disabled) {
|
|
148
|
+
void (async () => {
|
|
149
|
+
for (const id_0 of ids) {
|
|
150
|
+
var _state2$timestamp;
|
|
151
|
+
const itemPath_0 = path ? `${path}.${id_0}` : `${id_0}`;
|
|
152
|
+
const state2 = globalState.get(itemPath_0);
|
|
153
|
+
const {
|
|
154
|
+
deps
|
|
155
|
+
} = options;
|
|
156
|
+
if (deps && globalState.hasChangedDependencies(itemPath_0, deps) || refreshAge < Date.now() - ((_state2$timestamp = state2 === null || state2 === void 0 ? void 0 : state2.timestamp) !== null && _state2$timestamp !== void 0 ? _state2$timestamp : 0) && (!(state2 !== null && state2 !== void 0 && state2.operationId) || state2.operationId.startsWith('S'))) {
|
|
157
|
+
var _state2$data, _state2$timestamp2;
|
|
158
|
+
if (!deps) globalState.dropDependencies(itemPath_0);
|
|
159
|
+
await loadAsyncData(itemPath_0,
|
|
160
|
+
// TODO: I guess, the loader is not correctly typed here -
|
|
161
|
+
// it can be synchronous, and in that case the following method
|
|
162
|
+
// should be kept synchronous to not alter the sync logic.
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
164
|
+
(old, ...args_0) => loader(id_0, old, ...args_0), globalState, {
|
|
165
|
+
data: (_state2$data = state2 === null || state2 === void 0 ? void 0 : state2.data) !== null && _state2$data !== void 0 ? _state2$data : null,
|
|
166
|
+
timestamp: (_state2$timestamp2 = state2 === null || state2 === void 0 ? void 0 : state2.timestamp) !== null && _state2$timestamp2 !== void 0 ? _state2$timestamp2 : 0
|
|
167
|
+
});
|
|
174
168
|
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
169
|
+
}
|
|
170
|
+
})();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
179
173
|
const [localState] = useGlobalState(path, {});
|
|
180
174
|
const ref = useRef(null);
|
|
181
175
|
(_ref$current = ref.current) !== null && _ref$current !== void 0 ? _ref$current : ref.current = {
|
|
@@ -204,17 +198,17 @@ function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
|
204
198
|
if (!localLoader || !rc.globalState || !rc.ids) {
|
|
205
199
|
throw Error('Internal error');
|
|
206
200
|
}
|
|
207
|
-
for (const
|
|
208
|
-
const
|
|
209
|
-
const
|
|
201
|
+
for (const id_1 of rc.ids) {
|
|
202
|
+
const itemPath_1 = rc.path ? `${rc.path}.${id_1}` : `${id_1}`;
|
|
203
|
+
const promiseOrVoid_0 = loadAsyncData(itemPath_1,
|
|
210
204
|
// TODO: Revise! Most probably we don't have fully correct loader
|
|
211
205
|
// typing, as it may return either promise or value, and those two
|
|
212
206
|
// cases call for different runtime behavior, which in turns only
|
|
213
207
|
// happens if the outer function on the next line matches the same
|
|
214
208
|
// async / sync signature.
|
|
215
209
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
216
|
-
(oldData, meta) => localLoader(
|
|
217
|
-
if (
|
|
210
|
+
(oldData, meta) => localLoader(id_1, oldData, meta), rc.globalState);
|
|
211
|
+
if (promiseOrVoid_0 instanceof Promise) await promiseOrVoid_0;
|
|
218
212
|
}
|
|
219
213
|
};
|
|
220
214
|
|
|
@@ -224,14 +218,14 @@ function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
|
224
218
|
// happens if the outer function on the next line matches the same
|
|
225
219
|
// async / sync signature.
|
|
226
220
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
227
|
-
const reloadSingle =
|
|
221
|
+
const reloadSingle = customLoader_0 => reload(
|
|
228
222
|
// TODO: Revise! Most probably we don't have fully correct loader
|
|
229
223
|
// typing, as it may return either promise or value, and those two
|
|
230
224
|
// cases call for different runtime behavior, which in turns only
|
|
231
225
|
// happens if the outer function on the next line matches the same
|
|
232
226
|
// async / sync signature.
|
|
233
227
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
234
|
-
|
|
228
|
+
customLoader_0 && ((id_2, ...args_1) => customLoader_0(...args_1)));
|
|
235
229
|
const setSingle = data => {
|
|
236
230
|
void reload(() => data);
|
|
237
231
|
};
|
|
@@ -241,14 +235,30 @@ function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
|
241
235
|
setSingle
|
|
242
236
|
};
|
|
243
237
|
});
|
|
238
|
+
const [stale, setStale] = useState({});
|
|
239
|
+
|
|
240
|
+
// TODO: Merge into the data-reloading effect above?
|
|
241
|
+
useEffect(() => {
|
|
242
|
+
const now = Date.now();
|
|
243
|
+
const nowStale = {};
|
|
244
|
+
for (const [key, e] of Object.entries(localState)) {
|
|
245
|
+
nowStale[key] = maxage < now - e.timestamp;
|
|
246
|
+
}
|
|
247
|
+
const id_3 = areEqual(stale, nowStale) ? null : requestAnimationFrame(() => {
|
|
248
|
+
setStale(nowStale);
|
|
249
|
+
});
|
|
250
|
+
return () => {
|
|
251
|
+
if (id_3 !== null) cancelAnimationFrame(id_3);
|
|
252
|
+
};
|
|
253
|
+
});
|
|
244
254
|
if (!Array.isArray(idOrIds)) {
|
|
245
|
-
var
|
|
255
|
+
var _e_0$timestamp, _e_0$data;
|
|
246
256
|
// TODO: Revise related typings!
|
|
247
|
-
const
|
|
248
|
-
const timestamp = (
|
|
257
|
+
const e_0 = localState[idOrIds];
|
|
258
|
+
const timestamp = (_e_0$timestamp = e_0 === null || e_0 === void 0 ? void 0 : e_0.timestamp) !== null && _e_0$timestamp !== void 0 ? _e_0$timestamp : 0;
|
|
249
259
|
return {
|
|
250
|
-
data:
|
|
251
|
-
loading: !!(
|
|
260
|
+
data: stale[idOrIds] ? null : (_e_0$data = e_0 === null || e_0 === void 0 ? void 0 : e_0.data) !== null && _e_0$data !== void 0 ? _e_0$data : null,
|
|
261
|
+
loading: !!(e_0 !== null && e_0 !== void 0 && e_0.operationId),
|
|
252
262
|
reload: stable.reloadSingle,
|
|
253
263
|
set: stable.setSingle,
|
|
254
264
|
timestamp
|
|
@@ -260,19 +270,19 @@ function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
|
260
270
|
reload: stable.reload,
|
|
261
271
|
timestamp: Number.MAX_VALUE
|
|
262
272
|
};
|
|
263
|
-
for (const
|
|
264
|
-
var
|
|
273
|
+
for (const id_4 of ids) {
|
|
274
|
+
var _e_1$timestamp, _e_1$data;
|
|
265
275
|
// TODO: Revise related typing. Should `localState` have a more specific type?
|
|
266
|
-
const
|
|
267
|
-
const loading = !!(
|
|
268
|
-
const
|
|
269
|
-
res.items[
|
|
270
|
-
data:
|
|
276
|
+
const e_1 = localState[id_4];
|
|
277
|
+
const loading = !!(e_1 !== null && e_1 !== void 0 && e_1.operationId);
|
|
278
|
+
const timestamp_0 = (_e_1$timestamp = e_1 === null || e_1 === void 0 ? void 0 : e_1.timestamp) !== null && _e_1$timestamp !== void 0 ? _e_1$timestamp : 0;
|
|
279
|
+
res.items[id_4] = {
|
|
280
|
+
data: stale[id_4] ? null : (_e_1$data = e_1 === null || e_1 === void 0 ? void 0 : e_1.data) !== null && _e_1$data !== void 0 ? _e_1$data : null,
|
|
271
281
|
loading,
|
|
272
|
-
timestamp
|
|
282
|
+
timestamp: timestamp_0
|
|
273
283
|
};
|
|
274
284
|
res.loading || (res.loading = loading);
|
|
275
|
-
if (res.timestamp >
|
|
285
|
+
if (res.timestamp > timestamp_0) res.timestamp = timestamp_0;
|
|
276
286
|
}
|
|
277
287
|
return res;
|
|
278
288
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAsyncCollection.js","names":["useEffect","useRef","useState","getGlobalState","DEFAULT_MAXAGE","loadAsyncData","newAsyncDataEnvelope","useGlobalState","hash","isDebugMode","gcOnWithhold","ids","path","gs","collection","get","id","envelope","numRefs","set","idsToStringSet","res","Set","add","toString","gcOnRelease","gcAge","entries","Object","now","Date","idSet","toBeReleased","has","timestamp","process","env","NODE_ENV","console","log","normalizeIds","idOrIds","Array","isArray","from","sort","a","b","localeCompare","useAsyncCollection","loader","options","_options$maxage","_options$refreshAge","_options$garbageColle","_ref$current","maxage","refreshAge","garbageCollectAge","globalState","ssrContext","disabled","noSSR","operationId","globalThis","crypto","randomUUID","itemPath","state","initialValue","promiseOrVoid","args","data","Promise","pending","push","idsHash","_state2$timestamp","state2","deps","hasChangedDependencies","startsWith","_state2$data","_state2$timestamp2","dropDependencies","old","localState","ref","current","stable","reload","customLoader","rc","Error","localLoader","oldData","meta","reloadSingle","setSingle","_e$timestamp","_e$data","e","loading","items","Number","MAX_VALUE","_e$timestamp2","_e$data2"],"sources":["../../src/useAsyncCollection.ts"],"sourcesContent":["/**\n * Loads and uses item(s) in an async collection.\n */\n\nimport { useEffect, useRef, useState } from 'react';\n\nimport type GlobalState from './GlobalState';\nimport { getGlobalState } from './GlobalStateProvider';\n\nimport {\n type AsyncDataEnvelopeT,\n type AsyncDataReloaderT,\n DEFAULT_MAXAGE,\n type DataInEnvelopeAtPathT,\n type OperationIdT,\n type UseAsyncDataOptionsT,\n type UseAsyncDataResT,\n loadAsyncData,\n newAsyncDataEnvelope,\n} from './useAsyncData';\n\nimport useGlobalState from './useGlobalState';\n\nimport {\n type ForceT,\n type LockT,\n type TypeLock,\n hash,\n isDebugMode,\n} from './utils';\n\nexport type AsyncCollectionT<\n DataT = unknown,\n IdT extends number | string = number | string,\n> = Partial<Record<IdT, AsyncDataEnvelopeT<DataT>>>;\n\nexport type AsyncCollectionLoaderT<\n DataT,\n IdT extends number | string = number | string,\n> = (id: IdT, oldData: DataT | null, meta: {\n abortSignal: AbortSignal;\n oldDataTimestamp: number;\n}) => DataT | Promise<DataT | null> | null;\n\nexport type AsyncCollectionReloaderT<\n DataT,\n IdT extends number | string = number | string,\n> = (loader?: AsyncCollectionLoaderT<DataT, IdT>) => Promise<void> | void;\n\ntype CollectionItemT<DataT> = {\n data: DataT | null;\n loading: boolean;\n timestamp: number;\n};\n\nexport type UseAsyncCollectionResT<\n DataT,\n IdT extends number | string = number | string,\n> = {\n items: Record<IdT, CollectionItemT<DataT>>;\n loading: boolean;\n reload: AsyncCollectionReloaderT<DataT, IdT>;\n timestamp: number;\n};\n\ntype CurrentT<DataT, IdT extends number | string> = {\n globalState: GlobalState<unknown>;\n ids: IdT[];\n loader: AsyncCollectionLoaderT<DataT, IdT>;\n path: null | string | undefined;\n};\n\ntype StableT<DataT, IdT extends number | string> = {\n reload: AsyncCollectionReloaderT<DataT, IdT>;\n reloadSingle: AsyncDataReloaderT<DataT>;\n setSingle: (data: DataT | null) => void;\n};\n\n/**\n * GarbageCollector: the piece of logic executed on mounting of\n * an useAsyncCollection() hook, and on update of hook params, to update\n * the state according to the new param values. It increments by 1 `numRefs`\n * counters for the requested collection items.\n */\nfunction gcOnWithhold<IdT extends number | string>(\n ids: IdT[],\n path: null | string | undefined,\n gs: GlobalState<unknown>,\n) {\n const collection = { ...gs.get<ForceT, AsyncCollectionT>(path) };\n\n for (const id of ids) {\n let envelope = collection[id];\n if (envelope) envelope = { ...envelope, numRefs: 1 + envelope.numRefs };\n else envelope = newAsyncDataEnvelope<unknown>(null, { numRefs: 1 });\n collection[id] = envelope;\n }\n\n gs.set<ForceT, AsyncCollectionT>(path, collection);\n}\n\nfunction idsToStringSet<IdT extends number | string>(ids: IdT[]): Set<string> {\n const res = new Set<string>();\n for (const id of ids) {\n res.add(id.toString());\n }\n return res;\n}\n\n/**\n * GarbageCollector: the piece of logic executed on un-mounting of\n * an useAsyncCollection() hook, and on update of hook params, to clean-up\n * after the previous param values. It decrements by 1 `numRefs` counters\n * for previously requested collection items, and also drops from the state\n * stale records.\n */\nfunction gcOnRelease<IdT extends number | string>(\n ids: IdT[],\n path: null | string | undefined,\n gs: GlobalState<unknown>,\n gcAge: number,\n) {\n type EnvelopeT = AsyncDataEnvelopeT<unknown>;\n\n const entries = Object.entries<EnvelopeT | undefined>(\n gs.get<ForceT, AsyncCollectionT>(path),\n );\n\n const now = Date.now();\n const idSet = idsToStringSet(ids);\n const collection: AsyncCollectionT = {};\n for (const [id, envelope] of entries) {\n if (envelope) {\n const toBeReleased = idSet.has(id);\n\n let { numRefs } = envelope;\n if (toBeReleased) --numRefs;\n\n if (gcAge > now - envelope.timestamp || numRefs > 0) {\n collection[id as IdT] = toBeReleased\n ? { ...envelope, numRefs }\n : envelope;\n } else if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n // eslint-disable-next-line no-console\n console.log(\n `useAsyncCollection(): Garbage collected at the path \"${\n path}\", ID = ${id}`,\n );\n }\n }\n }\n\n gs.set<ForceT, AsyncCollectionT>(path, collection);\n}\n\nfunction normalizeIds<IdT extends number | string>(\n idOrIds: IdT | IdT[],\n): IdT[] {\n if (Array.isArray(idOrIds)) {\n // Removes ID duplicates.\n const res = Array.from(new Set(idOrIds));\n\n // Ensures stable ID order.\n res.sort((a, b) => a.toString().localeCompare(b.toString()));\n\n return res;\n }\n return [idOrIds];\n}\n\n/**\n * Resolves and stores at the given `path` of the global state elements of\n * an asynchronous data collection.\n */\n\nfunction useAsyncCollection<\n StateT,\n PathT extends null | string | undefined,\n IdT extends number | string,\n\n DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> = DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,\n>(\n id: IdT,\n path: PathT,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncDataResT<DataT>;\n\nfunction useAsyncCollection<\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n>(\n id: IdT,\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;\n\nfunction useAsyncCollection<\n StateT,\n PathT extends null | string | undefined,\n IdT extends number | string,\n\n DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> = DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,\n>(\n id: IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncCollectionResT<DataT, IdT>;\n\nfunction useAsyncCollection<\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n>(\n id: IdT[],\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncCollectionResT<DataT, IdT>;\n\nfunction useAsyncCollection<\n StateT,\n PathT extends null | string | undefined,\n IdT extends number | string,\n\n DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> = DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,\n>(\n id: IdT | IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncCollectionResT<DataT, IdT> | UseAsyncDataResT<DataT>;\n\n// TODO: This is largely similar to useAsyncData() logic, just more generic.\n// Perhaps, a bunch of logic blocks can be split into stand-alone functions,\n// and reused in both hooks.\n// eslint-disable-next-line complexity\nfunction useAsyncCollection<\n DataT,\n IdT extends number | string,\n>(\n idOrIds: IdT | IdT[],\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options: UseAsyncDataOptionsT = {},\n): UseAsyncCollectionResT<DataT, IdT> | UseAsyncDataResT<DataT> {\n const ids = normalizeIds(idOrIds);\n const maxage: number = options.maxage ?? DEFAULT_MAXAGE;\n const refreshAge: number = options.refreshAge ?? maxage;\n const garbageCollectAge: number = options.garbageCollectAge ?? maxage;\n\n const globalState = getGlobalState();\n\n // Server-side logic.\n if (globalState.ssrContext) {\n if (!options.disabled && !options.noSSR) {\n const operationId: OperationIdT = `S${globalThis.crypto.randomUUID()}`;\n for (const id of ids) {\n const itemPath = path ? `${path}.${id}` : `${id}`;\n const state = globalState.get<ForceT, AsyncDataEnvelopeT<DataT>>(\n itemPath,\n {\n initialValue: newAsyncDataEnvelope<DataT>(),\n },\n );\n if (!state.timestamp && !state.operationId) {\n const promiseOrVoid = loadAsyncData<ForceT, DataT>(\n itemPath,\n (...args):\n DataT | Promise<DataT | null> | null => loader(id, ...args),\n globalState,\n {\n data: state.data,\n timestamp: state.timestamp,\n },\n operationId,\n );\n\n if (promiseOrVoid instanceof Promise) {\n globalState.ssrContext.pending.push(promiseOrVoid);\n }\n }\n }\n }\n\n // Client-side logic.\n } else {\n const { disabled } = options;\n\n // Reference-counting & garbage collection.\n\n const idsHash = hash(ids);\n\n // TODO: Violation of rules of hooks is fine here,\n // but perhaps it can be refactored to avoid the need for it.\n useEffect(() => { // eslint-disable-line react-hooks/rules-of-hooks\n if (!disabled) gcOnWithhold(ids, path, globalState);\n return () => {\n if (!disabled) gcOnRelease(ids, path, globalState, garbageCollectAge);\n };\n\n // `ids` are represented in the dependencies array by `idsHash` value,\n // as useEffect() hook requires a constant size of dependencies array.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n disabled,\n garbageCollectAge,\n globalState,\n idsHash,\n path,\n ]);\n\n // NOTE: a bunch of Rules of Hooks ignored belows because in our very\n // special case the otherwise wrong behavior is actually what we need.\n\n // Data loading and refreshing.\n useEffect(() => { // eslint-disable-line react-hooks/rules-of-hooks\n if (!disabled) {\n void (async () => {\n for (const id of ids) {\n const itemPath = path ? `${path}.${id}` : `${id}`;\n\n type EnvT = AsyncDataEnvelopeT<DataT> | undefined;\n const state2: EnvT = globalState.get<ForceT, EnvT>(itemPath);\n\n const { deps } = options;\n if (\n (deps && globalState.hasChangedDependencies(itemPath, deps))\n || (\n refreshAge < Date.now() - (state2?.timestamp ?? 0)\n && (!state2?.operationId || state2.operationId.startsWith('S'))\n )\n ) {\n if (!deps) globalState.dropDependencies(itemPath);\n await loadAsyncData<ForceT, DataT>(\n itemPath,\n // TODO: I guess, the loader is not correctly typed here -\n // it can be synchronous, and in that case the following method\n // should be kept synchronous to not alter the sync logic.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n (old, ...args) => loader(id, old as DataT, ...args),\n globalState,\n {\n data: state2?.data ?? null,\n timestamp: state2?.timestamp ?? 0,\n },\n );\n }\n }\n })();\n }\n });\n }\n\n const [localState] = useGlobalState<\n ForceT, Record<string, AsyncDataEnvelopeT<DataT>>\n >(path, {});\n\n const ref = useRef<CurrentT<DataT, IdT>>(null);\n\n ref.current ??= {\n globalState,\n ids,\n loader,\n path,\n };\n\n useEffect(() => {\n ref.current = {\n globalState,\n ids,\n loader,\n path,\n };\n }, [globalState, ids, loader, path]);\n\n const [stable] = useState<StableT<DataT, IdT>>(() => {\n const reload = async (\n customLoader?: AsyncCollectionLoaderT<DataT, IdT>,\n ) => {\n const rc = ref.current;\n if (!rc) throw Error('Internal error');\n\n const localLoader = customLoader ?? rc.loader;\n\n // TODO: Revise - not sure all related typing is 100% correct,\n // thus let's keep this runtime assertion.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!localLoader || !rc.globalState || !rc.ids) {\n throw Error('Internal error');\n }\n\n for (const id of rc.ids) {\n const itemPath = rc.path ? `${rc.path}.${id}` : `${id}`;\n\n const promiseOrVoid = loadAsyncData<ForceT, DataT>(\n itemPath,\n // TODO: Revise! Most probably we don't have fully correct loader\n // typing, as it may return either promise or value, and those two\n // cases call for different runtime behavior, which in turns only\n // happens if the outer function on the next line matches the same\n // async / sync signature.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n (oldData: DataT | null, meta) => localLoader(id, oldData, meta),\n rc.globalState,\n );\n\n if (promiseOrVoid instanceof Promise) await promiseOrVoid;\n }\n };\n\n // TODO: Revise! Most probably we don't have fully correct loader\n // typing, as it may return either promise or value, and those two\n // cases call for different runtime behavior, which in turns only\n // happens if the outer function on the next line matches the same\n // async / sync signature.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const reloadSingle: AsyncDataReloaderT<DataT> = (customLoader) => reload(\n // TODO: Revise! Most probably we don't have fully correct loader\n // typing, as it may return either promise or value, and those two\n // cases call for different runtime behavior, which in turns only\n // happens if the outer function on the next line matches the same\n // async / sync signature.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n customLoader && ((id, ...args) => customLoader(...args)),\n );\n\n const setSingle = (data: DataT | null) => {\n void reload(() => data);\n };\n\n return { reload, reloadSingle, setSingle };\n });\n\n if (!Array.isArray(idOrIds)) {\n // TODO: Revise related typings!\n const e = localState[idOrIds as string];\n const timestamp = e?.timestamp ?? 0;\n return {\n data: maxage < Date.now() - timestamp ? null : e?.data ?? null,\n loading: !!e?.operationId,\n reload: stable.reloadSingle,\n set: stable.setSingle,\n timestamp,\n };\n }\n\n const res: UseAsyncCollectionResT<DataT, IdT> = {\n items: {} as Record<IdT, CollectionItemT<DataT>>,\n loading: false,\n reload: stable.reload,\n timestamp: Number.MAX_VALUE,\n };\n\n for (const id of ids) {\n // TODO: Revise related typing. Should `localState` have a more specific type?\n const e = localState[id as string];\n const loading = !!e?.operationId;\n const timestamp = e?.timestamp ?? 0;\n\n res.items[id] = {\n data: maxage < Date.now() - timestamp ? null : e?.data ?? null,\n loading,\n timestamp,\n };\n res.loading ||= loading;\n if (res.timestamp > timestamp) res.timestamp = timestamp;\n }\n\n return res;\n}\n\nexport default useAsyncCollection;\n\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface UseAsyncCollectionI<StateT> {\n <PathT extends null | string | undefined, IdT extends number | string>(\n id: IdT,\n path: PathT,\n loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncDataResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>>;\n\n <\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n >(\n id: IdT,\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;\n\n <PathT extends null | string | undefined, IdT extends number | string>(\n id: IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncCollectionResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>;\n\n <\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n >(\n id: IdT[],\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncCollectionResT<DataT, IdT>;\n\n <PathT extends null | string | undefined, IdT extends number | string>(\n id: IdT | IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncCollectionResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>\n | UseAsyncDataResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${I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EsE,MAAM,CAACC;EACpB,CAAC;EAED,KAAK,MAAMzF,EAAE,IAAIL,GAAG,EAAE;IAAA,IAAA+F,aAAA,EAAAC,QAAA;IACpB;IACA,MAAMN,CAAC,GAAGf,UAAU,CAACtE,EAAE,CAAW;IAClC,MAAMsF,OAAO,GAAG,CAAC,EAACD,CAAC,aAADA,CAAC,eAADA,CAAC,CAAEtC,WAAW;IAChC,MAAM7B,SAAS,IAAAwE,aAAA,GAAGL,CAAC,aAADA,CAAC,uBAADA,CAAC,CAAEnE,SAAS,cAAAwE,aAAA,cAAAA,aAAA,GAAI,CAAC;IAEnCrF,GAAG,CAACkF,KAAK,CAACvF,EAAE,CAAC,GAAG;MACdwD,IAAI,EAAEhB,MAAM,GAAG1B,IAAI,CAACD,GAAG,CAAC,CAAC,GAAGK,SAAS,GAAG,IAAI,IAAAyE,QAAA,GAAGN,CAAC,aAADA,CAAC,uBAADA,CAAC,CAAE7B,IAAI,cAAAmC,QAAA,cAAAA,QAAA,GAAI,IAAI;MAC9DL,OAAO;MACPpE;IACF,CAAC;IACDb,GAAG,CAACiF,OAAO,KAAXjF,GAAG,CAACiF,OAAO,GAAKA,OAAO;IACvB,IAAIjF,GAAG,CAACa,SAAS,GAAGA,SAAS,EAAEb,GAAG,CAACa,SAAS,GAAGA,SAAS;EAC1D;EAEA,OAAOb,GAAG;AACZ;AAEA,eAAe4B,kBAAkB;;AAEjC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"useAsyncCollection.js","names":["useEffect","useRef","useState","useGlobalStateObject","DEFAULT_MAXAGE","loadAsyncData","newAsyncDataEnvelope","useGlobalState","areEqual","isDebugMode","gcOnWithhold","ids","path","gs","collection","get","id","envelope","numRefs","set","idsToStringSet","res","Set","add","toString","gcOnRelease","gcAge","entries","Object","now","Date","idSet","toBeReleased","has","timestamp","process","env","NODE_ENV","console","log","normalizeIds","idOrIds","Array","isArray","from","sort","a","b","localeCompare","useAsyncCollection","loader","options","_options$maxage","_options$refreshAge","_options$garbageColle","_ref$current","maxage","refreshAge","garbageCollectAge","globalState","ssrContext","disabled","noSSR","operationId","globalThis","crypto","randomUUID","itemPath","state","initialValue","promiseOrVoid","args","data","Promise","pending","push","idsString","JSON","stringify","localIds","parse","_state2$timestamp","state2","deps","hasChangedDependencies","startsWith","_state2$data","_state2$timestamp2","dropDependencies","old","localState","ref","current","stable","reload","customLoader","rc","Error","localLoader","oldData","meta","reloadSingle","setSingle","stale","setStale","nowStale","key","e","requestAnimationFrame","cancelAnimationFrame","_e_0$timestamp","_e_0$data","loading","items","Number","MAX_VALUE","_e_1$timestamp","_e_1$data"],"sources":["../../src/useAsyncCollection.ts"],"sourcesContent":["/**\n * Loads and uses item(s) in an async collection.\n */\n\nimport { useEffect, useRef, useState } from 'react';\n\nimport type GlobalState from './GlobalState';\nimport { useGlobalStateObject } from './GlobalStateProvider';\n\nimport {\n type AsyncDataEnvelopeT,\n type AsyncDataReloaderT,\n DEFAULT_MAXAGE,\n type DataInEnvelopeAtPathT,\n type OperationIdT,\n type UseAsyncDataOptionsT,\n type UseAsyncDataResT,\n loadAsyncData,\n newAsyncDataEnvelope,\n} from './useAsyncData';\n\nimport useGlobalState from './useGlobalState';\n\nimport {\n type ForceT,\n type LockT,\n type TypeLock,\n areEqual,\n isDebugMode,\n} from './utils';\n\nexport type AsyncCollectionT<\n DataT = unknown,\n IdT extends number | string = number | string,\n> = Partial<Record<IdT, AsyncDataEnvelopeT<DataT>>>;\n\nexport type AsyncCollectionLoaderT<\n DataT,\n IdT extends number | string = number | string,\n> = (id: IdT, oldData: DataT | null, meta: {\n abortSignal: AbortSignal;\n oldDataTimestamp: number;\n}) => DataT | Promise<DataT | null> | null;\n\nexport type AsyncCollectionReloaderT<\n DataT,\n IdT extends number | string = number | string,\n> = (loader?: AsyncCollectionLoaderT<DataT, IdT>) => Promise<void> | void;\n\ntype CollectionItemT<DataT> = {\n data: DataT | null;\n loading: boolean;\n timestamp: number;\n};\n\nexport type UseAsyncCollectionResT<\n DataT,\n IdT extends number | string = number | string,\n> = {\n items: Record<IdT, CollectionItemT<DataT>>;\n loading: boolean;\n reload: AsyncCollectionReloaderT<DataT, IdT>;\n timestamp: number;\n};\n\ntype CurrentT<DataT, IdT extends number | string> = {\n globalState: GlobalState<unknown>;\n ids: IdT[];\n loader: AsyncCollectionLoaderT<DataT, IdT>;\n path: null | string | undefined;\n};\n\ntype StableT<DataT, IdT extends number | string> = {\n reload: AsyncCollectionReloaderT<DataT, IdT>;\n reloadSingle: AsyncDataReloaderT<DataT>;\n setSingle: (data: DataT | null) => void;\n};\n\n/**\n * GarbageCollector: the piece of logic executed on mounting of\n * an useAsyncCollection() hook, and on update of hook params, to update\n * the state according to the new param values. It increments by 1 `numRefs`\n * counters for the requested collection items.\n */\nfunction gcOnWithhold<IdT extends number | string>(\n ids: IdT[],\n path: null | string | undefined,\n gs: GlobalState<unknown>,\n) {\n const collection = { ...gs.get<ForceT, AsyncCollectionT>(path) };\n\n for (const id of ids) {\n let envelope = collection[id];\n if (envelope) envelope = { ...envelope, numRefs: 1 + envelope.numRefs };\n else envelope = newAsyncDataEnvelope<unknown>(null, { numRefs: 1 });\n collection[id] = envelope;\n }\n\n gs.set<ForceT, AsyncCollectionT>(path, collection);\n}\n\nfunction idsToStringSet<IdT extends number | string>(ids: IdT[]): Set<string> {\n const res = new Set<string>();\n for (const id of ids) {\n res.add(id.toString());\n }\n return res;\n}\n\n/**\n * GarbageCollector: the piece of logic executed on un-mounting of\n * an useAsyncCollection() hook, and on update of hook params, to clean-up\n * after the previous param values. It decrements by 1 `numRefs` counters\n * for previously requested collection items, and also drops from the state\n * stale records.\n */\nfunction gcOnRelease<IdT extends number | string>(\n ids: IdT[],\n path: null | string | undefined,\n gs: GlobalState<unknown>,\n gcAge: number,\n) {\n type EnvelopeT = AsyncDataEnvelopeT<unknown>;\n\n const entries = Object.entries<EnvelopeT | undefined>(\n gs.get<ForceT, AsyncCollectionT>(path),\n );\n\n const now = Date.now();\n const idSet = idsToStringSet(ids);\n const collection: AsyncCollectionT = {};\n for (const [id, envelope] of entries) {\n if (envelope) {\n const toBeReleased = idSet.has(id);\n\n let { numRefs } = envelope;\n if (toBeReleased) --numRefs;\n\n if (gcAge > now - envelope.timestamp || numRefs > 0) {\n collection[id as IdT] = toBeReleased\n ? { ...envelope, numRefs }\n : envelope;\n } else if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n // eslint-disable-next-line no-console\n console.log(\n `useAsyncCollection(): Garbage collected at the path \"${\n path}\", ID = ${id}`,\n );\n }\n }\n }\n\n gs.set<ForceT, AsyncCollectionT>(path, collection);\n}\n\nfunction normalizeIds<IdT extends number | string>(\n idOrIds: IdT | IdT[],\n): IdT[] {\n if (Array.isArray(idOrIds)) {\n // Removes ID duplicates.\n const res = Array.from(new Set(idOrIds));\n\n // Ensures stable ID order.\n res.sort((a, b) => a.toString().localeCompare(b.toString()));\n\n return res;\n }\n return [idOrIds];\n}\n\n/**\n * Resolves and stores at the given `path` of the global state elements of\n * an asynchronous data collection.\n */\n\nfunction useAsyncCollection<\n StateT,\n PathT extends null | string | undefined,\n IdT extends number | string,\n\n DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> = DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,\n>(\n id: IdT,\n path: PathT,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncDataResT<DataT>;\n\nfunction useAsyncCollection<\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n>(\n id: IdT,\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;\n\nfunction useAsyncCollection<\n StateT,\n PathT extends null | string | undefined,\n IdT extends number | string,\n\n DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> = DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,\n>(\n id: IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncCollectionResT<DataT, IdT>;\n\nfunction useAsyncCollection<\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n>(\n id: IdT[],\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncCollectionResT<DataT, IdT>;\n\nfunction useAsyncCollection<\n StateT,\n PathT extends null | string | undefined,\n IdT extends number | string,\n\n DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> = DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,\n>(\n id: IdT | IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options?: UseAsyncDataOptionsT,\n): UseAsyncCollectionResT<DataT, IdT> | UseAsyncDataResT<DataT>;\n\n// TODO: This is largely similar to useAsyncData() logic, just more generic.\n// Perhaps, a bunch of logic blocks can be split into stand-alone functions,\n// and reused in both hooks.\n// eslint-disable-next-line complexity\nfunction useAsyncCollection<\n DataT,\n IdT extends number | string,\n>(\n idOrIds: IdT | IdT[],\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<DataT, IdT>,\n options: UseAsyncDataOptionsT = {},\n): UseAsyncCollectionResT<DataT, IdT> | UseAsyncDataResT<DataT> {\n const ids = normalizeIds(idOrIds);\n const maxage: number = options.maxage ?? DEFAULT_MAXAGE;\n const refreshAge: number = options.refreshAge ?? maxage;\n const garbageCollectAge: number = options.garbageCollectAge ?? maxage;\n\n const globalState = useGlobalStateObject();\n\n // Server-side logic.\n if (globalState.ssrContext) {\n if (!options.disabled && !options.noSSR) {\n const operationId: OperationIdT = `S${globalThis.crypto.randomUUID()}`;\n for (const id of ids) {\n const itemPath = path ? `${path}.${id}` : `${id}`;\n const state = globalState.get<ForceT, AsyncDataEnvelopeT<DataT>>(\n itemPath,\n {\n initialValue: newAsyncDataEnvelope<DataT>(),\n },\n );\n if (!state.timestamp && !state.operationId) {\n const promiseOrVoid = loadAsyncData<ForceT, DataT>(\n itemPath,\n (...args):\n DataT | Promise<DataT | null> | null => loader(id, ...args),\n globalState,\n {\n data: state.data,\n timestamp: state.timestamp,\n },\n operationId,\n );\n\n if (promiseOrVoid instanceof Promise) {\n globalState.ssrContext.pending.push(promiseOrVoid);\n }\n }\n }\n }\n }\n\n const { disabled } = options;\n\n // Reference-counting & garbage collection.\n\n const idsString = JSON.stringify(ids);\n\n useEffect(() => {\n const localIds = JSON.parse(idsString) as IdT[];\n\n if (!disabled) gcOnWithhold(localIds, path, globalState);\n return () => {\n if (!disabled) {\n gcOnRelease(localIds, path, globalState, garbageCollectAge);\n }\n };\n\n // `ids` are represented in the dependencies array by `idsHash` value,\n // as useEffect() hook requires a constant size of dependencies array.\n }, [\n disabled,\n garbageCollectAge,\n globalState,\n idsString,\n path,\n ]);\n\n // NOTE: a bunch of Rules of Hooks ignored belows because in our very\n // special case the otherwise wrong behavior is actually what we need.\n\n // Data loading and refreshing.\n useEffect(() => {\n if (!disabled) {\n void (async () => {\n for (const id of ids) {\n const itemPath = path ? `${path}.${id}` : `${id}`;\n\n type EnvT = AsyncDataEnvelopeT<DataT> | undefined;\n const state2: EnvT = globalState.get<ForceT, EnvT>(itemPath);\n\n const { deps } = options;\n if (\n (deps && globalState.hasChangedDependencies(itemPath, deps))\n || (\n refreshAge < Date.now() - (state2?.timestamp ?? 0)\n && (!state2?.operationId || state2.operationId.startsWith('S'))\n )\n ) {\n if (!deps) globalState.dropDependencies(itemPath);\n await loadAsyncData<ForceT, DataT>(\n itemPath,\n // TODO: I guess, the loader is not correctly typed here -\n // it can be synchronous, and in that case the following method\n // should be kept synchronous to not alter the sync logic.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n (old, ...args) => loader(id, old, ...args),\n globalState,\n {\n data: state2?.data ?? null,\n timestamp: state2?.timestamp ?? 0,\n },\n );\n }\n }\n })();\n }\n });\n\n const [localState] = useGlobalState<\n ForceT, Record<string, AsyncDataEnvelopeT<DataT>>\n >(path, {});\n\n const ref = useRef<CurrentT<DataT, IdT>>(null);\n\n ref.current ??= {\n globalState,\n ids,\n loader,\n path,\n };\n\n useEffect(() => {\n ref.current = {\n globalState,\n ids,\n loader,\n path,\n };\n }, [globalState, ids, loader, path]);\n\n const [stable] = useState<StableT<DataT, IdT>>(() => {\n const reload = async (\n customLoader?: AsyncCollectionLoaderT<DataT, IdT>,\n ) => {\n const rc = ref.current;\n if (!rc) throw Error('Internal error');\n\n const localLoader = customLoader ?? rc.loader;\n\n // TODO: Revise - not sure all related typing is 100% correct,\n // thus let's keep this runtime assertion.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (!localLoader || !rc.globalState || !rc.ids) {\n throw Error('Internal error');\n }\n\n for (const id of rc.ids) {\n const itemPath = rc.path ? `${rc.path}.${id}` : `${id}`;\n\n const promiseOrVoid = loadAsyncData<ForceT, DataT>(\n itemPath,\n // TODO: Revise! Most probably we don't have fully correct loader\n // typing, as it may return either promise or value, and those two\n // cases call for different runtime behavior, which in turns only\n // happens if the outer function on the next line matches the same\n // async / sync signature.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n (oldData: DataT | null, meta) => localLoader(id, oldData, meta),\n rc.globalState,\n );\n\n if (promiseOrVoid instanceof Promise) await promiseOrVoid;\n }\n };\n\n // TODO: Revise! Most probably we don't have fully correct loader\n // typing, as it may return either promise or value, and those two\n // cases call for different runtime behavior, which in turns only\n // happens if the outer function on the next line matches the same\n // async / sync signature.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n const reloadSingle: AsyncDataReloaderT<DataT> = (customLoader) => reload(\n // TODO: Revise! Most probably we don't have fully correct loader\n // typing, as it may return either promise or value, and those two\n // cases call for different runtime behavior, which in turns only\n // happens if the outer function on the next line matches the same\n // async / sync signature.\n // eslint-disable-next-line @typescript-eslint/promise-function-async\n customLoader && ((id, ...args) => customLoader(...args)),\n );\n\n const setSingle = (data: DataT | null) => {\n void reload(() => data);\n };\n\n return { reload, reloadSingle, setSingle };\n });\n\n const [stale, setStale] = useState({} as Record<IdT, boolean>);\n\n // TODO: Merge into the data-reloading effect above?\n useEffect(() => {\n const now = Date.now();\n const nowStale = {} as Record<IdT, boolean>;\n for (const [key, e] of Object.entries(localState)) {\n nowStale[key as IdT] = maxage < now - e.timestamp;\n }\n\n const id = areEqual(stale, nowStale) ? null : requestAnimationFrame(() => {\n setStale(nowStale);\n });\n\n return () => {\n if (id !== null) cancelAnimationFrame(id);\n };\n });\n\n if (!Array.isArray(idOrIds)) {\n // TODO: Revise related typings!\n const e = localState[idOrIds as string];\n const timestamp = e?.timestamp ?? 0;\n return {\n data: stale[idOrIds] ? null : e?.data ?? null,\n loading: !!e?.operationId,\n reload: stable.reloadSingle,\n set: stable.setSingle,\n timestamp,\n };\n }\n\n const res: UseAsyncCollectionResT<DataT, IdT> = {\n items: {} as Record<IdT, CollectionItemT<DataT>>,\n loading: false,\n reload: stable.reload,\n timestamp: Number.MAX_VALUE,\n };\n\n for (const id of ids) {\n // TODO: Revise related typing. Should `localState` have a more specific type?\n const e = localState[id as string];\n const loading = !!e?.operationId;\n const timestamp = e?.timestamp ?? 0;\n\n res.items[id] = {\n data: stale[id] ? null : e?.data ?? null,\n loading,\n timestamp,\n };\n res.loading ||= loading;\n if (res.timestamp > timestamp) res.timestamp = timestamp;\n }\n\n return res;\n}\n\nexport default useAsyncCollection;\n\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface UseAsyncCollectionI<StateT> {\n <PathT extends null | string | undefined, IdT extends number | string>(\n id: IdT,\n path: PathT,\n loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncDataResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>>;\n\n <\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n >(\n id: IdT,\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;\n\n <PathT extends null | string | undefined, IdT extends number | string>(\n id: IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncCollectionResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>;\n\n <\n Forced extends ForceT | LockT = LockT,\n DataT = unknown,\n IdT extends number | string = number | string,\n >(\n id: IdT[],\n path: null | string | undefined,\n loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncCollectionResT<DataT, IdT>;\n\n <PathT extends null | string | undefined, IdT extends number | string>(\n id: IdT | IdT[],\n path: PathT,\n loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,\n options?: UseAsyncDataOptionsT,\n ): UseAsyncCollectionResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>\n | UseAsyncDataResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>>;\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAAC,SAG3CC,oBAAoB;AAAA,SAK3BC,cAAc,EAKdC,aAAa,EACbC,oBAAoB;AAAA,OAGfC,cAAc;AAAA,SAMnBC,QAAQ,EACRC,WAAW;AAkDb;AACA;AACA;AACA;AACA;AACA;AACA,SAASC,YAAYA,CACnBC,GAAU,EACVC,IAA+B,EAC/BC,EAAwB,EACxB;EACA,MAAMC,UAAU,GAAG;IAAE,GAAGD,EAAE,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