const fileName_1 = "(@effect/core) _src/io/Supervisor/operations/track.ts";
import * as tsplus_module_1 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_2 from "@tsplus/stdlib/data/AtomicReference";
import * as tsplus_module_3 from "@effect/core/io/Supervisor/definition";
import * as tsplus_module_4 from "@tsplus/stdlib/collections/Chunk/definition";
export const unsafeTrack = unsafeTrack_1;
/**
 * Creates a new supervisor that tracks children in a set.
 *
 * @tsplus static ets/Supervisor/Ops track
 */

export 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.mjs.map