@arbidocs/sdk 0.3.45 → 0.3.46

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/dist/index.cjs CHANGED
@@ -4709,13 +4709,11 @@ async function listDocuments(arbi) {
4709
4709
  return requireData(await arbi.fetch.GET("/v1/document/list"), "Failed to fetch documents");
4710
4710
  }
4711
4711
  async function* listPaginated(arbi, options = {}) {
4712
- const { pageSize = 5e3, order = "id_asc", fields, signal } = options;
4713
- let offset = 0;
4714
- let pagesFetched = 0;
4715
- while (!signal?.aborted && pagesFetched < MAX_PAGES) {
4712
+ const { pageSize = 5e3, order = "id_asc", fields, signal, lookahead = 3 } = options;
4713
+ const fetchPage = async (pageOffset) => {
4716
4714
  const query = {
4717
4715
  limit: pageSize,
4718
- offset,
4716
+ offset: pageOffset,
4719
4717
  order
4720
4718
  };
4721
4719
  if (fields) query.fields = fields;
@@ -4726,16 +4724,40 @@ async function* listPaginated(arbi, options = {}) {
4726
4724
  if (error) {
4727
4725
  throw new Error(typeof error === "string" ? error : JSON.stringify(error));
4728
4726
  }
4727
+ return data ?? [];
4728
+ };
4729
+ if (signal?.aborted) return;
4730
+ const depth = Math.max(1, Math.min(lookahead, MAX_PAGES));
4731
+ let issued = 0;
4732
+ let pagesYielded = 0;
4733
+ let nextOffsetToIssue = 0;
4734
+ let done = false;
4735
+ const queue = [];
4736
+ const tryEnqueue = () => {
4737
+ while (!done && queue.length < depth && issued < MAX_PAGES) {
4738
+ const p2 = fetchPage(nextOffsetToIssue);
4739
+ p2.catch(() => {
4740
+ });
4741
+ queue.push(p2);
4742
+ nextOffsetToIssue += pageSize;
4743
+ issued++;
4744
+ }
4745
+ };
4746
+ tryEnqueue();
4747
+ while (queue.length > 0 && !signal?.aborted) {
4748
+ const page = await queue.shift();
4729
4749
  if (signal?.aborted) return;
4730
- const page = data ?? [];
4750
+ const isShort = page.length < pageSize;
4751
+ if (isShort) done = true;
4752
+ tryEnqueue();
4731
4753
  if (page.length > 0) {
4732
4754
  yield page;
4733
- offset += page.length;
4734
- pagesFetched++;
4755
+ pagesYielded++;
4735
4756
  }
4736
- if (page.length < pageSize) return;
4757
+ if (isShort) return;
4758
+ if (pagesYielded >= MAX_PAGES) break;
4737
4759
  }
4738
- if (pagesFetched >= MAX_PAGES) {
4760
+ if (!signal?.aborted && !done && pagesYielded >= MAX_PAGES) {
4739
4761
  console.warn(
4740
4762
  `[arbi-sdk] listPaginated hit MAX_PAGES (${MAX_PAGES}) \u2014 stopped. Workspace may have more than ${MAX_PAGES * pageSize} documents.`
4741
4763
  );