/serially

Primary LanguageScalaMIT LicenseMIT

Serially

Build Status Coverage Status Codacy Badge Version License: MIT

This library contains Serially.scala which allows to run tasks serially The behavior is somehow similar to what actors propose, however it provides typesafety. Also it is easy to write tests using Serially.now to avoid unnecessary concurrency.

Serially example

This example explains how we can ensure that there are no concurrent updates to var state

val system = ActorSystem() // yes, we have dependency on akka
val serially = Serially()

var state: Int = 0

// this runs sequentially, like message handling in actors 
serially {
 state = state + 1
}

// you also can expose computation result as Future[T]
val stateBefore: Future[Int] = serially {
  val stateBefore = state
  state = state + 1
  stateBefore
} 

StateVar example

Basically the same as on previous example with less code

val system = ActorSystem()
val serially = Serially()
val state = StateVar(0, serially)
val result: Future[String] = state { before => 
  val after = before + 1
  (after, "ok")
} 

Setup

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

libraryDependencies += "com.evolutiongaming" %% "serially" % "1.0.6"