GithubHelp home page GithubHelp logo

sequentially's Introduction

Sequentially

Build Status Coverage Status Codacy Badge Version License: MIT

This library contains Sequentially.scala which allows running tasks sequentially for the same key and in parallel for different keys The behavior is somehow similar to what actors propose, however it provides typesafety. Also, it is easy to write tests using Sequentially.now to avoid an unnecessary concurrency.

trait Sequentially[-K] {
  def apply[KK <: K, T](key: KK)(task: => T): Future[T]
}

Example

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

type Key = Unit // ok for example
val system = ActorSystem() // yes, we have dependency on akka
val sequentially = Sequentially[Key](system)

var state: Int = 0

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

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

Other good stuff

We usually have more complicated requirements in real life applications, thus we have implemented more powerful Sequentially-like structures

trait SequentiallyAsync[-K] extends Sequentially[K] {

  def async[KK <: K, T](key: K)(task: => Future[T]): Future[T]
}
trait SequentiallyHandler[-K] extends SequentiallyAsync[K] {

  def handler[KK <: K, T](key: KK)(task: => Future[() => Future[T]]): Future[T]
}

And also we have mixed TrieMap with Sequentially, results can be found in

  • AsyncMap.scala
  • SequentialMap.scala
  • AsyncHandlerMap.scala

Setup

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

libraryDependencies += "com.evolutiongaming" %% "sequentially" % "1.1.5"

sequentially's People

Contributors

dfakhritdinov avatar maxerothan avatar mr-git avatar scala-steward avatar t3hnar avatar terjokhin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.