import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
export function isIterator(thing) {
    return (typeof thing === "object" &&
        thing !== null &&
        typeof thing[Symbol.iterator] === "function" &&
        // avoid detecting array/set as iterator
        typeof thing.next === "function");
}
export function isAsyncIterable(thing) {
    return (typeof thing === "object" &&
        thing !== null &&
        typeof thing[Symbol.asyncIterator] ===
            "function");
}
export function* consumeIteratorInContext(context, iter) {
    const storage = AsyncLocalStorageProviderSingleton.getInstance();
    while (true) {
        const { value, done } = storage.run(context, iter.next.bind(iter));
        if (done) {
            break;
        }
        else {
            yield value;
        }
    }
}
export async function* consumeAsyncIterableInContext(context, iter) {
    const storage = AsyncLocalStorageProviderSingleton.getInstance();
    const iterator = iter[Symbol.asyncIterator]();
    while (true) {
        const { value, done } = await storage.run(context, iterator.next.bind(iter));
        if (done) {
            break;
        }
        else {
            yield value;
        }
    }
}
