// ets_tracing: off
import * as CK from "../../../../Collections/Immutable/Chunk";
import * as T from "../../../../Effect";
import { pipe } from "../../../../Function";
import * as H from "../../../../Hub";
import * as M from "../../../../Managed";
import * as RunIntoHubManaged from "./runIntoHubManaged";
/**
 * Converts the stream to a managed list of queues. Every value will be replicated to every queue with the
 * slowest queue being allowed to buffer `maximumLag` chunks before the driver is backpressured.
 *
 * Queues can unsubscribe from upstream by shutting down.
 */

export function broadcastedQueues_(self, n, maximumLag) {
  return M.map_(M.tap_(M.bind_(M.bind_(M.do, "hub", () => T.toManaged(H.makeBounded(maximumLag))), "queues", ({
    hub
  }) => M.collectAll(CK.fill(n, () => H.subscribe(hub)))), ({
    hub
  }) => M.fork(RunIntoHubManaged.runIntoHubManaged_(self, hub))), ({
    queues
  }) => queues);
}
/**
 * Converts the stream to a managed list of queues. Every value will be replicated to every queue with the
 * slowest queue being allowed to buffer `maximumLag` chunks before the driver is backpressured.
 *
 * Queues can unsubscribe from upstream by shutting down.
 *
 * @ets_data_first broadcastedQueues_
 */

export function broadcastedQueues(n, maximumLag) {
  return self => broadcastedQueues_(self, n, maximumLag);
}
//# sourceMappingURL=broadcastedQueues.js.map