@arbidocs/sdk 0.3.45 → 0.3.47

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,18 @@ 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 {
4713
+ pageSize = 5e3,
4714
+ firstPageSize,
4715
+ order = "id_asc",
4716
+ fields,
4717
+ signal,
4718
+ lookahead = 1
4719
+ } = options;
4720
+ const fetchPage = async (pageOffset, limit) => {
4716
4721
  const query = {
4717
- limit: pageSize,
4718
- offset,
4722
+ limit,
4723
+ offset: pageOffset,
4719
4724
  order
4720
4725
  };
4721
4726
  if (fields) query.fields = fields;
@@ -4726,16 +4731,42 @@ async function* listPaginated(arbi, options = {}) {
4726
4731
  if (error) {
4727
4732
  throw new Error(typeof error === "string" ? error : JSON.stringify(error));
4728
4733
  }
4734
+ return data ?? [];
4735
+ };
4736
+ if (signal?.aborted) return;
4737
+ const depth = Math.max(1, Math.min(lookahead, MAX_PAGES));
4738
+ let issued = 0;
4739
+ let pagesYielded = 0;
4740
+ let nextOffsetToIssue = 0;
4741
+ let done = false;
4742
+ const queue = [];
4743
+ const tryEnqueue = () => {
4744
+ while (!done && queue.length < depth && issued < MAX_PAGES) {
4745
+ const limit = issued === 0 && firstPageSize !== void 0 ? firstPageSize : pageSize;
4746
+ const promise = fetchPage(nextOffsetToIssue, limit);
4747
+ promise.catch(() => {
4748
+ });
4749
+ queue.push({ limit, promise });
4750
+ nextOffsetToIssue += limit;
4751
+ issued++;
4752
+ }
4753
+ };
4754
+ tryEnqueue();
4755
+ while (queue.length > 0 && !signal?.aborted) {
4756
+ const { limit, promise } = queue.shift();
4757
+ const page = await promise;
4729
4758
  if (signal?.aborted) return;
4730
- const page = data ?? [];
4759
+ const isShort = page.length < limit;
4760
+ if (isShort) done = true;
4761
+ tryEnqueue();
4731
4762
  if (page.length > 0) {
4732
4763
  yield page;
4733
- offset += page.length;
4734
- pagesFetched++;
4764
+ pagesYielded++;
4735
4765
  }
4736
- if (page.length < pageSize) return;
4766
+ if (isShort) return;
4767
+ if (pagesYielded >= MAX_PAGES) break;
4737
4768
  }
4738
- if (pagesFetched >= MAX_PAGES) {
4769
+ if (!signal?.aborted && !done && pagesYielded >= MAX_PAGES) {
4739
4770
  console.warn(
4740
4771
  `[arbi-sdk] listPaginated hit MAX_PAGES (${MAX_PAGES}) \u2014 stopped. Workspace may have more than ${MAX_PAGES * pageSize} documents.`
4741
4772
  );