// ets_tracing: off
import { pipe } from "../../Function";
import * as Q from "../../Queue";
import * as Ref from "../../Ref";
import * as T from "../_internal/effect";
import * as M from "../_internal/managed";
import * as Pull from "../Pull";
import * as Take from "../Take";
import { Stream } from "./definitions";
import { toQueueUnbounded } from "./toQueueUnbounded";
/**
 * Allows a faster producer to progress independently of a slower consumer by buffering
 * elements into an unbounded queue.
 */

export function bufferUnbounded(self) {
  return new Stream(M.map_(M.bind_(M.bind_(M.do, "done", () => T.toManaged(Ref.makeRef(true))), "queue", () => toQueueUnbounded(self)), ({
    done,
    queue
  }) => T.chain_(done.get, _ => {
    if (_) {
      return Pull.end;
    } else {
      return T.chain_(Q.take(queue), Take.foldM(() => T.zipRight_(done.set(true), Pull.end), Pull.halt, Pull.emitChunk));
    }
  })));
}
//# sourceMappingURL=bufferUnbounded.js.map