import { dual } from "../../Function.js" import * as Option from "../../Option.js" import type { Pipeable } from "../../Pipeable.js" import { pipeArguments } from "../../Pipeable.js" import type * as STM from "../../STM.js" import type * as TRef from "../../TRef.js" import * as core from "./core.js" import * as Entry from "./entry.js" import type * as Journal from "./journal.js" import type * as TxnId from "./txnId.js" import * as Versioned from "./versioned.js" /** @internal */ const TRefSymbolKey = "effect/TRef" /** @internal */ export const TRefTypeId: TRef.TRefTypeId = Symbol.for( TRefSymbolKey ) as TRef.TRefTypeId export const tRefVariance = { /* c8 ignore next */ _A: (_: any) => _ } /** @internal */ export class TRefImpl implements TRef.TRef, Pipeable { readonly [TRefTypeId] = tRefVariance /** @internal */ todos: Map /** @internal */ versioned: Versioned.Versioned constructor(value: A) { this.versioned = new Versioned.Versioned(value) this.todos = new Map() } modify(f: (a: A) => readonly [B, A]): STM.STM { return core.effect((journal) => { const entry = getOrMakeEntry(this, journal) const [retValue, newValue] = f(Entry.unsafeGet(entry) as A) Entry.unsafeSet(entry, newValue) return retValue }) } pipe() { return pipeArguments(this, arguments) } } /** @internal */ export const make = (value: A): STM.STM> => core.effect>((journal) => { const ref = new TRefImpl(value) journal.set(ref, Entry.make(ref, true)) return ref }) /** @internal */ export const get = (self: TRef.TRef) => self.modify((a) => [a, a]) /** @internal */ export const set = dual< (value: A) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, value: A) => STM.STM >( 2, (self: TRef.TRef, value: A): STM.STM => self.modify((): [void, A] => [void 0, value]) ) /** @internal */ export const getAndSet = dual< (value: A) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, value: A) => STM.STM >(2, (self, value) => self.modify((a) => [a, value])) /** @internal */ export const getAndUpdate = dual< (f: (a: A) => A) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => A) => STM.STM >(2, (self, f) => self.modify((a) => [a, f(a)])) /** @internal */ export const getAndUpdateSome = dual< (f: (a: A) => Option.Option) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => Option.Option) => STM.STM >(2, (self, f) => self.modify((a) => Option.match(f(a), { onNone: () => [a, a], onSome: (b) => [a, b] }) )) /** @internal */ export const setAndGet = dual< (value: A) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, value: A) => STM.STM >(2, (self, value) => self.modify(() => [value, value])) /** @internal */ export const modify = dual< (f: (a: A) => readonly [B, A]) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => readonly [B, A]) => STM.STM >(2, (self, f) => self.modify(f)) /** @internal */ export const modifySome = dual< (fallback: B, f: (a: A) => Option.Option) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, fallback: B, f: (a: A) => Option.Option) => STM.STM >(3, (self, fallback, f) => self.modify((a) => Option.match(f(a), { onNone: () => [fallback, a], onSome: (b) => b }) )) /** @internal */ export const update = dual< (f: (a: A) => A) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => A) => STM.STM >(2, (self, f) => self.modify((a) => [void 0, f(a)])) /** @internal */ export const updateAndGet = dual< (f: (a: A) => A) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => A) => STM.STM >(2, (self, f) => self.modify((a) => { const b = f(a) return [b, b] })) /** @internal */ export const updateSome = dual< (f: (a: A) => Option.Option) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => Option.Option) => STM.STM >( 2, (self, f) => self.modify((a) => [ void 0, Option.match(f(a), { onNone: () => a, onSome: (b) => b }) ]) ) /** @internal */ export const updateSomeAndGet = dual< (f: (a: A) => Option.Option) => (self: TRef.TRef) => STM.STM, (self: TRef.TRef, f: (a: A) => Option.Option) => STM.STM >( 2, (self, f) => self.modify((a) => Option.match(f(a), { onNone: () => [a, a], onSome: (b) => [b, b] }) ) ) /** @internal */ const getOrMakeEntry = (self: TRef.TRef, journal: Journal.Journal): Entry.Entry => { if (journal.has(self)) { return journal.get(self)! } const entry = Entry.make(self, false) journal.set(self, entry) return entry } /** @internal */ export const unsafeGet: { (journal: Journal.Journal): (self: TRef.TRef) => A (self: TRef.TRef, journal: Journal.Journal): A } = dual< (journal: Journal.Journal) => (self: TRef.TRef) => A, (self: TRef.TRef, journal: Journal.Journal) => A >(2, (self: TRef.TRef, journal: Journal.Journal) => Entry.unsafeGet(getOrMakeEntry(self, journal)) as A) /** @internal */ export const unsafeSet: { (value: A, journal: Journal.Journal): (self: TRef.TRef) => void (self: TRef.TRef, value: A, journal: Journal.Journal): void } = dual< (value: A, journal: Journal.Journal) => (self: TRef.TRef) => void, (self: TRef.TRef, value: A, journal: Journal.Journal) => void >(3, (self, value, journal) => { const entry = getOrMakeEntry(self, journal) Entry.unsafeSet(entry, value) return undefined })