MCPcopy
hub / github.com/TanStack/ai / createSubscribeAdapter

Function createSubscribeAdapter

packages/ai-client/tests/chat-client.test.ts:275–316  ·  view source on GitHub ↗
(chunksToSend: Array<StreamChunk>)

Source from the content-addressed store, hash-verified

273
274 describe('subscribe/send connection mode', () => {
275 function createSubscribeAdapter(chunksToSend: Array<StreamChunk>) {
276 let hasPendingSend = false
277 let wakeSubscriber: (() => void) | null = null
278 let removeAbortListener: (() => void) | null = null
279
280 const subscribe = vi.fn((signal?: AbortSignal) => {
281 return (async function* () {
282 while (!signal?.aborted) {
283 if (!hasPendingSend) {
284 await new Promise<void>((resolve) => {
285 removeAbortListener?.()
286 removeAbortListener = null
287 wakeSubscriber = resolve
288 const onAbort = () => resolve()
289 signal?.addEventListener('abort', onAbort, { once: true })
290 removeAbortListener = () => {
291 signal?.removeEventListener('abort', onAbort)
292 }
293 })
294 continue
295 }
296
297 hasPendingSend = false
298 for (const chunk of chunksToSend) {
299 yield chunk
300 }
301 }
302 removeAbortListener?.()
303 removeAbortListener = null
304 })()
305 })
306
307 const send = vi.fn(async () => {
308 removeAbortListener?.()
309 removeAbortListener = null
310 hasPendingSend = true
311 wakeSubscriber?.()
312 wakeSubscriber = null
313 })
314
315 return { subscribe, send }
316 }
317
318 it('should use subscribe/send adapter mode', async () => {
319 const adapter = createSubscribeAdapter(

Callers 1

Calls 1

addEventListenerMethod · 0.45

Tested by

no test coverage detected