"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.track = track;
exports.unsafeTrack = void 0;

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

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/AtomicReference"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Supervisor/definition"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/collections/Chunk/definition"));

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; }

const fileName_1 = "(@effect/core) _src/io/Supervisor/operations/track.ts";
const unsafeTrack = unsafeTrack_1;
/**
 * Creates a new supervisor that tracks children in a set.
 *
 * @tsplus static ets/Supervisor/Ops track
 */

exports.unsafeTrack = unsafeTrack;

function track(__tsplusTrace) {
  return tsplus_module_1.succeed(unsafeTrack_1, __tsplusTrace);
} // `setInterval` is limited to take delays which are 32-bit values


const MAX_SET_INTERVAL_VALUE = 2 ** 31 - 1;
/**
 * @tsplus static ets/Supervisor/Ops unsafeTrack
 */

function unsafeTrack_1() {
  const set = new Set();
  const interval = new tsplus_module_2.AtomicReference(undefined);
  return new tsplus_module_3.Supervisor(tsplus_module_1.succeed(() => tsplus_module_4.from(set), fileName_1 + ":23:19"), (_, __, ___, fiber) => {
    if (set.has(fiber)) {
      if (interval.get == null) {
        interval.set( // @ts-ignore
        setInterval(() => {// Keep the process alive
        }, MAX_SET_INTERVAL_VALUE));
      }
    } else {
      set.add(fiber);
    }
  }, (_, fiber) => {
    set.delete(fiber);

    if (set.size === 0) {
      const ci = interval.get;

      if (ci) {
        clearInterval(ci);
      }
    }
  });
}
//# sourceMappingURL=track.js.map