TQueue

A convenience implementation of a queue in the Txn monad, built on top of TVar.

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.syntax.semigroup._
import cats.instances.string._

import io.github.timwspence.cats.stm.STM

val stm = STM.runtime[IO].unsafeRunSync()
// stm: STM[IO] = io.github.timwspence.cats.stm.STM$Make$$anon$1$$anon$2@5e445b49
import stm._

val txn: Txn[String] = for {
  tqueue <- TQueue.empty[String]
  _      <- tqueue.put("hello")
  _      <- tqueue.put("world")
  hello  <- tqueue.read
  world  <- tqueue.peek
} yield hello |+| world
// txn: Txn[String] = Bind(
//   txn = Bind(
//     txn = Alloc(
//       v = Delay(
//         thunk = cats.effect.IO$$$Lambda$11318/433264122@27e3207a,
//         event = cats.effect.tracing.TracingEvent$StackTrace
//       )
//     ),
//     f = scala.Function1$$Lambda$11378/1900052463@5d5d22f9
//   ),
//   f = <function1>
// )

val result = stm.commit(txn).unsafeRunSync()
// result: String = "helloworld"