Skip to content
STM monad for cats-effect
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
js/src/main/scala/com/olegpy/stm/internal refactor: make internal API private, some extra tests May 10, 2019
jvm/src
project chore: add coveralls reporting May 4, 2019
shared/src test: add dining philosophers problem May 13, 2019
.gitignore Add tests; fix TRef.in Apr 26, 2019
.jvmopts fix: reimplement monitor to avoid deadlocks? May 10, 2019
.travis.yml chore: don't try to unpack secret for PRs May 13, 2019
LICENSE Create LICENSE Apr 26, 2019
README.md Fix example May 13, 2019
build.sbt

README.md

stm4cats

Maven Central Build Status Coverage Status

An implementation of STM for any cats-effect compatible effect type.

Current stable version is 0.1.0-M1, available for Scala 2.11 and 2.12 and Scala.JS 0.6:

// Use %%% for Scala.JS
libraryDependencies += "com.olegpy" %% "stm4cats" % "0.1.0-M1"

Or, if you're feeling adventurous, a snapshot is build from master on each commit.

resolvers += Resolver.sonatypeRepo("snapshots")
libraryDependencies += "com.olegpy" %% "stm4cats" % "0.1.0-SNAPSHOT"

Try it

import cats.implicits._
import cats.effect.IO
import com.olegpy.stm._
import scala.concurrent.ExecutionContext.global
import scala.concurrent.duration._

implicit val cs = IO.contextShift(global)
implicit val timer = IO.timer(global)


def transfer(fromR: TRef[Int], toR: TRef[Int], amt: Int): STM[Unit] =
  for {
    from <- fromR.get
    if from >= amt // Or STM.check(from >= amt)
    _ <- fromR.update(_ - amt)
    _ <- toR.update(_ + amt)
  } yield ()
  
def freeMoney(toR: TRef[Int]): IO[Unit] = STM.atomically[IO] {
  toR.update(_ + 10)
}

val io = for {
  fromR <- TRef(0).commit[IO]
  // Or shorter syntax:
  toR   <- TRef.in[IO](0)
  amt   =  100

  f1    <- transfer(fromR, toR, amt).commit[IO].start
  f2    <- (freeMoney(fromR) >> IO.sleep(1.second)).foreverM.start
  // In 10 seconds, the transfer succeeds
  _     <- f1.join
  _     <- f2.cancel
  res   <- toR.get.commit[IO]
  _     <- IO(assert(res == amt))
} yield ()

io.unsafeRunSync() // Well, not on JS

Acknowledgements

My interest in STM, as well as some of API in stm4cats was influenced by:

You can’t perform that action at this time.