"use strict";

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

var _index = /*#__PURE__*/require("../Clock/index.cjs");

var Tp = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Collections/Immutable/Tuple/index.cjs"));

var _index3 = /*#__PURE__*/require("../Function/index.cjs");

var O = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../Option/index.cjs"));

var Ref = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("../RefM/index.cjs"));

var core = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./core.cjs"));

var die = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./die.cjs"));

var Do = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./do.cjs"));

var _environment = /*#__PURE__*/require("./environment.cjs");

var P = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./excl-forEach-promise.cjs"));

var uninterruptibleMask = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./interruption.cjs"));

var map = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./map.cjs"));

var tap = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./tap.cjs"));

var to = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("./to.cjs"));

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

/**
 * Returns an effect that, if evaluated, will return the cached result of
 * this effect. Cached results will expire after `timeToLive` duration. In
 * addition, returns an effect that can be used to invalidate the current
 * cached value before the `timeToLive` duration expires.
 *
 * @ets_data_first cachedInvalidate_
 */
function cachedInvalidate(ttl, __trace) {
  return fa => cachedInvalidate_(fa, ttl);
}
/**
 * Returns an effect that, if evaluated, will return the cached result of
 * this effect. Cached results will expire after `timeToLive` duration. In
 * addition, returns an effect that can be used to invalidate the current
 * cached value before the `timeToLive` duration expires.
 */


function cachedInvalidate_(fa, ttl, __trace) {
  return map.map_(Do.bind_(Do.bind_(Do.do, "r", () => (0, _environment.environment)()), "cache", () => Ref.makeRefM(O.none)), ({
    cache,
    r
  }) => Tp.tuple(core.provideAll_(get(fa, ttl, cache), r), invalidate(cache)), __trace);
}

function invalidate(cache) {
  return cache.set(O.none);
}

function compute(fa, ttl, start) {
  return map.map_(tap.tap_(Do.bind_(Do.do, "p", () => P.make()), ({
    p
  }) => to.to_(fa, p)), ({
    p
  }) => O.some(Tp.tuple(start + ttl, p)));
}

function get(fa, ttl, cache) {
  return uninterruptibleMask.uninterruptibleMask(({
    restore
  }) => core.chain_(_index.currentTime, time => core.chain_(Ref.updateSomeAndGet(o => O.fold_(o, () => O.some(compute(fa, ttl, time)), ({
    tuple: [end]
  }) => end - time <= 0 ? O.some(compute(fa, ttl, time)) : O.none))(cache), a => a._tag === "None" ? die.die("bug") : restore(P.await(a.value.get(1))))));
}
//# sourceMappingURL=cachedInvalidate.cjs.map