"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.effectAsyncInterruptEither = void 0
const E = __importStar(require("../../Either"))
const Function_1 = require("../../Function")
const Q = __importStar(require("../../Queue"))
const T = __importStar(require("../_internal/effect"))
const M = __importStar(require("../_internal/managed"))
const Ref = __importStar(require("../_internal/ref"))
const Pull = __importStar(require("../Pull"))
const Take = __importStar(require("../Take"))
const definitions_1 = require("./definitions")
/**
 * Creates a stream from an asynchronous callback that can be called multiple times.
 * The registration of the callback returns either a canceler or synchronously returns a stream.
 * The optionality of the error type `E` can be used to signal the end of the stream, by
 * setting it to `None`.
 */
function effectAsyncInterruptEither(register, outputBuffer = 16) {
  return new definitions_1.Stream(
    M.map_(
      M.bind_(
        M.bind_(
          M.bind_(
            M.bind_(M.do, "output", () => T.toManaged(Q.makeBounded(outputBuffer))),
            "runtime",
            () => T.toManaged(T.runtime())
          ),
          "eitherStream",
          ({ output, runtime }) =>
            M.succeedWith(() =>
              register((k, cb) =>
                ((x) => runtime.runCancel(x, cb))(
                  T.chain_(Take.fromPull(k), (x) => Q.offer_(output, x))
                )
              )
            )
        ),
        "pull",
        ({ eitherStream, output }) =>
          E.fold_(
            eitherStream,
            (canceler) =>
              M.ensuring_(
                M.map_(
                  M.bind_(M.do, "done", () => Ref.makeManagedRef(false)),
                  ({ done }) =>
                    T.chain_(done.get, (b) =>
                      b
                        ? Pull.end
                        : T.onError_(T.chain_(Q.take(output), Take.done), () =>
                            T.chain_(done.set(true), () => Q.shutdown(output))
                          )
                    )
                ),
                canceler
              ),
            (s) => M.chain_(T.toManaged(Q.shutdown(output)), () => s.proc)
          )
      ),
      ({ pull }) => pull
    )
  )
}
exports.effectAsyncInterruptEither = effectAsyncInterruptEither
//# sourceMappingURL=effectAsyncInterruptEither.js.map
