import * as tracing_1 from "../../../Tracing";
const fileName_1 = "(@effect-ts/system): _src/Experimental/Stream/SubscriptionRef/index.ts";
import * as A from "../../../Collections/Immutable/Chunk";
import * as Tp from "../../../Collections/Immutable/Tuple";
import * as T from "../../../Effect";
import { pipe } from "../../../Function";
import * as H from "../../../Hub";
import * as M from "../../../Managed";
import * as RefM from "../../../RefM";
import * as S from "..";
/**
 * 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.
 */

export class SubscriptionRef {
  constructor(ref, changes) {
    this.ref = ref;
    this.changes = changes;
  }

}
/**
 * Creates a new `SubscriptionRef` with the specified value.
 */

export 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(M.managedApply(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");
}
//# sourceMappingURL=index.js.map