• Fetch manga by IDs in batches.

    Parameters

    • ids: number[]

      Array of AniList manga IDs to fetch.

    • Optionaltoken: string

      Optional authentication token.

    • OptionalshouldCancel: () => boolean

      Optional function to check for cancellation.

    • OptionalabortSignal: AbortSignal

      Optional abort signal to cancel the fetch.

    Returns Promise<AniListManga[]>

    A promise resolving to an array of AniListManga objects.

    export async function getBatchedMangaIds(
    ids: number[],
    token?: string,
    shouldCancel?: () => boolean,
    abortSignal?: AbortSignal,
    ): Promise<AniListManga[]> {
    if (!ids.length) return [];

    // Check for cancellation
    if (shouldCancel && shouldCancel()) {
    throw new Error("Operation cancelled by user");
    }

    // Abort if signal is aborted
    if (abortSignal && abortSignal.aborted) {
    throw new Error("Operation aborted by abort signal");
    }

    const results: AniListManga[] = [];
    const batchSize = 25; // AniList allows 25 ids per request

    // Process in batches to avoid overloading the API
    for (let i = 0; i < ids.length; i += batchSize) {
    // Check for cancellation between batches
    if (shouldCancel && shouldCancel()) {
    throw new Error("Operation cancelled by user");
    }

    // Abort if signal is aborted
    if (abortSignal && abortSignal.aborted) {
    throw new Error("Operation aborted by abort signal");
    }

    const batchIds = ids.slice(i, i + batchSize);
    try {
    const batchResults = await getMangaByIds(batchIds, token, abortSignal);
    results.push(...batchResults);
    } catch (error) {
    console.error(
    `Error fetching manga batch ${i} to ${i + batchSize}:`,
    error,
    );
    // Continue with next batch even if one fails
    }
    }

    return results;
    }