import { getOrMakeEntry } from "@effect/core/stm/TRef/operations/_internal/getOrMakeEntry"
/**
* Updates the value of the variable and returns the old value.
*
* @tsplus static effect/core/stm/TRef.Aspects getAndUpdate
* @tsplus pipeable effect/core/stm/TRef getAndUpdate
*/
export function getAndUpdate(f: (a: A) => A) {
return (self: TRef): STM =>
STM.Effect((journal) => {
const entry = getOrMakeEntry(self, journal)
const oldValue = entry.use((_) => _.unsafeGet())
entry.use((_) => _.unsafeSet(f(oldValue)))
return oldValue
})
}