import type * as Effect from "../Effect.js" import * as Effectable from "../Effectable.js" import { dual } from "../Function.js" import * as MutableRef from "../MutableRef.js" import * as Option from "../Option.js" import * as Readable from "../Readable.js" import type * as Ref from "../Ref.js" import * as core from "./core.js" /** @internal */ export const RefTypeId: Ref.RefTypeId = Symbol.for("effect/Ref") as Ref.RefTypeId /** @internal */ export const refVariance = { /* c8 ignore next */ _A: (_: any) => _ } class RefImpl extends Effectable.Class implements Ref.Ref { commit() { return this.get } readonly [RefTypeId] = refVariance readonly [Readable.TypeId]: Readable.TypeId = Readable.TypeId constructor(readonly ref: MutableRef.MutableRef) { super() this.get = core.sync(() => MutableRef.get(this.ref)) } readonly get: Effect.Effect modify(f: (a: A) => readonly [B, A]): Effect.Effect { return core.sync(() => { const current = MutableRef.get(this.ref) const [b, a] = f(current) if ((current as unknown) !== (a as unknown)) { MutableRef.set(a)(this.ref) } return b }) } } /** @internal */ export const unsafeMake = (value: A): Ref.Ref => new RefImpl(MutableRef.make(value)) /** @internal */ export const make = (value: A): Effect.Effect> => core.sync(() => unsafeMake(value)) /** @internal */ export const get = (self: Ref.Ref) => self.get /** @internal */ export const set = dual< (value: A) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, value: A) => Effect.Effect >(2, (self: Ref.Ref, value: A) => self.modify((): [void, A] => [void 0, value])) /** @internal */ export const getAndSet = dual< (value: A) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, value: A) => Effect.Effect >(2, (self: Ref.Ref, value: A) => self.modify((a): [A, A] => [a, value])) /** @internal */ export const getAndUpdate = dual< (f: (a: A) => A) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, f: (a: A) => A) => Effect.Effect >(2, (self: Ref.Ref, f: (a: A) => A) => self.modify((a): [A, A] => [a, f(a)])) /** @internal */ export const getAndUpdateSome = dual< (pf: (a: A) => Option.Option) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, pf: (a: A) => Option.Option) => Effect.Effect >(2, (self: Ref.Ref, pf: (a: A) => Option.Option) => self.modify((value): [A, A] => { const option = pf(value) switch (option._tag) { case "None": { return [value, value] } case "Some": { return [value, option.value] } } })) /** @internal */ export const setAndGet = dual< (value: A) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, value: A) => Effect.Effect >(2, (self: Ref.Ref, value: A) => self.modify((): [A, A] => [value, value])) /** @internal */ export const modify = dual< (f: (a: A) => readonly [B, A]) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, f: (a: A) => readonly [B, A]) => Effect.Effect >(2, (self, f) => self.modify(f)) /** @internal */ export const modifySome = dual< ( fallback: B, pf: (a: A) => Option.Option ) => (self: Ref.Ref) => Effect.Effect, ( self: Ref.Ref, fallback: B, pf: (a: A) => Option.Option ) => Effect.Effect >(3, (self, fallback, pf) => self.modify((value) => { const option = pf(value) switch (option._tag) { case "None": { return [fallback, value] } case "Some": { return option.value } } })) /** @internal */ export const update = dual< (f: (a: A) => A) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, f: (a: A) => A) => Effect.Effect >(2, (self: Ref.Ref, f: (a: A) => A) => self.modify((a): [void, A] => [void 0, f(a)])) /** @internal */ export const updateAndGet = dual< (f: (a: A) => A) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, f: (a: A) => A) => Effect.Effect >(2, (self: Ref.Ref, f: (a: A) => A) => self.modify((a): [A, A] => { const result = f(a) return [result, result] })) /** @internal */ export const updateSome = dual< (f: (a: A) => Option.Option) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, f: (a: A) => Option.Option) => Effect.Effect >(2, (self: Ref.Ref, f: (a: A) => Option.Option) => self.modify( (a): [void, A] => [ void 0, Option.match(f(a), { onNone: () => a, onSome: (b) => b }) ] )) /** @internal */ export const updateSomeAndGet = dual< (pf: (a: A) => Option.Option) => (self: Ref.Ref) => Effect.Effect, (self: Ref.Ref, pf: (a: A) => Option.Option) => Effect.Effect >(2, (self: Ref.Ref, pf: (a: A) => Option.Option) => self.modify((value): [A, A] => { const option = pf(value) switch (option._tag) { case "None": { return [value, value] } case "Some": { return [option.value, option.value] } } })) /** @internal */ export const unsafeGet = (self: Ref.Ref): A => MutableRef.get((self as RefImpl).ref)