2025-06-11 15:22:47 -07:00
|
|
|
function segmentData<T>(data: T[], segmentSize: number): T[][] {
|
2025-08-14 21:48:38 +01:00
|
|
|
return data.reduce((acc: T[][], curr, index) => {
|
|
|
|
|
const segment = Math.floor(index / segmentSize)
|
2025-06-11 15:22:47 -07:00
|
|
|
|
2025-08-14 21:48:38 +01:00
|
|
|
if (!acc[segment]) {
|
|
|
|
|
acc[segment] = []
|
|
|
|
|
}
|
|
|
|
|
acc[segment].push(curr)
|
|
|
|
|
return acc
|
|
|
|
|
}, [])
|
2025-06-11 15:22:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchSegmented<T>(
|
2025-08-14 21:48:38 +01:00
|
|
|
data: T[],
|
|
|
|
|
createUrl: (ids: T[]) => string,
|
|
|
|
|
options = {},
|
|
|
|
|
segmentSize = 800,
|
2025-06-11 15:22:47 -07:00
|
|
|
): Promise<any> {
|
2025-08-14 21:48:38 +01:00
|
|
|
return Promise.all(
|
|
|
|
|
segmentData(data, segmentSize).map((ids) => useBaseFetch(createUrl(ids), options)),
|
|
|
|
|
).then((results) => results.flat())
|
2025-06-11 15:22:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function asEncodedJsonArray<T>(data: T[]): string {
|
2025-08-14 21:48:38 +01:00
|
|
|
return encodeURIComponent(JSON.stringify(data))
|
2025-06-11 15:22:47 -07:00
|
|
|
}
|