import { getOrMakeEntry } from "@effect/core/stm/TRef/operations/_internal/getOrMakeEntry"
/**
* Updates the value of the variable, returning a function of the specified
* value.
*
* @tsplus static effect/core/stm/TRef.Aspects modify
* @tsplus pipeable effect/core/stm/TRef modify
*/
export function modify(f: (a: A) => readonly [B, A]) {
return (self: TRef): STM =>
STM.Effect((journal) => {
const entry = getOrMakeEntry(self, journal)
const [retValue, newValue] = entry.use((_) => f(_.unsafeGet()))
entry.use((_) => _.unsafeSet(newValue))
return retValue
})
}