All files / apps/web/src/utils batch.ts

100% Statements 41/41
100% Branches 13/13
100% Functions 15/15
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134        16x       80x 80x 80x 114x 114x       114x         80x 109x 5x           16x 61x   61x 68x 68x 63x   5x                           61x   68x         16x               56x   68x 68x       56x   61x 61x   68x       56x 56x   63x                           56x   63x   56x 68x                 68x 68x 5x                   63x 63x              
import { hashKey } from "@tanstack/react-query";
import { TRPCError } from "@trpc/server";
import { omit, unique, values } from "remeda";
 
export const getDuplicates = <T, K extends string | string[]>(
	array: readonly T[],
	getKey: (item: T) => K,
) => {
	const items = array.map(getKey);
	const map = new Map<string, { count: number; value: K }>();
	for (const item of items) {
		const key = Array.isArray(item) ? item.join("|") : (item satisfies string);
		const prevMapElement = map.get(key) || {
			count: 0,
			value: item,
		};
		map.set(key, {
			count: prevMapElement.count + 1,
			value: prevMapElement.value,
		});
	}
	return [...map.values()]
		.filter(({ count }) => count !== 1)
		.map(({ value, count }) => [value, count] as const);
};
 
export type GeneralInput = { cursor: number; limit: number };
export type GeneralOutput<T> = { cursor: number; items: T[] };
 
const mergeInputs = <I extends GeneralInput>(inputs: I[]): Map<I, I> => {
	const sorted = [...inputs].toSorted((a, b) => a.cursor - b.cursor);
 
	const groups = sorted.reduce<(I & { members: I[] })[]>((acc, element) => {
		const lastGroup = acc.at(-1);
		if (!lastGroup || element.cursor > lastGroup.cursor + lastGroup.limit) {
			return [...acc, { ...element, members: [element] }];
		}
		return [
			...acc.slice(0, -1),
			{
				...lastGroup,
				limit:
					Math.max(
						lastGroup.cursor + lastGroup.limit,
						element.cursor + element.limit,
					) - lastGroup.cursor,
				members: [...lastGroup.members, element],
			},
		];
	}, []);
 
	return new Map<I, I>(
		groups.flatMap(({ members, ...group }) =>
			members.map((member) => [member, group as unknown as I]),
		),
	);
};
 
export const queueList = async <
	I extends GeneralInput,
	R,
	O extends GeneralOutput<R>,
>(
	inputs: readonly I[],
	fetchPage: (inputs: I) => Promise<O>,
): Promise<(O | TRPCError)[]> => {
	const mappedInputs = inputs.reduce<Partial<Record<string, I[]>>>(
		(acc, input) => {
			const key = hashKey([omit(input, ["cursor", "limit"])]);
			return { ...acc, [key]: [...(acc[key] ?? []), input] };
		},
		{},
	);
	const compactInputsMap = new Map<I, I>(
		values(mappedInputs).flatMap((mappedInput = []) => {
			const compactInput = mergeInputs(mappedInput);
			return mappedInput.map(
				// oxlint-disable-next-line typescript/no-non-null-assertion
				(input) => [input, compactInput.get(input)!],
			);
		}),
	);
	const compactInputs = unique([...compactInputsMap.values()]);
	const list = await Promise.all(
		compactInputs.map(async (input) =>
			fetchPage(input).catch(
				(error) =>
					/* c8 ignore start */
					error instanceof TRPCError
						? error
						: new TRPCError({
								code: "INTERNAL_SERVER_ERROR",
								message: `Unknown internal error: ${String(error)}`,
								cause: error,
							}),
				/* c8 ignore stop */
			),
		),
	);
	const mappedList = new Map<I, O | TRPCError>(
		// oxlint-disable-next-line typescript/no-non-null-assertion
		compactInputs.map((input, index) => [input, list[index]!]),
	);
	return inputs.map((input) => {
		const mappedInput = compactInputsMap.get(input);
		/* c8 ignore start */
		if (!mappedInput) {
			return new TRPCError({
				code: "INTERNAL_SERVER_ERROR",
				message: `Expected for input "${JSON.stringify(input)}" to have corresponding compact input.`,
			});
		}
		/* c8 ignore stop */
		const output = mappedList.get(mappedInput);
		if (output instanceof TRPCError) {
			return output;
		}
		/* c8 ignore start */
		if (!output) {
			return new TRPCError({
				code: "INTERNAL_SERVER_ERROR",
				message: `Expected for compact input "${JSON.stringify(mappedInput)}" to have corresponding output.`,
			});
		}
		/* c8 ignore stop */
		const startIndex = input.cursor - output.cursor;
		return {
			...output,
			cursor: input.cursor,
			items: output.items.slice(startIndex, startIndex + input.limit),
		};
	});
};