"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.make = exports.SubscriptionRef = void 0;
const tracing_1 = __importStar(require("../../Tracing"));
const fileName_1 = "(@effect-ts/system): _src/Stream/SubcriptionRef/index.ts";
const A = __importStar(require("../../Collections/Immutable/Chunk"));
const Tp = __importStar(require("../../Collections/Immutable/Tuple"));
const T = __importStar(require("../../Effect"));
const Function_1 = require("../../Function");
const H = __importStar(require("../../Hub"));
const M = __importStar(require("../../Managed"));
const RefM = __importStar(require("../../RefM"));
const S = __importStar(require("../Stream"));
/**
 * A `SubscriptionRef<A>` contains a `RefM` with a value of type
 * `A` and a `Stream` that can be subscribed to in order to receive the
 * current value as well as all changes to the value.
 */
class SubscriptionRef {
    constructor(ref, changes) {
        this.ref = ref;
        this.changes = changes;
    }
}
exports.SubscriptionRef = SubscriptionRef;
/**
 * Creates a new `SubscriptionRef` with the specified value.
 */
function make(a) {
    return (T.map_(T.let_(T.bind_(T.bind_(T.do, "ref", () => RefM.makeRefM(a), fileName_1 + ":25:11"), "hub", () => H.makeUnbounded(), fileName_1 + ":26:11"), "changes", ({ hub, ref }) => S.unwrapManaged(new M.Managed(T.uninterruptible(RefM.modify_(ref, (a) => T.zipWith_(T.succeed(a, fileName_1 + ":33:26"), H.subscribe(hub).effect, (a, { tuple: [finalizer, queue] }) => Tp.tuple(Tp.tuple(finalizer, S.concat_(S.fromChunk(A.single(a)), S.fromQueue(queue))), a), fileName_1 + ":32:25"))))), fileName_1 + ":27:10"), ({ changes, hub, ref }) => new SubscriptionRef(RefM.tapInput_(ref, (_) => H.publish_(hub, _)), changes), fileName_1 + ":49:10"));
}
exports.make = make;
//# sourceMappingURL=index.js.map