sirmax / cats-helper

Helpers for cats & cats-effect

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cats Helper

Build Status Coverage Status Codacy Badge Version License: MIT

ClockHelper

import com.evolutiongaming.catshelper.ClockHelper._

val clock = Clock.const[Id](nanos = 1000, millis = 2)

clock.millis // 2
clock.nanos // 1000
clock.micros // 1
clock.instant // Instant.ofEpochMilli(2)

SerialRef

Like Ref but allows A => F[A] rather than A => A
Ensures that updates are run serially

import com.evolutiongaming.catshelper.SerialRef

for {
  ref <- SerialRef.of[IO, Int](0)
  _   <- ref.update(a => (a + 1).pure[IO])
} yield {}

LazyVal

Functional alternative to lazy keyword in Scala

trait LazyVal[F[_], A] {

  def get: F[A]

  def getLoaded: F[Option[A]]
}

ToFuture & FromFuture

trait ToFuture[F[_]] {
  def apply[A](fa: F[A]): Future[A]
}

trait FromFuture[F[_]] {
  def apply[A](future: => Future[A]): F[A]
}

ToTry & FromTry

trait ToTry[F[_]] {

  def apply[A](fa: F[A]): Try[A]
}

trait FromTry[F[_]] {

  def apply[A](fa: Try[A]): F[A]
}

Log

trait Log[F[_]] {

  def debug(msg: => String): F[Unit]

  def info(msg: => String): F[Unit]

  def warn(msg: => String): F[Unit]

  def warn(msg: => String, cause: Throwable): F[Unit]

  def error(msg: => String): F[Unit]

  def error(msg: => String, cause: Throwable): F[Unit]
}

Runtime

trait Runtime[F[_]] {

  def availableCores: F[Int]

  def freeMemory: F[Long]

  def totalMemory: F[Long]

  def maxMemory: F[Long]

  def gc: F[Unit]
}

ThreadLocalRef

trait ThreadLocalRef[F[_], A] {

  def get: F[A]

  def set(a: A): F[Unit]

  def update(f: A => A): F[Unit]

  def modify[B](f: A => (A, B)): F[B]
}

ResourceFenced

This is useful to ensure release called at most once, in cases when "unsafe" api like Resource.allocated being used

val resource: Resource[F, A] = ???
resource.fenced

ReadWriteRef

A mutable reference to A value with read-write lock semantics.

FeatureToggled

Manages a given Resource[F, A] providing access to it only when a feature-toggle is on.

val serviceResource: Resource[F, AService] = ???
val flag: F[Boolean] = ???

val ftService: Resource[F, Resource[F, Option[AService]]] = FeatureToggled
  .polling(
    serviceResource,
    flag,
    pollInterval = 10.seconds,
    gracePeriod = 30.seconds,
  )

ftService.use { access =>
  access.use {
    case Some(service) => service.doStuff(…)
    case None          => F.unit
  }
}

PureTest

This helper lives in a separate cats-helper-testkit module. It is makes testing F[_]-based code easier.

NOTE: cats-helper-testkit is an experimental module and may break SemVer guarantees from time to time. However we will do our best to avoid unnecessary breakages.

"what time is it now?" in PureTest[IO].of { env =>
  import env._
  for {
    _ <- IO.sleep(1.hour)
    _ <- testRuntime.getTimeSinceStart.map(_ shouldBe 1.hour)
  } yield ()
}

Setup

addSbtPlugin("com.evolution" % "sbt-artifactory-plugin" % "0.0.2")

libraryDependencies += "com.evolutiongaming" %% "cats-helper" % "2.2.3"

About

Helpers for cats & cats-effect

License:MIT License


Languages

Language:Scala 100.0%