Skip to content

Commit

Permalink
Simplify CatsRefFromRefLike#access (CE now allows this)
Browse files Browse the repository at this point in the history
  • Loading branch information
durban committed Jul 4, 2023
1 parent 7f94703 commit 11df4ba
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions core/shared/src/main/scala/dev/tauri/choam/refs/RefLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,14 @@ object RefLike {
self.set[F](a)

override def access: F[(A, A => F[Boolean])] = {
F.monad.flatMap(this.get) { ov =>
// `access` as defined in cats-effect must never
// succeed after it was called once, so we need a flag:
F.monad.map(Ref.unpadded[Boolean](false).run[F]) { hasBeenCalled =>
val setter = { (nv: A) =>
hasBeenCalled.getAndSet.provide(true).flatMapF { wasAlreadyCalled =>
if (!wasAlreadyCalled) {
self.modify { currVal =>
if (equ(currVal, ov)) (nv, true)
else (currVal, false)
}
} else {
Rxn.pure(false)
}
}.run[F]
}
(ov, setter)
F.monad.map(this.get) { ov =>
val setter = { (nv: A) =>
self.modify { currVal =>
if (equ(currVal, ov)) (nv, true)
else (currVal, false)
}.run[F]
}
(ov, setter)
}
}

Expand Down

0 comments on commit 11df4ba

Please sign in to comment.