@dr.pogodin/react-global-state 0.19.1 → 0.19.3
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/cjs/GlobalState.js +289 -0
- package/build/cjs/GlobalState.js.map +1 -0
- package/build/cjs/GlobalStateProvider.js +97 -0
- package/build/cjs/GlobalStateProvider.js.map +1 -0
- package/build/cjs/SsrContext.js +15 -0
- package/build/cjs/SsrContext.js.map +1 -0
- package/build/cjs/index.js +88 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/useAsyncCollection.js +281 -0
- package/build/cjs/useAsyncCollection.js.map +1 -0
- package/build/cjs/useAsyncData.js +260 -0
- package/build/cjs/useAsyncData.js.map +1 -0
- package/build/cjs/useGlobalState.js +149 -0
- package/build/cjs/useGlobalState.js.map +1 -0
- package/build/cjs/utils.js +91 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/code/GlobalStateProvider.js +5 -7
- package/build/code/GlobalStateProvider.js.map +1 -1
- package/build/code/useAsyncCollection.js +52 -57
- package/build/code/useAsyncCollection.js.map +1 -1
- package/build/code/useAsyncData.js +11 -10
- package/build/code/useAsyncData.js.map +1 -1
- package/build/code/utils.js +1 -2
- package/build/code/utils.js.map +1 -1
- package/build/types/useAsyncCollection.d.ts +1 -1
- package/build/types/useAsyncData.d.ts +1 -1
- package/package.json +21 -19
- package/tstyche.config.json +4 -4
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _lodash = require("lodash");
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _jsUtils = require("@dr.pogodin/js-utils");
|
|
10
|
+
var _GlobalStateProvider = require("./GlobalStateProvider");
|
|
11
|
+
var _utils = require("./utils");
|
|
12
|
+
// Hook for updates of global state.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The primary hook for interacting with the global state, modeled after
|
|
16
|
+
* the standard React's
|
|
17
|
+
* [useState](https://reactjs.org/docs/hooks-reference.html#usestate).
|
|
18
|
+
* It subscribes a component to a given `path` of global state, and provides
|
|
19
|
+
* a function to update it. Each time the value at `path` changes, the hook
|
|
20
|
+
* triggers re-render of its host component.
|
|
21
|
+
*
|
|
22
|
+
* **Note:**
|
|
23
|
+
* - For performance, the library does not copy objects written to / read from
|
|
24
|
+
* global state paths. You MUST NOT manually mutate returned state values,
|
|
25
|
+
* or change objects already written into the global state, without explicitly
|
|
26
|
+
* clonning them first yourself.
|
|
27
|
+
* - State update notifications are asynchronous. When your code does multiple
|
|
28
|
+
* global state updates in the same React rendering cycle, all state update
|
|
29
|
+
* notifications are queued and dispatched together, after the current
|
|
30
|
+
* rendering cycle. In other words, in any given rendering cycle the global
|
|
31
|
+
* state values are "fixed", and all changes becomes visible at once in the
|
|
32
|
+
* next triggered rendering pass.
|
|
33
|
+
*
|
|
34
|
+
* @param path Dot-delimitered state path. It can be undefined to
|
|
35
|
+
* subscribe for entire state.
|
|
36
|
+
*
|
|
37
|
+
* Under-the-hood state values are read and written using `lodash`
|
|
38
|
+
* [_.get()](https://lodash.com/docs/4.17.15#get) and
|
|
39
|
+
* [_.set()](https://lodash.com/docs/4.17.15#set) methods, thus it is safe
|
|
40
|
+
* to access state paths which have not been created before.
|
|
41
|
+
* @param initialValue Initial value to set at the `path`, or its
|
|
42
|
+
* factory:
|
|
43
|
+
* - If a function is given, it will act similar to
|
|
44
|
+
* [the lazy initial state of the standard React's useState()](https://reactjs.org/docs/hooks-reference.html#lazy-initial-state):
|
|
45
|
+
* only if the value at `path` is `undefined`, the function will be executed,
|
|
46
|
+
* and the value it returns will be written to the `path`.
|
|
47
|
+
* - Otherwise, the given value itself will be written to the `path`,
|
|
48
|
+
* if the current value at `path` is `undefined`.
|
|
49
|
+
* @return It returs an array with two elements: `[value, setValue]`:
|
|
50
|
+
*
|
|
51
|
+
* - The `value` is the current value at given `path`.
|
|
52
|
+
*
|
|
53
|
+
* - The `setValue()` is setter function to write a new value to the `path`.
|
|
54
|
+
*
|
|
55
|
+
* Similar to the standard React's `useState()`, it supports
|
|
56
|
+
* [functional value updates](https://reactjs.org/docs/hooks-reference.html#functional-updates):
|
|
57
|
+
* if `setValue()` is called with a function as argument, that function will
|
|
58
|
+
* be called and its return value will be written to `path`. Otherwise,
|
|
59
|
+
* the argument of `setValue()` itself is written to `path`.
|
|
60
|
+
*
|
|
61
|
+
* Also, similar to the standard React's state setters, `setValue()` is
|
|
62
|
+
* stable function: it does not change between component re-renders.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
// "Enforced type overload"
|
|
66
|
+
|
|
67
|
+
// "Entire state overload"
|
|
68
|
+
|
|
69
|
+
// "State evaluation overload"
|
|
70
|
+
|
|
71
|
+
function useGlobalState(path,
|
|
72
|
+
// TODO: Revise it later!
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
initialValue
|
|
75
|
+
|
|
76
|
+
// TODO: Revise it later!
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
|
+
) {
|
|
79
|
+
const globalState = (0, _GlobalStateProvider.getGlobalState)();
|
|
80
|
+
const ref = (0, _react.useRef)(undefined);
|
|
81
|
+
|
|
82
|
+
// TODO: Revise how this `rc` variable is used, perhaps we can simplify stuff
|
|
83
|
+
// here.
|
|
84
|
+
let rc = ref.current;
|
|
85
|
+
if (!ref.current) {
|
|
86
|
+
const emitter = new _jsUtils.Emitter();
|
|
87
|
+
ref.current = {
|
|
88
|
+
emitter,
|
|
89
|
+
globalState,
|
|
90
|
+
path,
|
|
91
|
+
setter: value => {
|
|
92
|
+
const newState = (0, _lodash.isFunction)(value) ? value(rc.globalState.get(rc.path)) : value;
|
|
93
|
+
if (process.env.NODE_ENV !== 'production' && (0, _utils.isDebugMode)()) {
|
|
94
|
+
/* eslint-disable no-console */
|
|
95
|
+
console.groupCollapsed(`ReactGlobalState - useGlobalState setter triggered for path ${rc.path ?? ''}`);
|
|
96
|
+
console.log('New value:', (0, _utils.cloneDeepForLog)(newState, rc.path ?? ''));
|
|
97
|
+
console.groupEnd();
|
|
98
|
+
/* eslint-enable no-console */
|
|
99
|
+
}
|
|
100
|
+
rc.globalState.set(rc.path, newState);
|
|
101
|
+
|
|
102
|
+
// NOTE: The regular global state's update notifications, automatically
|
|
103
|
+
// triggered by the rc.globalState.set() call above, are batched, and
|
|
104
|
+
// scheduled to fire asynchronosuly at a later time, which is problematic
|
|
105
|
+
// for managed text inputs - if they have their value update delayed to
|
|
106
|
+
// future render cycles, it will result in reset of their cursor position
|
|
107
|
+
// to the value end. Calling the rc.emitter.emit() below causes a sooner
|
|
108
|
+
// state update for the current component, thus working around the issue.
|
|
109
|
+
// For additional details see the original issue:
|
|
110
|
+
// https://github.com/birdofpreyru/react-global-state/issues/22
|
|
111
|
+
if (newState !== rc.state) rc.emitter.emit();
|
|
112
|
+
},
|
|
113
|
+
state: (0, _lodash.isFunction)(initialValue) ? initialValue() : initialValue,
|
|
114
|
+
subscribe: emitter.addListener.bind(emitter),
|
|
115
|
+
watcher: () => {
|
|
116
|
+
// TODO: Revise it later.
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
118
|
+
const state = rc.globalState.get(rc.path);
|
|
119
|
+
if (state !== rc.state) rc.emitter.emit();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
rc = ref.current;
|
|
124
|
+
rc.globalState = globalState;
|
|
125
|
+
rc.path = path;
|
|
126
|
+
rc.state = (0, _react.useSyncExternalStore)(rc.subscribe, () => rc.globalState.get(rc.path, {
|
|
127
|
+
initialValue
|
|
128
|
+
}), () => rc.globalState.get(rc.path, {
|
|
129
|
+
initialState: true,
|
|
130
|
+
initialValue
|
|
131
|
+
}));
|
|
132
|
+
(0, _react.useEffect)(() => {
|
|
133
|
+
const {
|
|
134
|
+
watcher
|
|
135
|
+
} = ref.current;
|
|
136
|
+
globalState.watch(watcher);
|
|
137
|
+
watcher();
|
|
138
|
+
return () => {
|
|
139
|
+
globalState.unWatch(watcher);
|
|
140
|
+
};
|
|
141
|
+
}, [globalState]);
|
|
142
|
+
(0, _react.useEffect)(() => {
|
|
143
|
+
ref.current.watcher();
|
|
144
|
+
}, [path]);
|
|
145
|
+
return [rc.state, rc.setter];
|
|
146
|
+
}
|
|
147
|
+
var _default = exports.default = useGlobalState; // TODO: Revise.
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
149
|
+
//# sourceMappingURL=useGlobalState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useGlobalState.js","names":["_lodash","require","_react","_jsUtils","_GlobalStateProvider","_utils","useGlobalState","path","initialValue","globalState","getGlobalState","ref","useRef","undefined","rc","current","emitter","Emitter","setter","value","newState","isFunction","get","process","env","NODE_ENV","isDebugMode","console","groupCollapsed","log","cloneDeepForLog","groupEnd","set","state","emit","subscribe","addListener","bind","watcher","useSyncExternalStore","initialState","useEffect","watch","unWatch","_default","exports","default"],"sources":["../../src/useGlobalState.ts"],"sourcesContent":["// Hook for updates of global state.\n\nimport { isFunction } from 'lodash';\n\nimport {\n type Dispatch,\n type SetStateAction,\n useEffect,\n useRef,\n useSyncExternalStore,\n} from 'react';\n\nimport { Emitter } from '@dr.pogodin/js-utils';\n\nimport type GlobalState from './GlobalState';\nimport { getGlobalState } from './GlobalStateProvider';\n\nimport {\n type CallbackT,\n type ForceT,\n type LockT,\n type TypeLock,\n type ValueAtPathT,\n type ValueOrInitializerT,\n cloneDeepForLog,\n isDebugMode,\n} from './utils';\n\nexport type SetterT<T> = Dispatch<SetStateAction<T>>;\n\ntype ListenerT = () => void;\n\ntype GlobalStateRef = {\n emitter: Emitter<[]>;\n globalState: GlobalState<unknown>;\n path: null | string | undefined;\n setter: SetterT<unknown>;\n subscribe: (listener: ListenerT) => () => void;\n state: unknown;\n watcher: CallbackT;\n};\n\nexport type UseGlobalStateResT<T> = [T, SetterT<T>];\n\n/**\n * The primary hook for interacting with the global state, modeled after\n * the standard React's\n * [useState](https://reactjs.org/docs/hooks-reference.html#usestate).\n * It subscribes a component to a given `path` of global state, and provides\n * a function to update it. Each time the value at `path` changes, the hook\n * triggers re-render of its host component.\n *\n * **Note:**\n * - For performance, the library does not copy objects written to / read from\n * global state paths. You MUST NOT manually mutate returned state values,\n * or change objects already written into the global state, without explicitly\n * clonning them first yourself.\n * - State update notifications are asynchronous. When your code does multiple\n * global state updates in the same React rendering cycle, all state update\n * notifications are queued and dispatched together, after the current\n * rendering cycle. In other words, in any given rendering cycle the global\n * state values are \"fixed\", and all changes becomes visible at once in the\n * next triggered rendering pass.\n *\n * @param path Dot-delimitered state path. It can be undefined to\n * subscribe for entire state.\n *\n * Under-the-hood state values are read and written using `lodash`\n * [_.get()](https://lodash.com/docs/4.17.15#get) and\n * [_.set()](https://lodash.com/docs/4.17.15#set) methods, thus it is safe\n * to access state paths which have not been created before.\n * @param initialValue Initial value to set at the `path`, or its\n * factory:\n * - If a function is given, it will act similar to\n * [the lazy initial state of the standard React's useState()](https://reactjs.org/docs/hooks-reference.html#lazy-initial-state):\n * only if the value at `path` is `undefined`, the function will be executed,\n * and the value it returns will be written to the `path`.\n * - Otherwise, the given value itself will be written to the `path`,\n * if the current value at `path` is `undefined`.\n * @return It returs an array with two elements: `[value, setValue]`:\n *\n * - The `value` is the current value at given `path`.\n *\n * - The `setValue()` is setter function to write a new value to the `path`.\n *\n * Similar to the standard React's `useState()`, it supports\n * [functional value updates](https://reactjs.org/docs/hooks-reference.html#functional-updates):\n * if `setValue()` is called with a function as argument, that function will\n * be called and its return value will be written to `path`. Otherwise,\n * the argument of `setValue()` itself is written to `path`.\n *\n * Also, similar to the standard React's state setters, `setValue()` is\n * stable function: it does not change between component re-renders.\n */\n\n// \"Enforced type overload\"\nfunction useGlobalState<\n Forced extends ForceT | LockT = LockT,\n ValueT = void,\n>(\n path: null | string | undefined,\n initialValue?: ValueOrInitializerT<TypeLock<Forced, never, ValueT>>,\n): UseGlobalStateResT<TypeLock<Forced, void, ValueT>>;\n\n// \"Entire state overload\"\nfunction useGlobalState<StateT>(): UseGlobalStateResT<StateT>;\n\n// \"State evaluation overload\"\nfunction useGlobalState<\n StateT,\n PathT extends null | string | undefined,\n>(\n path: PathT,\n initialValue: ValueOrInitializerT<\n Exclude<ValueAtPathT<StateT, PathT, never>, undefined>>\n): UseGlobalStateResT<Exclude<ValueAtPathT<StateT, PathT, void>, undefined>>;\n\nfunction useGlobalState<\n StateT,\n PathT extends null | string | undefined,\n>(\n path: PathT,\n initialValue?: ValueOrInitializerT<ValueAtPathT<StateT, PathT, never>>\n): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\nfunction useGlobalState(\n path?: null | string,\n // TODO: Revise it later!\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initialValue?: ValueOrInitializerT<any>,\n\n // TODO: Revise it later!\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): UseGlobalStateResT<any> {\n const globalState = getGlobalState();\n\n const ref = useRef<GlobalStateRef>(undefined);\n\n // TODO: Revise how this `rc` variable is used, perhaps we can simplify stuff\n // here.\n let rc: GlobalStateRef | undefined = ref.current;\n if (!ref.current) {\n const emitter = new Emitter();\n ref.current = {\n emitter,\n globalState,\n path,\n setter: (value) => {\n const newState = isFunction(value)\n ? value(rc!.globalState.get(rc!.path)) as unknown\n : value;\n\n if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n /* eslint-disable no-console */\n console.groupCollapsed(\n `ReactGlobalState - useGlobalState setter triggered for path ${\n rc!.path ?? ''\n }`,\n );\n console.log('New value:', cloneDeepForLog(newState, rc!.path ?? ''));\n console.groupEnd();\n /* eslint-enable no-console */\n }\n rc!.globalState.set<ForceT, unknown>(rc!.path, newState);\n\n // NOTE: The regular global state's update notifications, automatically\n // triggered by the rc.globalState.set() call above, are batched, and\n // scheduled to fire asynchronosuly at a later time, which is problematic\n // for managed text inputs - if they have their value update delayed to\n // future render cycles, it will result in reset of their cursor position\n // to the value end. Calling the rc.emitter.emit() below causes a sooner\n // state update for the current component, thus working around the issue.\n // For additional details see the original issue:\n // https://github.com/birdofpreyru/react-global-state/issues/22\n if (newState !== rc!.state) rc!.emitter.emit();\n },\n state: isFunction(initialValue) ? initialValue() : initialValue,\n subscribe: emitter.addListener.bind(emitter),\n watcher: () => {\n // TODO: Revise it later.\n // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression\n const state = rc!.globalState.get(rc!.path) as unknown;\n if (state !== rc!.state) rc!.emitter.emit();\n },\n };\n }\n rc = ref.current!;\n\n rc.globalState = globalState;\n rc.path = path;\n\n rc.state = useSyncExternalStore(\n rc.subscribe,\n () => rc.globalState.get<ForceT, unknown>(rc.path, { initialValue }),\n\n () => rc.globalState.get<ForceT, unknown>(\n rc.path,\n { initialState: true, initialValue },\n ),\n );\n\n useEffect(() => {\n const { watcher } = ref.current!;\n globalState.watch(watcher);\n watcher();\n return () => {\n globalState.unWatch(watcher);\n };\n }, [globalState]);\n\n useEffect(() => {\n ref.current!.watcher();\n }, [path]);\n\n return [rc.state, rc.setter];\n}\n\nexport default useGlobalState;\n\n// TODO: Revise.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface UseGlobalStateI<StateT> {\n (): UseGlobalStateResT<StateT>;\n\n <PathT extends null | string | undefined>(\n path: PathT,\n initialValue: ValueOrInitializerT<\n Exclude<ValueAtPathT<StateT, PathT, never>, undefined>>\n ): UseGlobalStateResT<Exclude<ValueAtPathT<StateT, PathT, void>, undefined>>;\n\n <PathT extends null | string | undefined>(\n path: PathT,\n initialValue?: ValueOrInitializerT<ValueAtPathT<StateT, PathT, never>>\n ): UseGlobalStateResT<ValueAtPathT<StateT, PathT, void>>;\n\n <Forced extends ForceT | LockT = LockT, ValueT = unknown>(\n path: null | string | undefined,\n initialValue?: ValueOrInitializerT<TypeLock<Forced, never, ValueT>>,\n ): UseGlobalStateResT<TypeLock<Forced, void, ValueT>>;\n}\n"],"mappings":";;;;;;AAEA,IAAAA,OAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AAQA,IAAAE,QAAA,GAAAF,OAAA;AAGA,IAAAG,oBAAA,GAAAH,OAAA;AAEA,IAAAI,MAAA,GAAAJ,OAAA;AAjBA;;AA4CA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;;AASA;;AAGA;;AAkBA,SAASK,cAAcA,CACrBC,IAAoB;AACpB;AACA;AACAC;;AAEA;AACA;AAAA,EACyB;EACzB,MAAMC,WAAW,GAAG,IAAAC,mCAAc,EAAC,CAAC;EAEpC,MAAMC,GAAG,GAAG,IAAAC,aAAM,EAAiBC,SAAS,CAAC;;EAE7C;EACA;EACA,IAAIC,EAA8B,GAAGH,GAAG,CAACI,OAAO;EAChD,IAAI,CAACJ,GAAG,CAACI,OAAO,EAAE;IAChB,MAAMC,OAAO,GAAG,IAAIC,gBAAO,CAAC,CAAC;IAC7BN,GAAG,CAACI,OAAO,GAAG;MACZC,OAAO;MACPP,WAAW;MACXF,IAAI;MACJW,MAAM,EAAGC,KAAK,IAAK;QACjB,MAAMC,QAAQ,GAAG,IAAAC,kBAAU,EAACF,KAAK,CAAC,GAC9BA,KAAK,CAACL,EAAE,CAAEL,WAAW,CAACa,GAAG,CAACR,EAAE,CAAEP,IAAI,CAAC,CAAC,GACpCY,KAAK;QAET,IAAII,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,IAAI,IAAAC,kBAAW,EAAC,CAAC,EAAE;UAC1D;UACAC,OAAO,CAACC,cAAc,CACpB,+DACEd,EAAE,CAAEP,IAAI,IAAI,EAAE,EAElB,CAAC;UACDoB,OAAO,CAACE,GAAG,CAAC,YAAY,EAAE,IAAAC,sBAAe,EAACV,QAAQ,EAAEN,EAAE,CAAEP,IAAI,IAAI,EAAE,CAAC,CAAC;UACpEoB,OAAO,CAACI,QAAQ,CAAC,CAAC;UAClB;QACF;QACAjB,EAAE,CAAEL,WAAW,CAACuB,GAAG,CAAkBlB,EAAE,CAAEP,IAAI,EAAEa,QAAQ,CAAC;;QAExD;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA,IAAIA,QAAQ,KAAKN,EAAE,CAAEmB,KAAK,EAAEnB,EAAE,CAAEE,OAAO,CAACkB,IAAI,CAAC,CAAC;MAChD,CAAC;MACDD,KAAK,EAAE,IAAAZ,kBAAU,EAACb,YAAY,CAAC,GAAGA,YAAY,CAAC,CAAC,GAAGA,YAAY;MAC/D2B,SAAS,EAAEnB,OAAO,CAACoB,WAAW,CAACC,IAAI,CAACrB,OAAO,CAAC;MAC5CsB,OAAO,EAAEA,CAAA,KAAM;QACb;QACA;QACA,MAAML,KAAK,GAAGnB,EAAE,CAAEL,WAAW,CAACa,GAAG,CAACR,EAAE,CAAEP,IAAI,CAAY;QACtD,IAAI0B,KAAK,KAAKnB,EAAE,CAAEmB,KAAK,EAAEnB,EAAE,CAAEE,OAAO,CAACkB,IAAI,CAAC,CAAC;MAC7C;IACF,CAAC;EACH;EACApB,EAAE,GAAGH,GAAG,CAACI,OAAQ;EAEjBD,EAAE,CAACL,WAAW,GAAGA,WAAW;EAC5BK,EAAE,CAACP,IAAI,GAAGA,IAAI;EAEdO,EAAE,CAACmB,KAAK,GAAG,IAAAM,2BAAoB,EAC7BzB,EAAE,CAACqB,SAAS,EACZ,MAAMrB,EAAE,CAACL,WAAW,CAACa,GAAG,CAAkBR,EAAE,CAACP,IAAI,EAAE;IAAEC;EAAa,CAAC,CAAC,EAEpE,MAAMM,EAAE,CAACL,WAAW,CAACa,GAAG,CACtBR,EAAE,CAACP,IAAI,EACP;IAAEiC,YAAY,EAAE,IAAI;IAAEhC;EAAa,CACrC,CACF,CAAC;EAED,IAAAiC,gBAAS,EAAC,MAAM;IACd,MAAM;MAAEH;IAAQ,CAAC,GAAG3B,GAAG,CAACI,OAAQ;IAChCN,WAAW,CAACiC,KAAK,CAACJ,OAAO,CAAC;IAC1BA,OAAO,CAAC,CAAC;IACT,OAAO,MAAM;MACX7B,WAAW,CAACkC,OAAO,CAACL,OAAO,CAAC;IAC9B,CAAC;EACH,CAAC,EAAE,CAAC7B,WAAW,CAAC,CAAC;EAEjB,IAAAgC,gBAAS,EAAC,MAAM;IACd9B,GAAG,CAACI,OAAO,CAAEuB,OAAO,CAAC,CAAC;EACxB,CAAC,EAAE,CAAC/B,IAAI,CAAC,CAAC;EAEV,OAAO,CAACO,EAAE,CAACmB,KAAK,EAAEnB,EAAE,CAACI,MAAM,CAAC;AAC9B;AAAC,IAAA0B,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEcxC,cAAc,EAE7B;AACA","ignoreList":[]}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.cloneDeepForLog = cloneDeepForLog;
|
|
7
|
+
exports.escape = escape;
|
|
8
|
+
exports.hash = hash;
|
|
9
|
+
exports.isDebugMode = isDebugMode;
|
|
10
|
+
var _lodash = require("lodash");
|
|
11
|
+
// TODO: This (ForceT, LockT, and TypeLock) probably should be moved to JS Utils
|
|
12
|
+
// lib.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Given the type of state, `StateT`, and the type of state path, `PathT`,
|
|
16
|
+
* it evaluates the type of value at that path of the state, if it can be
|
|
17
|
+
* evaluated from the path type (it is possible when `PathT` is a string
|
|
18
|
+
* literal type, and `StateT` elements along this path have appropriate
|
|
19
|
+
* types); otherwise it falls back to the specified `UnknownT` type,
|
|
20
|
+
* which should be set either `never` (for input arguments), or `void`
|
|
21
|
+
* (for return types) - `never` and `void` in those places forbid assignments,
|
|
22
|
+
* and are not auto-inferred to more permissible types.
|
|
23
|
+
*
|
|
24
|
+
* BEWARE: When StateT is any the construct resolves to any for any string
|
|
25
|
+
* paths.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns 'true' if debug logging should be performed; 'false' otherwise.
|
|
30
|
+
*
|
|
31
|
+
* BEWARE: The actual safeguards for the debug logging still should read
|
|
32
|
+
* if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
|
|
33
|
+
* // Some debug logging
|
|
34
|
+
* }
|
|
35
|
+
* to ensure that debug code is stripped out by Webpack in production mode.
|
|
36
|
+
*
|
|
37
|
+
* @returns
|
|
38
|
+
* @ignore
|
|
39
|
+
*/
|
|
40
|
+
function isDebugMode() {
|
|
41
|
+
try {
|
|
42
|
+
return process.env.NODE_ENV !== 'production' && !!process.env.REACT_GLOBAL_STATE_DEBUG;
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const cloneDeepBailKeys = new Set();
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Deep-clones given value for logging purposes, or returns the value itself
|
|
51
|
+
* if the previous clone attempt, with the same key, took more than 300ms
|
|
52
|
+
* (to avoid situations when large payload in the global state slows down
|
|
53
|
+
* development versions of the app due to the logging overhead).
|
|
54
|
+
*/
|
|
55
|
+
function cloneDeepForLog(value, key = '') {
|
|
56
|
+
if (cloneDeepBailKeys.has(key)) {
|
|
57
|
+
// eslint-disable-next-line no-console
|
|
58
|
+
console.warn(`The logged value won't be clonned (key "${key}").`);
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
const start = Date.now();
|
|
62
|
+
const res = (0, _lodash.cloneDeep)(value);
|
|
63
|
+
const time = Date.now() - start;
|
|
64
|
+
if (time > 300) {
|
|
65
|
+
// eslint-disable-next-line no-console
|
|
66
|
+
console.warn(`${time}ms spent to clone the logged value (key "${key}").`);
|
|
67
|
+
cloneDeepBailKeys.add(key);
|
|
68
|
+
}
|
|
69
|
+
return res;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Converts given value to an escaped string. For now, we are good with the most
|
|
74
|
+
* trivial escape logic:
|
|
75
|
+
* - '%' characters are replaced by '%0' code pair;
|
|
76
|
+
* - '/' characters are replaced by '%1' code pair.
|
|
77
|
+
*/
|
|
78
|
+
function escape(x) {
|
|
79
|
+
return typeof x === 'string' ? x.replace(/%/g, '%0').replace(/\//g, '%1') : x.toString();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Hashes given string array. For our current needs we are fine to go with
|
|
84
|
+
* the most trivial implementation, which probably should not be called "hash"
|
|
85
|
+
* in the strict sense: we just escape each given string to not include '/'
|
|
86
|
+
* characters, and then we join all those strings using '/' as the separator.
|
|
87
|
+
*/
|
|
88
|
+
function hash(items) {
|
|
89
|
+
return items.map(escape).join('/');
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":["_lodash","require","isDebugMode","process","env","NODE_ENV","REACT_GLOBAL_STATE_DEBUG","cloneDeepBailKeys","Set","cloneDeepForLog","value","key","has","console","warn","start","Date","now","res","cloneDeep","time","add","escape","x","replace","toString","hash","items","map","join"],"sources":["../../src/utils.ts"],"sourcesContent":["import { type GetFieldType, cloneDeep } from 'lodash';\n\nexport type CallbackT = () => void;\n\n// TODO: This (ForceT, LockT, and TypeLock) probably should be moved to JS Utils\n// lib.\n\nexport declare const force: unique symbol;\nexport declare const lock: unique symbol;\n\nexport type ForceT = typeof force;\nexport type LockT = typeof lock;\n\nexport type TypeLock<\n Unlocked extends ForceT | LockT,\n\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents\n LockedT extends never | void,\n UnlockedT,\n> = Unlocked extends ForceT ? UnlockedT : LockedT;\n\n/**\n * Given the type of state, `StateT`, and the type of state path, `PathT`,\n * it evaluates the type of value at that path of the state, if it can be\n * evaluated from the path type (it is possible when `PathT` is a string\n * literal type, and `StateT` elements along this path have appropriate\n * types); otherwise it falls back to the specified `UnknownT` type,\n * which should be set either `never` (for input arguments), or `void`\n * (for return types) - `never` and `void` in those places forbid assignments,\n * and are not auto-inferred to more permissible types.\n *\n * BEWARE: When StateT is any the construct resolves to any for any string\n * paths.\n */\nexport type ValueAtPathT<\n StateT,\n PathT extends null | string | undefined,\n\n // TODO: Revise later.\n // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents, @typescript-eslint/no-invalid-void-type\n UnknownT extends never | undefined | void,\n> = unknown extends StateT\n ? UnknownT\n : string extends PathT\n ? UnknownT\n : PathT extends null | undefined\n ? StateT\n : GetFieldType<StateT, PathT> extends undefined\n ? UnknownT : GetFieldType<StateT, PathT>;\n\nexport type ValueOrInitializerT<ValueT> = ValueT | (() => ValueT);\n\n/**\n * Returns 'true' if debug logging should be performed; 'false' otherwise.\n *\n * BEWARE: The actual safeguards for the debug logging still should read\n * if (process.env.NODE_ENV !== 'production' && isDebugMode()) {\n * // Some debug logging\n * }\n * to ensure that debug code is stripped out by Webpack in production mode.\n *\n * @returns\n * @ignore\n */\nexport function isDebugMode(): boolean {\n try {\n return process.env.NODE_ENV !== 'production'\n && !!process.env.REACT_GLOBAL_STATE_DEBUG;\n } catch {\n return false;\n }\n}\n\nconst cloneDeepBailKeys = new Set<string>();\n\n/**\n * Deep-clones given value for logging purposes, or returns the value itself\n * if the previous clone attempt, with the same key, took more than 300ms\n * (to avoid situations when large payload in the global state slows down\n * development versions of the app due to the logging overhead).\n */\nexport function cloneDeepForLog<T>(value: T, key: string = ''): T {\n if (cloneDeepBailKeys.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`The logged value won't be clonned (key \"${key}\").`);\n return value;\n }\n\n const start = Date.now();\n const res = cloneDeep(value);\n const time = Date.now() - start;\n if (time > 300) {\n // eslint-disable-next-line no-console\n console.warn(`${time}ms spent to clone the logged value (key \"${key}\").`);\n cloneDeepBailKeys.add(key);\n }\n\n return res;\n}\n\n/**\n * Converts given value to an escaped string. For now, we are good with the most\n * trivial escape logic:\n * - '%' characters are replaced by '%0' code pair;\n * - '/' characters are replaced by '%1' code pair.\n */\nexport function escape(x: number | string): string {\n return typeof x === 'string'\n ? x.replace(/%/g, '%0').replace(/\\//g, '%1')\n : x.toString();\n}\n\n/**\n * Hashes given string array. For our current needs we are fine to go with\n * the most trivial implementation, which probably should not be called \"hash\"\n * in the strict sense: we just escape each given string to not include '/'\n * characters, and then we join all those strings using '/' as the separator.\n */\nexport function hash(items: Array<number | string>): string {\n return items.map(escape).join('/');\n}\n"],"mappings":";;;;;;;;;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAIA;AACA;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,WAAWA,CAAA,EAAY;EACrC,IAAI;IACF,OAAOC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,IACvC,CAAC,CAACF,OAAO,CAACC,GAAG,CAACE,wBAAwB;EAC7C,CAAC,CAAC,MAAM;IACN,OAAO,KAAK;EACd;AACF;AAEA,MAAMC,iBAAiB,GAAG,IAAIC,GAAG,CAAS,CAAC;;AAE3C;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,eAAeA,CAAIC,KAAQ,EAAEC,GAAW,GAAG,EAAE,EAAK;EAChE,IAAIJ,iBAAiB,CAACK,GAAG,CAACD,GAAG,CAAC,EAAE;IAC9B;IACAE,OAAO,CAACC,IAAI,CAAC,2CAA2CH,GAAG,KAAK,CAAC;IACjE,OAAOD,KAAK;EACd;EAEA,MAAMK,KAAK,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC;EACxB,MAAMC,GAAG,GAAG,IAAAC,iBAAS,EAACT,KAAK,CAAC;EAC5B,MAAMU,IAAI,GAAGJ,IAAI,CAACC,GAAG,CAAC,CAAC,GAAGF,KAAK;EAC/B,IAAIK,IAAI,GAAG,GAAG,EAAE;IACd;IACAP,OAAO,CAACC,IAAI,CAAC,GAAGM,IAAI,4CAA4CT,GAAG,KAAK,CAAC;IACzEJ,iBAAiB,CAACc,GAAG,CAACV,GAAG,CAAC;EAC5B;EAEA,OAAOO,GAAG;AACZ;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,SAASI,MAAMA,CAACC,CAAkB,EAAU;EACjD,OAAO,OAAOA,CAAC,KAAK,QAAQ,GACxBA,CAAC,CAACC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAACA,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,GAC1CD,CAAC,CAACE,QAAQ,CAAC,CAAC;AAClB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,IAAIA,CAACC,KAA6B,EAAU;EAC1D,OAAOA,KAAK,CAACC,GAAG,CAACN,MAAM,CAAC,CAACO,IAAI,CAAC,GAAG,CAAC;AACpC","ignoreList":[]}
|
|
@@ -36,8 +36,7 @@ export function getGlobalState() {
|
|
|
36
36
|
* - If `throwWithoutSsrContext` is `true`, and there is no SSR context attached
|
|
37
37
|
* to the global state provided by {@link <GlobalStateProvider>}.
|
|
38
38
|
*/
|
|
39
|
-
export function getSsrContext() {
|
|
40
|
-
let throwWithoutSsrContext = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
39
|
+
export function getSsrContext(throwWithoutSsrContext = true) {
|
|
41
40
|
const {
|
|
42
41
|
ssrContext
|
|
43
42
|
} = getGlobalState();
|
|
@@ -59,11 +58,10 @@ export function getSsrContext() {
|
|
|
59
58
|
* - If `GlobalState` instance, it will be used by this provider.
|
|
60
59
|
* - If not given, a new `GlobalState` instance will be created and used.
|
|
61
60
|
*/
|
|
62
|
-
const GlobalStateProvider =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
} = _ref;
|
|
61
|
+
const GlobalStateProvider = ({
|
|
62
|
+
children,
|
|
63
|
+
...rest
|
|
64
|
+
}) => {
|
|
67
65
|
const localStateRef = useRef(undefined);
|
|
68
66
|
let state;
|
|
69
67
|
// TODO: Revise.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStateProvider.js","names":["isFunction","createContext","use","useRef","GlobalState","jsx","_jsx","Context","getGlobalState","globalState","Error","getSsrContext","throwWithoutSsrContext","
|
|
1
|
+
{"version":3,"file":"GlobalStateProvider.js","names":["isFunction","createContext","use","useRef","GlobalState","jsx","_jsx","Context","getGlobalState","globalState","Error","getSsrContext","throwWithoutSsrContext","ssrContext","GlobalStateProvider","children","rest","localStateRef","undefined","state","stateProxy","current","initialState","value"],"sources":["../../src/GlobalStateProvider.tsx"],"sourcesContent":["import { isFunction } from 'lodash';\n\nimport {\n type ReactNode,\n createContext,\n use,\n useRef,\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 * Gets {@link GlobalState} instance from the context. In most cases\n * you should use {@link useGlobalState}, and other hooks to interact with\n * the global state, instead of accessing it directly.\n * @return\n */\nexport function getGlobalState<\n StateT,\n SsrContextT extends SsrContext<StateT> = SsrContext<StateT>,\n>(): GlobalState<StateT, SsrContextT> {\n // TODO: Think about it: on one hand we on purpose called this function\n // as getGlobalState(), so that ppl looking for the state hook prefer using\n // useGlobalState(), while this getGlobalState() is reserved for nieche cases;\n // on the other hand, perhaps we can rename it into useSomething, to both\n // follow conventions, and to keep stuff clearly named at the same time.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n const globalState = use(Context);\n if (!globalState) throw new Error('Missing GlobalStateProvider');\n return globalState as GlobalState<StateT, SsrContextT>;\n}\n\n/**\n * @category Hooks\n * @desc Gets SSR context.\n * @param throwWithoutSsrContext If `true` (default),\n * this hook will throw if no SSR context is attached to the global state;\n * set `false` to not throw in such case. In either case the hook will throw\n * if the {@link <GlobalStateProvider>} (hence the state) is missing.\n * @returns SSR context.\n * @throws\n * - If current component has no parent {@link <GlobalStateProvider>}\n * in the rendered React tree.\n * - If `throwWithoutSsrContext` is `true`, and there is no SSR context attached\n * to the global state provided by {@link <GlobalStateProvider>}.\n */\nexport function getSsrContext<\n SsrContextT extends SsrContext<unknown>,\n>(\n throwWithoutSsrContext = true,\n): SsrContextT | undefined {\n const { ssrContext } = getGlobalState<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> = {\n children?: ReactNode;\n} & (NewStateProps<StateT, SsrContextT> | {\n stateProxy: true | GlobalState<StateT, SsrContextT>;\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 const localStateRef = useRef<GST>(undefined);\n let state: GST;\n // TODO: Revise.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if ('stateProxy' in rest && rest.stateProxy) {\n localStateRef.current = undefined;\n state = rest.stateProxy === true ? getGlobalState() : rest.stateProxy;\n } else {\n if (!localStateRef.current) {\n const {\n initialState,\n ssrContext,\n } = rest as NewStateProps<StateT, SsrContextT>;\n localStateRef.current = new GlobalState(\n isFunction(initialState) ? initialState() : initialState,\n ssrContext,\n );\n }\n state = localStateRef.current;\n }\n return <Context value={state}>{children}</Context>;\n};\n\nexport default GlobalStateProvider;\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,QAAQ;AAEnC,SAEEC,aAAa,EACbC,GAAG,EACHC,MAAM,QACD,OAAO;AAEd,OAAOC,WAAW;AAAsB,SAAAC,GAAA,IAAAC,IAAA;AAKxC,MAAMC,OAAO,gBAAGN,aAAa,CAA8B,IAAI,CAAC;;AAEhE;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASO,cAAcA,CAAA,EAGQ;EACpC;EACA;EACA;EACA;EACA;EACA;EACA,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;AACA,OAAO,SAASE,aAAaA,CAG3BC,sBAAsB,GAAG,IAAI,EACJ;EACzB,MAAM;IAAEC;EAAW,CAAC,GAAGL,cAAc,CAAoC,CAAC;EAC1E,IAAI,CAACK,UAAU,IAAID,sBAAsB,EAAE;IACzC,MAAM,IAAIF,KAAK,CAAC,sBAAsB,CAAC;EACzC;EACA,OAAOG,UAAU;AACnB;AAgBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,mBAAmB,GAAGA,CAI1B;EAAEC,QAAQ;EAAE,GAAGC;AAAoD,CAAC,KACtD;EAEd,MAAMC,aAAa,GAAGd,MAAM,CAAMe,SAAS,CAAC;EAC5C,IAAIC,KAAU;EACd;EACA;EACA,IAAI,YAAY,IAAIH,IAAI,IAAIA,IAAI,CAACI,UAAU,EAAE;IAC3CH,aAAa,CAACI,OAAO,GAAGH,SAAS;IACjCC,KAAK,GAAGH,IAAI,CAACI,UAAU,KAAK,IAAI,GAAGZ,cAAc,CAAC,CAAC,GAAGQ,IAAI,CAACI,UAAU;EACvE,CAAC,MAAM;IACL,IAAI,CAACH,aAAa,CAACI,OAAO,EAAE;MAC1B,MAAM;QACJC,YAAY;QACZT;MACF,CAAC,GAAGG,IAA0C;MAC9CC,aAAa,CAACI,OAAO,GAAG,IAAIjB,WAAW,CACrCJ,UAAU,CAACsB,YAAY,CAAC,GAAGA,YAAY,CAAC,CAAC,GAAGA,YAAY,EACxDT,UACF,CAAC;IACH;IACAM,KAAK,GAAGF,aAAa,CAACI,OAAO;EAC/B;EACA,oBAAOf,IAAA,CAACC,OAAO;IAACgB,KAAK,EAAEJ,KAAM;IAAAJ,QAAA,EAAEA;EAAQ,CAAU,CAAC;AACpD,CAAC;AAED,eAAeD,mBAAmB","ignoreList":[]}
|
|
@@ -72,7 +72,10 @@ function gcOnRelease(ids, path, gs, gcAge) {
|
|
|
72
72
|
}
|
|
73
73
|
function normalizeIds(idOrIds) {
|
|
74
74
|
if (Array.isArray(idOrIds)) {
|
|
75
|
-
|
|
75
|
+
// Removes ID duplicates.
|
|
76
|
+
const res = Array.from(new Set(idOrIds));
|
|
77
|
+
|
|
78
|
+
// Ensures stable ID order.
|
|
76
79
|
res.sort((a, b) => a.toString().localeCompare(b.toString()));
|
|
77
80
|
return res;
|
|
78
81
|
}
|
|
@@ -134,12 +137,7 @@ function useHeap(ids, path, loader, gs) {
|
|
|
134
137
|
// happens if the outer function on the next line matches the same
|
|
135
138
|
// async / sync signature.
|
|
136
139
|
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
137
|
-
customLoader &&
|
|
138
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
139
|
-
args[_key - 1] = arguments[_key];
|
|
140
|
-
}
|
|
141
|
-
return customLoader(...args);
|
|
142
|
-
})
|
|
140
|
+
customLoader && ((id, ...args) => customLoader(...args)))
|
|
143
141
|
};
|
|
144
142
|
ref.current = heap;
|
|
145
143
|
}
|
|
@@ -155,9 +153,8 @@ function useHeap(ids, path, loader, gs) {
|
|
|
155
153
|
// Perhaps, a bunch of logic blocks can be split into stand-alone functions,
|
|
156
154
|
// and reused in both hooks.
|
|
157
155
|
// eslint-disable-next-line complexity
|
|
158
|
-
function useAsyncCollection(idOrIds, path, loader) {
|
|
156
|
+
function useAsyncCollection(idOrIds, path, loader, options = {}) {
|
|
159
157
|
var _options$maxage, _options$refreshAge, _options$garbageColle;
|
|
160
|
-
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
161
158
|
const ids = normalizeIds(idOrIds);
|
|
162
159
|
const maxage = (_options$maxage = options.maxage) !== null && _options$maxage !== void 0 ? _options$maxage : DEFAULT_MAXAGE;
|
|
163
160
|
const refreshAge = (_options$refreshAge = options.refreshAge) !== null && _options$refreshAge !== void 0 ? _options$refreshAge : maxage;
|
|
@@ -166,31 +163,32 @@ function useAsyncCollection(idOrIds, path, loader) {
|
|
|
166
163
|
const heap = useHeap(ids, path, loader, globalState);
|
|
167
164
|
|
|
168
165
|
// Server-side logic.
|
|
169
|
-
if (globalState.ssrContext
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
166
|
+
if (globalState.ssrContext) {
|
|
167
|
+
if (!options.disabled && !options.noSSR) {
|
|
168
|
+
const operationId = `S${uuid()}`;
|
|
169
|
+
for (const id of ids) {
|
|
170
|
+
const itemPath = path ? `${path}.${id}` : `${id}`;
|
|
171
|
+
const state = globalState.get(itemPath, {
|
|
172
|
+
initialValue: newAsyncDataEnvelope()
|
|
173
|
+
});
|
|
174
|
+
if (!state.timestamp && !state.operationId) {
|
|
175
|
+
const promiseOrVoid = load(itemPath, (...args) => loader(id, ...args), globalState, {
|
|
176
|
+
data: state.data,
|
|
177
|
+
timestamp: state.timestamp
|
|
178
|
+
}, operationId);
|
|
179
|
+
if (promiseOrVoid instanceof Promise) {
|
|
180
|
+
globalState.ssrContext.pending.push(promiseOrVoid);
|
|
180
181
|
}
|
|
181
|
-
return loader(id, ...args);
|
|
182
|
-
}, globalState, {
|
|
183
|
-
data: state.data,
|
|
184
|
-
timestamp: state.timestamp
|
|
185
|
-
}, operationId);
|
|
186
|
-
if (promiseOrVoid instanceof Promise) {
|
|
187
|
-
globalState.ssrContext.pending.push(promiseOrVoid);
|
|
188
182
|
}
|
|
189
183
|
}
|
|
190
184
|
}
|
|
191
185
|
|
|
192
186
|
// Client-side logic.
|
|
193
187
|
} else {
|
|
188
|
+
const {
|
|
189
|
+
disabled
|
|
190
|
+
} = options;
|
|
191
|
+
|
|
194
192
|
// Reference-counting & garbage collection.
|
|
195
193
|
|
|
196
194
|
const idsHash = hash(ids);
|
|
@@ -199,15 +197,15 @@ function useAsyncCollection(idOrIds, path, loader) {
|
|
|
199
197
|
// but perhaps it can be refactored to avoid the need for it.
|
|
200
198
|
useEffect(() => {
|
|
201
199
|
// eslint-disable-line react-hooks/rules-of-hooks
|
|
202
|
-
gcOnWithhold(ids, path, globalState);
|
|
200
|
+
if (!disabled) gcOnWithhold(ids, path, globalState);
|
|
203
201
|
return () => {
|
|
204
|
-
gcOnRelease(ids, path, globalState, garbageCollectAge);
|
|
202
|
+
if (!disabled) gcOnRelease(ids, path, globalState, garbageCollectAge);
|
|
205
203
|
};
|
|
206
204
|
|
|
207
205
|
// `ids` are represented in the dependencies array by `idsHash` value,
|
|
208
206
|
// as useEffect() hook requires a constant size of dependencies array.
|
|
209
207
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
210
|
-
}, [garbageCollectAge, globalState, idsHash, path]);
|
|
208
|
+
}, [disabled, garbageCollectAge, globalState, idsHash, path]);
|
|
211
209
|
|
|
212
210
|
// NOTE: a bunch of Rules of Hooks ignored belows because in our very
|
|
213
211
|
// special case the otherwise wrong behavior is actually what we need.
|
|
@@ -215,34 +213,31 @@ function useAsyncCollection(idOrIds, path, loader) {
|
|
|
215
213
|
// Data loading and refreshing.
|
|
216
214
|
useEffect(() => {
|
|
217
215
|
// eslint-disable-line react-hooks/rules-of-hooks
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
data: state2 === null || state2 === void 0 ? void 0 : state2.data,
|
|
241
|
-
timestamp: (_state2$timestamp2 = state2 === null || state2 === void 0 ? void 0 : state2.timestamp) !== null && _state2$timestamp2 !== void 0 ? _state2$timestamp2 : 0
|
|
242
|
-
});
|
|
216
|
+
if (!disabled) {
|
|
217
|
+
void (async () => {
|
|
218
|
+
for (const id of ids) {
|
|
219
|
+
var _state2$timestamp;
|
|
220
|
+
const itemPath = path ? `${path}.${id}` : `${id}`;
|
|
221
|
+
const state2 = globalState.get(itemPath);
|
|
222
|
+
const {
|
|
223
|
+
deps
|
|
224
|
+
} = options;
|
|
225
|
+
if (deps && globalState.hasChangedDependencies(itemPath, 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'))) {
|
|
226
|
+
var _state2$timestamp2;
|
|
227
|
+
if (!deps) globalState.dropDependencies(itemPath);
|
|
228
|
+
await load(itemPath,
|
|
229
|
+
// TODO: I guess, the loader is not correctly typed here -
|
|
230
|
+
// it can be synchronous, and in that case the following method
|
|
231
|
+
// should be kept synchronous to not alter the sync logic.
|
|
232
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
233
|
+
(old, ...args) => loader(id, old, ...args), globalState, {
|
|
234
|
+
data: state2 === null || state2 === void 0 ? void 0 : state2.data,
|
|
235
|
+
timestamp: (_state2$timestamp2 = state2 === null || state2 === void 0 ? void 0 : state2.timestamp) !== null && _state2$timestamp2 !== void 0 ? _state2$timestamp2 : 0
|
|
236
|
+
});
|
|
237
|
+
}
|
|
243
238
|
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
239
|
+
})();
|
|
240
|
+
}
|
|
246
241
|
});
|
|
247
242
|
}
|
|
248
243
|
const [localState] = useGlobalState(path, {});
|