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: