@tanstack/ai-client 0.30.0 → 0.31.1

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.
@@ -474,6 +474,7 @@ function normalizeConnectionAdapter(connection) {
474
474
  if (!hasConnect) throw new Error("Connection adapter must provide either connect or both subscribe and send");
475
475
  let activeBuffer = [];
476
476
  let activeWaiters = [];
477
+ let activeSubscriber;
477
478
  function push(chunk, runId) {
478
479
  if (runId) chunkRunIds.set(chunk, runId);
479
480
  const waiter = activeWaiters.shift();
@@ -481,17 +482,14 @@ function normalizeConnectionAdapter(connection) {
481
482
  else activeBuffer.push(chunk);
482
483
  }
483
484
  async function waitUntilSubscriberIdle(abortSignal) {
484
- const idle = () => activeBuffer.length === 0 && (activeWaiters.length > 0 || abortSignal?.aborted);
485
+ const idle = () => activeSubscriber !== activeWaiters || activeBuffer.length === 0 && (activeWaiters.length > 0 || abortSignal?.aborted);
485
486
  for (let i = 0; i < 16 && !abortSignal?.aborted; i++) {
486
487
  if (idle()) return;
487
488
  await Promise.resolve();
488
489
  }
489
- let macrotaskWaits = 0;
490
490
  while (!abortSignal?.aborted) {
491
491
  if (idle()) return;
492
492
  await new Promise((resolve) => setTimeout(resolve, 0));
493
- macrotaskWaits++;
494
- if (activeWaiters.length === 0 && macrotaskWaits >= 32) return;
495
493
  }
496
494
  }
497
495
  return {
@@ -501,19 +499,24 @@ function normalizeConnectionAdapter(connection) {
501
499
  activeBuffer = myBuffer;
502
500
  activeWaiters = myWaiters;
503
501
  return (async function* () {
504
- while (!abortSignal?.aborted) {
505
- let chunk;
506
- const buffered = myBuffer.shift();
507
- if (buffered !== void 0) chunk = buffered;
508
- else chunk = await new Promise((resolve) => {
509
- const onAbort = () => resolve(null);
510
- myWaiters.push((c) => {
511
- abortSignal?.removeEventListener("abort", onAbort);
512
- resolve(c);
502
+ activeSubscriber = myWaiters;
503
+ try {
504
+ while (!abortSignal?.aborted) {
505
+ let chunk;
506
+ const buffered = myBuffer.shift();
507
+ if (buffered !== void 0) chunk = buffered;
508
+ else chunk = await new Promise((resolve) => {
509
+ const onAbort = () => resolve(null);
510
+ myWaiters.push((c) => {
511
+ abortSignal?.removeEventListener("abort", onAbort);
512
+ resolve(c);
513
+ });
514
+ abortSignal?.addEventListener("abort", onAbort, { once: true });
513
515
  });
514
- abortSignal?.addEventListener("abort", onAbort, { once: true });
515
- });
516
- if (chunk !== null) yield chunk;
516
+ if (chunk !== null) yield chunk;
517
+ }
518
+ } finally {
519
+ if (activeSubscriber === myWaiters) activeSubscriber = void 0;
517
520
  }
518
521
  })();
519
522
  },