@tanstack/ai-client 0.29.0 → 0.29.2

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.
@@ -480,6 +480,20 @@ function normalizeConnectionAdapter(connection) {
480
480
  if (waiter) waiter(chunk);
481
481
  else activeBuffer.push(chunk);
482
482
  }
483
+ async function waitUntilSubscriberIdle(abortSignal) {
484
+ const idle = () => activeBuffer.length === 0 && (activeWaiters.length > 0 || abortSignal?.aborted);
485
+ for (let i = 0; i < 16 && !abortSignal?.aborted; i++) {
486
+ if (idle()) return;
487
+ await Promise.resolve();
488
+ }
489
+ let macrotaskWaits = 0;
490
+ while (!abortSignal?.aborted) {
491
+ if (idle()) return;
492
+ await new Promise((resolve) => setTimeout(resolve, 0));
493
+ macrotaskWaits++;
494
+ if (activeWaiters.length === 0 && macrotaskWaits >= 32) return;
495
+ }
496
+ }
483
497
  return {
484
498
  subscribe(abortSignal) {
485
499
  const myBuffer = activeBuffer.splice(0);
@@ -537,6 +551,7 @@ function normalizeConnectionAdapter(connection) {
537
551
  } catch {}
538
552
  throw err;
539
553
  }
554
+ await waitUntilSubscriberIdle(abortSignal);
540
555
  },
541
556
  ...typeof connection.joinRun === "function" ? { joinRun: (runId, abortSignal) => connection.joinRun(runId, abortSignal) } : {},
542
557
  ...(() => {