Skip to content
Snippets Groups Projects
index.ts 470 B
Newer Older
Alex Yang's avatar
Alex Yang committed
import { AsyncLocalStorage } from 'node:async_hooks'
import type { createStreamableUI } from 'ai/rsc'

type StreamableUI = ReturnType<typeof createStreamableUI>

const streamUIAsyncLocalStorage = new AsyncLocalStorage<StreamableUI>()

export function getCurrentStreamableUI () {
  return streamUIAsyncLocalStorage.getStore()
}

export function runWithStreamableUI<T> (
  streamUI: StreamableUI,
  fn: () => T
): T {
  return streamUIAsyncLocalStorage.run(streamUI, fn)
}