@dr.pogodin/react-global-state 0.18.0 → 0.19.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/LICENSE.md +1 -1
- package/build/{module → code}/GlobalState.js +15 -5
- package/build/code/GlobalState.js.map +1 -0
- package/build/{module → code}/GlobalStateProvider.js +18 -18
- package/build/code/GlobalStateProvider.js.map +1 -0
- package/build/code/SsrContext.js.map +1 -0
- package/build/{module → code}/index.js +9 -3
- package/build/code/index.js.map +1 -0
- package/build/{module → code}/useAsyncCollection.js +60 -27
- package/build/code/useAsyncCollection.js.map +1 -0
- package/build/{module → code}/useAsyncData.js +19 -17
- package/build/code/useAsyncData.js.map +1 -0
- package/build/{module → code}/useGlobalState.js +25 -8
- package/build/code/useGlobalState.js.map +1 -0
- package/build/{module → code}/utils.js +1 -1
- package/build/code/utils.js.map +1 -0
- package/build/types/GlobalState.d.ts +2 -2
- package/build/types/GlobalStateProvider.d.ts +3 -3
- package/build/types/SsrContext.d.ts +3 -3
- package/build/types/useAsyncCollection.d.ts +4 -6
- package/build/types/useAsyncData.d.ts +2 -2
- package/build/types/utils.d.ts +2 -3
- package/eslint.config.mjs +19 -0
- package/package.json +25 -41
- package/build/common/GlobalState.js +0 -279
- package/build/common/GlobalState.js.map +0 -1
- package/build/common/GlobalStateProvider.js +0 -97
- package/build/common/GlobalStateProvider.js.map +0 -1
- package/build/common/SsrContext.js +0 -15
- package/build/common/SsrContext.js.map +0 -1
- package/build/common/index.js +0 -84
- package/build/common/index.js.map +0 -1
- package/build/common/useAsyncCollection.js +0 -242
- package/build/common/useAsyncCollection.js.map +0 -1
- package/build/common/useAsyncData.js +0 -233
- package/build/common/useAsyncData.js.map +0 -1
- package/build/common/useGlobalState.js +0 -134
- package/build/common/useGlobalState.js.map +0 -1
- package/build/common/utils.js +0 -91
- package/build/common/utils.js.map +0 -1
- package/build/module/GlobalState.js.map +0 -1
- package/build/module/GlobalStateProvider.js.map +0 -1
- package/build/module/SsrContext.js.map +0 -1
- package/build/module/index.js.map +0 -1
- package/build/module/useAsyncCollection.js.map +0 -1
- package/build/module/useAsyncData.js.map +0 -1
- package/build/module/useGlobalState.js.map +0 -1
- package/build/module/utils.js.map +0 -1
- package/src/GlobalState.ts +0 -342
- package/src/GlobalStateProvider.tsx +0 -117
- package/src/SsrContext.ts +0 -11
- package/src/index.ts +0 -84
- package/src/useAsyncCollection.ts +0 -471
- package/src/useAsyncData.ts +0 -362
- package/src/useGlobalState.ts +0 -225
- package/src/utils.ts +0 -116
- /package/build/{module → code}/SsrContext.js +0 -0
|
@@ -1,471 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Loads and uses item(s) in an async collection.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { useEffect, useRef } from 'react';
|
|
6
|
-
import { v4 as uuid } from 'uuid';
|
|
7
|
-
|
|
8
|
-
import GlobalState from './GlobalState';
|
|
9
|
-
import { getGlobalState } from './GlobalStateProvider';
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
type AsyncDataEnvelopeT,
|
|
13
|
-
type AsyncDataReloaderT,
|
|
14
|
-
type DataInEnvelopeAtPathT,
|
|
15
|
-
type OperationIdT,
|
|
16
|
-
type UseAsyncDataOptionsT,
|
|
17
|
-
type UseAsyncDataResT,
|
|
18
|
-
DEFAULT_MAXAGE,
|
|
19
|
-
load,
|
|
20
|
-
newAsyncDataEnvelope,
|
|
21
|
-
} from './useAsyncData';
|
|
22
|
-
|
|
23
|
-
import useGlobalState from './useGlobalState';
|
|
24
|
-
|
|
25
|
-
import {
|
|
26
|
-
type ForceT,
|
|
27
|
-
type LockT,
|
|
28
|
-
type TypeLock,
|
|
29
|
-
hash,
|
|
30
|
-
isDebugMode,
|
|
31
|
-
} from './utils';
|
|
32
|
-
|
|
33
|
-
export type AsyncCollectionT<
|
|
34
|
-
DataT = unknown,
|
|
35
|
-
IdT extends number | string = number | string,
|
|
36
|
-
> = { [id in IdT]?: AsyncDataEnvelopeT<DataT> };
|
|
37
|
-
|
|
38
|
-
export type AsyncCollectionLoaderT<
|
|
39
|
-
DataT,
|
|
40
|
-
IdT extends number | string = number | string,
|
|
41
|
-
> =
|
|
42
|
-
(id: IdT, oldData: null | DataT, meta: {
|
|
43
|
-
isAborted: () => boolean;
|
|
44
|
-
oldDataTimestamp: number;
|
|
45
|
-
setAbortCallback: (cb: () => void) => void;
|
|
46
|
-
}) => DataT | Promise<DataT | null> | null;
|
|
47
|
-
|
|
48
|
-
export type AsyncCollectionReloaderT<
|
|
49
|
-
DataT,
|
|
50
|
-
IdT extends number | string = number | string,
|
|
51
|
-
>
|
|
52
|
-
= (loader?: AsyncCollectionLoaderT<DataT, IdT>) => Promise<void>;
|
|
53
|
-
|
|
54
|
-
type CollectionItemT<DataT> = {
|
|
55
|
-
data: DataT | null;
|
|
56
|
-
loading: boolean;
|
|
57
|
-
timestamp: number;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export type UseAsyncCollectionResT<
|
|
61
|
-
DataT,
|
|
62
|
-
IdT extends number | string = number | string,
|
|
63
|
-
> = {
|
|
64
|
-
items: {
|
|
65
|
-
[id in IdT]: CollectionItemT<DataT>;
|
|
66
|
-
}
|
|
67
|
-
loading: boolean;
|
|
68
|
-
reload: AsyncCollectionReloaderT<DataT, IdT>;
|
|
69
|
-
timestamp: number;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
type HeapT<
|
|
73
|
-
DataT,
|
|
74
|
-
IdT extends number | string,
|
|
75
|
-
> = {
|
|
76
|
-
// Note: these heap fields are necessary to make reload() a stable function.
|
|
77
|
-
globalState: GlobalState<unknown>;
|
|
78
|
-
ids: IdT[];
|
|
79
|
-
path: null | string | undefined;
|
|
80
|
-
loader: AsyncCollectionLoaderT<DataT, IdT>;
|
|
81
|
-
reload: AsyncCollectionReloaderT<DataT, IdT>;
|
|
82
|
-
reloadSingle: AsyncDataReloaderT<DataT>;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* GarbageCollector: the piece of logic executed on mounting of
|
|
87
|
-
* an useAsyncCollection() hook, and on update of hook params, to update
|
|
88
|
-
* the state according to the new param values. It increments by 1 `numRefs`
|
|
89
|
-
* counters for the requested collection items.
|
|
90
|
-
*/
|
|
91
|
-
function gcOnWithhold<IdT extends number | string>(
|
|
92
|
-
ids: IdT[],
|
|
93
|
-
path: null | string | undefined,
|
|
94
|
-
gs: GlobalState<unknown>,
|
|
95
|
-
) {
|
|
96
|
-
const collection = { ...gs.get<ForceT, AsyncCollectionT>(path) };
|
|
97
|
-
|
|
98
|
-
for (let i = 0; i < ids.length; ++i) {
|
|
99
|
-
const id = ids[i]!;
|
|
100
|
-
let envelope = collection[id];
|
|
101
|
-
if (envelope) envelope = { ...envelope, numRefs: 1 + envelope.numRefs };
|
|
102
|
-
else envelope = newAsyncDataEnvelope<unknown>(null, { numRefs: 1 });
|
|
103
|
-
collection[id] = envelope;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
gs.set<ForceT, AsyncCollectionT>(path, collection);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function idsToStringSet<IdT extends number | string>(ids: IdT[]): Set<string> {
|
|
110
|
-
const res = new Set<string>();
|
|
111
|
-
for (let i = 0; i < ids.length; ++i) {
|
|
112
|
-
res.add(ids[i]!.toString());
|
|
113
|
-
}
|
|
114
|
-
return res;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* GarbageCollector: the piece of logic executed on un-mounting of
|
|
119
|
-
* an useAsyncCollection() hook, and on update of hook params, to clean-up
|
|
120
|
-
* after the previous param values. It decrements by 1 `numRefs` counters
|
|
121
|
-
* for previously requested collection items, and also drops from the state
|
|
122
|
-
* stale records.
|
|
123
|
-
*/
|
|
124
|
-
function gcOnRelease<IdT extends number | string>(
|
|
125
|
-
ids: IdT[],
|
|
126
|
-
path: null | string | undefined,
|
|
127
|
-
gs: GlobalState<unknown>,
|
|
128
|
-
gcAge: number,
|
|
129
|
-
) {
|
|
130
|
-
type EnvelopeT = AsyncDataEnvelopeT<unknown>;
|
|
131
|
-
|
|
132
|
-
const entries = Object.entries<EnvelopeT | undefined>(
|
|
133
|
-
gs.get<ForceT, AsyncCollectionT>(path),
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
const now = Date.now();
|
|
137
|
-
const idSet = idsToStringSet(ids);
|
|
138
|
-
const collection: AsyncCollectionT = {};
|
|
139
|
-
for (let i = 0; i < entries.length; ++i) {
|
|
140
|
-
const [id, envelope] = entries[i]!;
|
|
141
|
-
|
|
142
|
-
if (envelope) {
|
|
143
|
-
const toBeReleased = idSet.has(id);
|
|
144
|
-
|
|
145
|
-
let { numRefs } = envelope;
|
|
146
|
-
if (toBeReleased) --numRefs;
|
|
147
|
-
|
|
148
|
-
if (gcAge > now - envelope.timestamp || numRefs > 0) {
|
|
149
|
-
collection[id as IdT] = toBeReleased
|
|
150
|
-
? { ...envelope, numRefs }
|
|
151
|
-
: envelope;
|
|
152
|
-
} else if (process.env.NODE_ENV !== 'production' && isDebugMode()) {
|
|
153
|
-
// eslint-disable-next-line no-console
|
|
154
|
-
console.log(
|
|
155
|
-
`useAsyncCollection(): Garbage collected at the path "${
|
|
156
|
-
path}", ID = ${id}`,
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
gs.set<ForceT, AsyncCollectionT>(path, collection);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function normalizeIds<IdT extends number | string>(
|
|
166
|
-
idOrIds: IdT | IdT[],
|
|
167
|
-
): IdT[] {
|
|
168
|
-
if (Array.isArray(idOrIds)) {
|
|
169
|
-
const res = [...idOrIds];
|
|
170
|
-
res.sort();
|
|
171
|
-
return res;
|
|
172
|
-
}
|
|
173
|
-
return [idOrIds];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Inits/updates, and returns the heap.
|
|
178
|
-
*/
|
|
179
|
-
function useHeap<
|
|
180
|
-
DataT,
|
|
181
|
-
IdT extends number | string,
|
|
182
|
-
>(
|
|
183
|
-
ids: IdT[],
|
|
184
|
-
path: null | string | undefined,
|
|
185
|
-
loader: AsyncCollectionLoaderT<DataT, IdT>,
|
|
186
|
-
gs: GlobalState<unknown>,
|
|
187
|
-
): HeapT<DataT, IdT> {
|
|
188
|
-
const ref = useRef<HeapT<DataT, IdT>>(undefined);
|
|
189
|
-
|
|
190
|
-
let heap = ref.current;
|
|
191
|
-
|
|
192
|
-
if (heap) {
|
|
193
|
-
// Update.
|
|
194
|
-
heap.ids = ids;
|
|
195
|
-
heap.path = path;
|
|
196
|
-
heap.loader = loader;
|
|
197
|
-
heap.globalState = gs;
|
|
198
|
-
} else {
|
|
199
|
-
// Initialization.
|
|
200
|
-
const reload = async (
|
|
201
|
-
customLoader?: AsyncCollectionLoaderT<DataT, IdT>,
|
|
202
|
-
) => {
|
|
203
|
-
const heap2 = ref.current!;
|
|
204
|
-
|
|
205
|
-
const localLoader = customLoader || heap2.loader;
|
|
206
|
-
if (!localLoader || !heap2.globalState || !heap2.ids) {
|
|
207
|
-
throw Error('Internal error');
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
for (let i = 0; i < heap2.ids.length; ++i) {
|
|
211
|
-
const id = heap2.ids[i]!;
|
|
212
|
-
const itemPath = heap2.path ? `${heap2.path}.${id}` : `${id}`;
|
|
213
|
-
|
|
214
|
-
// eslint-disable-next-line no-await-in-loop
|
|
215
|
-
await load(
|
|
216
|
-
itemPath,
|
|
217
|
-
(oldData: DataT | null, meta) => localLoader(id, oldData, meta),
|
|
218
|
-
heap2.globalState,
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
heap = {
|
|
223
|
-
globalState: gs,
|
|
224
|
-
ids,
|
|
225
|
-
path,
|
|
226
|
-
loader,
|
|
227
|
-
reload,
|
|
228
|
-
reloadSingle: (customLoader) => ref.current!.reload(
|
|
229
|
-
customLoader && ((id, ...args) => customLoader(...args)),
|
|
230
|
-
),
|
|
231
|
-
};
|
|
232
|
-
ref.current = heap;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return heap;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Resolves and stores at the given `path` of the global state elements of
|
|
240
|
-
* an asynchronous data collection.
|
|
241
|
-
*/
|
|
242
|
-
|
|
243
|
-
function useAsyncCollection<
|
|
244
|
-
StateT,
|
|
245
|
-
PathT extends null | string | undefined,
|
|
246
|
-
IdT extends number | string,
|
|
247
|
-
|
|
248
|
-
DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> =
|
|
249
|
-
DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,
|
|
250
|
-
>(
|
|
251
|
-
id: IdT,
|
|
252
|
-
path: PathT,
|
|
253
|
-
loader: AsyncCollectionLoaderT<DataT, IdT>,
|
|
254
|
-
options?: UseAsyncDataOptionsT,
|
|
255
|
-
): UseAsyncDataResT<DataT>;
|
|
256
|
-
|
|
257
|
-
function useAsyncCollection<
|
|
258
|
-
Forced extends ForceT | LockT = LockT,
|
|
259
|
-
DataT = unknown,
|
|
260
|
-
IdT extends number | string = number | string,
|
|
261
|
-
>(
|
|
262
|
-
id: IdT,
|
|
263
|
-
path: null | string | undefined,
|
|
264
|
-
loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,
|
|
265
|
-
options?: UseAsyncDataOptionsT,
|
|
266
|
-
): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;
|
|
267
|
-
|
|
268
|
-
function useAsyncCollection<
|
|
269
|
-
StateT,
|
|
270
|
-
PathT extends null | string | undefined,
|
|
271
|
-
IdT extends number | string,
|
|
272
|
-
|
|
273
|
-
DataT extends DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`> =
|
|
274
|
-
DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>,
|
|
275
|
-
>(
|
|
276
|
-
id: IdT[],
|
|
277
|
-
path: PathT,
|
|
278
|
-
loader: AsyncCollectionLoaderT<DataT, IdT>,
|
|
279
|
-
options?: UseAsyncDataOptionsT,
|
|
280
|
-
): UseAsyncCollectionResT<DataT, IdT>;
|
|
281
|
-
|
|
282
|
-
function useAsyncCollection<
|
|
283
|
-
Forced extends ForceT | LockT = LockT,
|
|
284
|
-
DataT = unknown,
|
|
285
|
-
IdT extends number | string = number | string,
|
|
286
|
-
>(
|
|
287
|
-
id: IdT[],
|
|
288
|
-
path: null | string | undefined,
|
|
289
|
-
loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,
|
|
290
|
-
options?: UseAsyncDataOptionsT,
|
|
291
|
-
): UseAsyncCollectionResT<DataT, IdT>;
|
|
292
|
-
|
|
293
|
-
// TODO: This is largely similar to useAsyncData() logic, just more generic.
|
|
294
|
-
// Perhaps, a bunch of logic blocks can be split into stand-alone functions,
|
|
295
|
-
// and reused in both hooks.
|
|
296
|
-
function useAsyncCollection<
|
|
297
|
-
DataT,
|
|
298
|
-
IdT extends number | string,
|
|
299
|
-
>(
|
|
300
|
-
idOrIds: IdT | IdT[],
|
|
301
|
-
path: null | string | undefined,
|
|
302
|
-
loader: AsyncCollectionLoaderT<DataT, IdT>,
|
|
303
|
-
options: UseAsyncDataOptionsT = {},
|
|
304
|
-
): UseAsyncDataResT<DataT> | UseAsyncCollectionResT<DataT, IdT> {
|
|
305
|
-
const ids = normalizeIds(idOrIds);
|
|
306
|
-
const maxage: number = options.maxage ?? DEFAULT_MAXAGE;
|
|
307
|
-
const refreshAge: number = options.refreshAge ?? maxage;
|
|
308
|
-
const garbageCollectAge: number = options.garbageCollectAge ?? maxage;
|
|
309
|
-
|
|
310
|
-
const globalState = getGlobalState();
|
|
311
|
-
|
|
312
|
-
const heap = useHeap(ids, path, loader, globalState);
|
|
313
|
-
|
|
314
|
-
// Server-side logic.
|
|
315
|
-
if (globalState.ssrContext && !options.noSSR) {
|
|
316
|
-
const operationId: OperationIdT = `S${uuid()}`;
|
|
317
|
-
for (let i = 0; i < ids.length; ++i) {
|
|
318
|
-
const id = ids[i]!;
|
|
319
|
-
const itemPath = path ? `${path}.${id}` : `${id}`;
|
|
320
|
-
const state = globalState.get<ForceT, AsyncDataEnvelopeT<DataT>>(itemPath, {
|
|
321
|
-
initialValue: newAsyncDataEnvelope<DataT>(),
|
|
322
|
-
});
|
|
323
|
-
if (!state.timestamp && !state.operationId) {
|
|
324
|
-
globalState.ssrContext.pending.push(
|
|
325
|
-
load(itemPath, (...args) => loader(id, ...args), globalState, {
|
|
326
|
-
data: state.data,
|
|
327
|
-
timestamp: state.timestamp,
|
|
328
|
-
}, operationId),
|
|
329
|
-
);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Client-side logic.
|
|
334
|
-
} else {
|
|
335
|
-
// Reference-counting & garbage collection.
|
|
336
|
-
|
|
337
|
-
const idsHash = hash(ids);
|
|
338
|
-
|
|
339
|
-
// TODO: Violation of rules of hooks is fine here,
|
|
340
|
-
// but perhaps it can be refactored to avoid the need for it.
|
|
341
|
-
useEffect(() => { // eslint-disable-line react-hooks/rules-of-hooks
|
|
342
|
-
gcOnWithhold(ids, path, globalState);
|
|
343
|
-
return () => gcOnRelease(ids, path, globalState, garbageCollectAge);
|
|
344
|
-
|
|
345
|
-
// `ids` are represented in the dependencies array by `idsHash` value,
|
|
346
|
-
// as useEffect() hook requires a constant size of dependencies array.
|
|
347
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
348
|
-
}, [
|
|
349
|
-
garbageCollectAge,
|
|
350
|
-
globalState,
|
|
351
|
-
idsHash,
|
|
352
|
-
path,
|
|
353
|
-
]);
|
|
354
|
-
|
|
355
|
-
// NOTE: a bunch of Rules of Hooks ignored belows because in our very
|
|
356
|
-
// special case the otherwise wrong behavior is actually what we need.
|
|
357
|
-
|
|
358
|
-
// Data loading and refreshing.
|
|
359
|
-
useEffect(() => { // eslint-disable-line react-hooks/rules-of-hooks
|
|
360
|
-
(async () => {
|
|
361
|
-
for (let i = 0; i < ids.length; ++i) {
|
|
362
|
-
const id = ids[i]!;
|
|
363
|
-
const itemPath = path ? `${path}.${id}` : `${id}`;
|
|
364
|
-
|
|
365
|
-
type EnvT = AsyncDataEnvelopeT<DataT> | undefined;
|
|
366
|
-
const state2: EnvT = globalState.get<ForceT, EnvT>(itemPath);
|
|
367
|
-
|
|
368
|
-
const { deps } = options;
|
|
369
|
-
if (
|
|
370
|
-
(deps && globalState.hasChangedDependencies(itemPath, deps))
|
|
371
|
-
|| (
|
|
372
|
-
refreshAge < Date.now() - (state2?.timestamp ?? 0)
|
|
373
|
-
&& (!state2?.operationId || state2.operationId.charAt(0) === 'S')
|
|
374
|
-
)
|
|
375
|
-
) {
|
|
376
|
-
if (!deps) globalState.dropDependencies(itemPath);
|
|
377
|
-
// eslint-disable-next-line no-await-in-loop
|
|
378
|
-
await load(
|
|
379
|
-
itemPath,
|
|
380
|
-
(old, ...args) => loader(id, old as DataT, ...args),
|
|
381
|
-
globalState,
|
|
382
|
-
{
|
|
383
|
-
data: state2?.data,
|
|
384
|
-
timestamp: state2?.timestamp ?? 0,
|
|
385
|
-
},
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
})();
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
const [localState] = useGlobalState<
|
|
394
|
-
ForceT, { [id: string]: AsyncDataEnvelopeT<DataT> }
|
|
395
|
-
>(path, {});
|
|
396
|
-
|
|
397
|
-
if (!Array.isArray(idOrIds)) {
|
|
398
|
-
const e = localState[idOrIds];
|
|
399
|
-
const timestamp = e?.timestamp ?? 0;
|
|
400
|
-
return {
|
|
401
|
-
data: maxage < Date.now() - timestamp ? null : (e?.data ?? null),
|
|
402
|
-
loading: !!e?.operationId,
|
|
403
|
-
reload: heap.reloadSingle!,
|
|
404
|
-
timestamp,
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
const res: UseAsyncCollectionResT<DataT, IdT> = {
|
|
409
|
-
items: {} as Record<IdT, CollectionItemT<DataT>>,
|
|
410
|
-
loading: false,
|
|
411
|
-
reload: heap.reload,
|
|
412
|
-
timestamp: Number.MAX_VALUE,
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
for (let i = 0; i < ids.length; ++i) {
|
|
416
|
-
const id = ids[i]!;
|
|
417
|
-
const e = localState[id];
|
|
418
|
-
const loading = !!e?.operationId;
|
|
419
|
-
const timestamp = e?.timestamp ?? 0;
|
|
420
|
-
|
|
421
|
-
res.items[id] = {
|
|
422
|
-
data: maxage < Date.now() - timestamp ? null : (e?.data ?? null),
|
|
423
|
-
loading,
|
|
424
|
-
timestamp,
|
|
425
|
-
};
|
|
426
|
-
res.loading ||= loading;
|
|
427
|
-
if (res.timestamp > timestamp) res.timestamp = timestamp;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
return res;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
export default useAsyncCollection;
|
|
434
|
-
|
|
435
|
-
export interface UseAsyncCollectionI<StateT> {
|
|
436
|
-
<PathT extends null | string | undefined, IdT extends number | string>(
|
|
437
|
-
id: IdT,
|
|
438
|
-
path: PathT,
|
|
439
|
-
loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,
|
|
440
|
-
options?: UseAsyncDataOptionsT,
|
|
441
|
-
): UseAsyncDataResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>>;
|
|
442
|
-
|
|
443
|
-
<
|
|
444
|
-
Forced extends ForceT | LockT = LockT,
|
|
445
|
-
DataT = unknown,
|
|
446
|
-
IdT extends number | string = number | string,
|
|
447
|
-
>(
|
|
448
|
-
id: IdT,
|
|
449
|
-
path: null | string | undefined,
|
|
450
|
-
loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,
|
|
451
|
-
options?: UseAsyncDataOptionsT,
|
|
452
|
-
): UseAsyncDataResT<TypeLock<Forced, void, DataT>>;
|
|
453
|
-
|
|
454
|
-
<PathT extends null | string | undefined, IdT extends number | string>(
|
|
455
|
-
id: IdT[],
|
|
456
|
-
path: PathT,
|
|
457
|
-
loader: AsyncCollectionLoaderT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>,
|
|
458
|
-
options?: UseAsyncDataOptionsT,
|
|
459
|
-
): UseAsyncCollectionResT<DataInEnvelopeAtPathT<StateT, `${PathT}.${IdT}`>, IdT>;
|
|
460
|
-
|
|
461
|
-
<
|
|
462
|
-
Forced extends ForceT | LockT = LockT,
|
|
463
|
-
DataT = unknown,
|
|
464
|
-
IdT extends number | string = number | string,
|
|
465
|
-
>(
|
|
466
|
-
id: IdT[],
|
|
467
|
-
path: null | string | undefined,
|
|
468
|
-
loader: AsyncCollectionLoaderT<TypeLock<Forced, void, DataT>, IdT>,
|
|
469
|
-
options?: UseAsyncDataOptionsT,
|
|
470
|
-
): UseAsyncCollectionResT<DataT, IdT>;
|
|
471
|
-
}
|