import asyncio
from collections.abc import AsyncIterable, Iterable
from typing import Any


# TODO reimplement this as CustomAsyncIterable by implementing __anext__ through asyncio.Queue(es)
# e.g. as here https://github.com/leangaurav/pypi_iterator/blob/main/iterators/timeout_iterator.py
async def to_async_iterable(sync_iterable: Iterable[Any]) -> AsyncIterable[Any]:
    done = object()
    it = iter(sync_iterable)
    while (value := await asyncio.to_thread(next, it, done)) is not done:
        yield value
