"use strict";

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

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

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

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

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

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

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

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/getOrElse"));

var _definition = /*#__PURE__*/require("@effect/core/io/Ref/definition");

var _RefInternal = /*#__PURE__*/require("@effect/core/io/Ref/operations/_internal/RefInternal");

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/Ref/operations/_internal/SynchronizedInternal.ts";
const SynchronizedInternal = { ..._RefInternal.RefInternal,

  /**
   * Internal Discriminators
   */
  get [_definition.RefSym]() {
    return _definition.RefSym;
  },

  get [_definition.SynchronizedSym]() {
    return _definition.SynchronizedSym;
  },

  /**
   * Reads the value from the `Ref`.
   */
  get(__tsplusTrace) {
    return this.ref.get(fileName_1 + ":25:24");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified function,
   * which computes a return value for the modification. This is a more
   * powerful version of `update`.
   */
  modifyEffect(f, __tsplusTrace) {
    return tsplus_module_3.withPermit(this.semaphore, fileName_1 + ":37:26")(tsplus_module_2.flatMap_(tsplus_module_2.flatMap_(this.get(fileName_1 + ":38:15"), f, fileName_1 + ":38:25"), tp => {
      const {
        tuple: [b, a]
      } = tp;
      return tsplus_module_1.as_(this.ref.set(a, fileName_1 + ":41:28"), () => b, fileName_1 + ":41:34");
    }, fileName_1 + ":38:36"));
  },

  /**
   * Writes a new value to the `Ref`, with a guarantee of immediate
   * consistency (at some cost to performance).
   */
  set(a, __tsplusTrace) {
    return tsplus_module_3.withPermit(this.semaphore, fileName_1 + ":50:26")(this.ref.set(a, fileName_1 + ":50:50"));
  },

  /**
   * Writes a new value to the `Ref` without providing a guarantee of
   * immediate consistency.
   */
  setAsync(a, __tsplusTrace) {
    return tsplus_module_3.withPermit(this.semaphore, fileName_1 + ":57:26")(this.ref.setAsync(a, fileName_1 + ":57:55"));
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified function,
   * returning the value immediately before modification.
   */
  getAndUpdateEffect(f, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_5.map_(f(v), result => tsplus_module_4.make(v, result), fileName_1 + ":68:45"), fileName_1 + ":68:29");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified partial
   * function, returning the value immediately before modification. If the
   * function is undefined on the current value it doesn't change it.
   */
  getAndUpdateSomeEffect(pf, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_5.map_(tsplus_module_7.getOrElse_(pf(v), () => tsplus_module_6.succeedNow(v, fileName_1 + ":80:68")), result => tsplus_module_4.make(v, result), fileName_1 + ":80:76"), fileName_1 + ":80:29");
  },

  modify(f, __tsplusTrace) {
    return this.modifyEffect(a => tsplus_module_6.succeedNow(f(a), fileName_1 + ":83:54"), fileName_1 + ":83:29");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified function,
   * which computes a return value for the modification if the function is
   * defined in the current value otherwise it returns a default value. This
   * is a more powerful version of `updateSome`.
   */
  modifySomeEffect(fallback, pf, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_7.getOrElse_(pf(v), () => tsplus_module_6.succeedNow(tsplus_module_4.make(fallback, v), fileName_1 + ":97:68")), fileName_1 + ":97:29");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified function.
   */
  updateEffect(f, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_5.map_(f(v), result => tsplus_module_4.make(undefined, result), fileName_1 + ":107:43"), fileName_1 + ":107:29");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified function,
   * returning the value immediately after modification.
   */
  updateAndGetEffect(f, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_5.map_(f(v), result => tsplus_module_4.make(result, result), fileName_1 + ":118:43"), fileName_1 + ":118:29");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified partial
   * function. If the function is undefined on the current value it doesn't
   * change it.
   */
  updateSomeEffect(pf, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_5.map_(tsplus_module_7.getOrElse_(pf(v), () => tsplus_module_6.succeedNow(v, fileName_1 + ":131:68")), result => tsplus_module_4.make(undefined, result), fileName_1 + ":131:76"), fileName_1 + ":131:29");
  },

  /**
   * Atomically modifies the `Ref.Synchronized` with the specified partial
   * function. If the function is undefined on the current value it returns
   * the old value without changing it.
   */
  updateSomeAndGetEffect(pf, __tsplusTrace) {
    return this.modifyEffect(v => tsplus_module_5.map_(tsplus_module_7.getOrElse_(pf(v), () => tsplus_module_6.succeedNow(v, fileName_1 + ":144:68")), result => tsplus_module_4.make(result, result), fileName_1 + ":144:76"), fileName_1 + ":144:29");
  }

};
exports.SynchronizedInternal = SynchronizedInternal;
//# sourceMappingURL=SynchronizedInternal.js.map