"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.asyncScoped = asyncScoped;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/repeatEffectChunkMaybe"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Pull/operations/end"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/zipRight"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Take/operations/done"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatMap"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/onError"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Ref/operations/make"));

var tsplus_module_8 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Take/operations/fromPull"));

var tsplus_module_9 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Cause/operations/isInterrupted"));

var tsplus_module_10 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/runtime"));

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Queue/external/bounded"));

var tsplus_module_12 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/acquireRelease"));

var tsplus_module_13 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/do"));

var tsplus_module_14 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/tap"));

var tsplus_module_15 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/map"));

var tsplus_module_16 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/scoped"));

var tsplus_module_17 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stream/Stream/operations/flatMap"));

var _errors = /*#__PURE__*/require("@effect/core/io/Cause/errors");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * Creates a stream from an asynchronous callback that can be called multiple
 * times. The registration of the callback itself returns an a scoped
 * resource. The optionality of the error type `E` can be used to signal the
 * end of the stream, by setting it to `None`.
 *
 * @tsplus static ets/Stream/Ops asyncScoped
 */
function asyncScoped(register, outputBuffer = 16, __tsplusTrace) {
  return tsplus_module_17.flatMap_(tsplus_module_16.scoped(() => tsplus_module_15.map_(tsplus_module_13.bind_(tsplus_module_14.tap_(tsplus_module_13.bind_(tsplus_module_13.bind_(tsplus_module_13.Do(), "output", () => tsplus_module_12.acquireRelease(() => tsplus_module_11.bounded(outputBuffer, __tsplusTrace), queue => queue.shutdown, __tsplusTrace), __tsplusTrace), "runtime", () => tsplus_module_10.runtime(__tsplusTrace), __tsplusTrace), ({
    output,
    runtime
  }) => register(async k => {
    try {
      runtime.unsafeRunPromise(tsplus_module_5.flatMap_(tsplus_module_8.fromPull(k), take => output.offer(take, __tsplusTrace), __tsplusTrace), __tsplusTrace);
    } catch (e) {
      if ((0, _errors.isFiberFailure)(e)) {
        if (!tsplus_module_9.isInterrupted(e.cause)) {
          throw e;
        }
      }
    }
  }), __tsplusTrace), "done", () => tsplus_module_7.makeRef(() => false, __tsplusTrace), __tsplusTrace), ({
    done,
    output
  }) => tsplus_module_5.flatMap_(done.get(__tsplusTrace), isDone => isDone ? tsplus_module_2.end : tsplus_module_6.onError_(tsplus_module_5.flatMap_(output.take, take => tsplus_module_4.done(take, __tsplusTrace), __tsplusTrace), () => tsplus_module_3.zipRight_(done.set(true, __tsplusTrace), () => output.shutdown, __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), __tsplusTrace), pull => tsplus_module_1.repeatEffectChunkMaybe(() => pull, __tsplusTrace), __tsplusTrace);
}
//# sourceMappingURL=asyncScoped.js.map