import { deferred } from "../async/deferred.ts";
export function mergeReadableStreams(...streams) {
    const resolvePromises = streams.map(() => deferred());
    return new ReadableStream({
        start(controller) {
            Promise.all(resolvePromises).then(() => {
                controller.close();
            });
            try {
                for (const [key, stream] of Object.entries(streams)) {
                    (async () => {
                        for await (const data of stream) {
                            controller.enqueue(data);
                        }
                        resolvePromises[+key].resolve();
                    })();
                }
            }
            catch (e) {
                controller.error(e);
            }
        },
    });
}
export function zipReadableStreams(...streams) {
    const readers = streams.map((s) => s.getReader());
    return new ReadableStream({
        async start(controller) {
            try {
                let resolved = 0;
                while (resolved != streams.length) {
                    for (const [key, reader] of Object.entries(readers)) {
                        const { value, done } = await reader.read();
                        if (!done) {
                            controller.enqueue(value);
                        }
                        else {
                            resolved++;
                            readers.splice(+key, 1);
                        }
                    }
                }
                controller.close();
            }
            catch (e) {
                controller.error(e);
            }
        },
    });
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 4884 characters]