import { dual } from "@effect/data/Function" import * as MutableRef from "@effect/data/MutableRef" import * as Option from "@effect/data/Option" import { pipeArguments } from "@effect/data/Pipeable" import type * as Effect from "@effect/io/Effect" import * as core from "@effect/io/internal/core" import type * as Ref from "@effect/io/Ref" /** @internal */ export const RefTypeId: Ref.RefTypeId = Symbol.for("@effect/io/Ref") as Ref.RefTypeId /** @internal */ export const refVariance = { _A: (_: never) => _ } class RefImpl implements Ref.Ref { readonly [RefTypeId] = refVariance constructor(readonly ref: MutableRef.MutableRef) {} 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 }) } pipe() { return pipeArguments(this, arguments) } } /** @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.modify((a) => [a, a]) /** @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] as const } 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)